Skip to content

Commit

Permalink
Merge pull request #18 from axoflow/fix-old-event-delay
Browse files Browse the repository at this point in the history
Fix "out of order" event delay metric values
  • Loading branch information
MrAnno authored Apr 2, 2024
2 parents badad47 + da58f47 commit c28c53a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ func main() {
requestTimeout = DEFAULT_TIMEOUT_SYSLOG
}

ctl := syslogngctl.Controller{
ControlChannel: syslogngctl.NewUnixDomainSocketControlChannel(runArgs.SocketAddr),
}
ctl := syslogngctl.NewController(syslogngctl.NewUnixDomainSocketControlChannel(runArgs.SocketAddr))

mux := http.NewServeMux()
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/syslog-ng-ctl/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func main() {
os.Exit(1)
}

ctl := syslogngctl.Controller{
ControlChannel: syslogngctl.NewUnixDomainSocketControlChannel(socketAddr),
}
ctl := syslogngctl.NewController(syslogngctl.NewUnixDomainSocketControlChannel(socketAddr))

cmds := []struct {
Args []string
Expand Down
21 changes: 14 additions & 7 deletions pkg/syslog-ng-ctl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,37 @@ type Controller struct {
lastMetricQueryTime time.Time
}

func (c Controller) GetLicenseInfo(ctx context.Context) (string, error) {
func NewController(controlChannel ControlChannel) *Controller {
return &Controller{
ControlChannel: controlChannel,
lastMetricQueryTime: time.Now(),
}
}

func (c *Controller) GetLicenseInfo(ctx context.Context) (string, error) {
return GetLicenseInfo(ctx, c.ControlChannel)
}

func (c Controller) Ping(ctx context.Context) error {
func (c *Controller) Ping(ctx context.Context) error {
return Ping(ctx, c.ControlChannel)
}

func (c Controller) Reload(ctx context.Context) error {
func (c *Controller) Reload(ctx context.Context) error {
return Reload(ctx, c.ControlChannel)
}

func (c Controller) Stats(ctx context.Context) ([]Stat, error) {
func (c *Controller) Stats(ctx context.Context) ([]Stat, error) {
return Stats(ctx, c.ControlChannel)
}

func (c Controller) OriginalConfig(ctx context.Context) (string, error) {
func (c *Controller) OriginalConfig(ctx context.Context) (string, error) {
return OriginalConfig(ctx, c.ControlChannel)
}

func (c Controller) PreprocessedConfig(ctx context.Context) (string, error) {
func (c *Controller) PreprocessedConfig(ctx context.Context) (string, error) {
return PreprocessedConfig(ctx, c.ControlChannel)
}

func (c Controller) StatsPrometheus(ctx context.Context) ([]*io_prometheus_client.MetricFamily, error) {
func (c *Controller) StatsPrometheus(ctx context.Context) ([]*io_prometheus_client.MetricFamily, error) {
return StatsPrometheus(ctx, c.ControlChannel, &c.lastMetricQueryTime)
}
7 changes: 3 additions & 4 deletions pkg/syslog-ng-ctl/stats_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ func transformEventDelayMetric(delayMetric *io_prometheus_client.MetricFamily, d
delayMetric := m

if d, ok := delayMetricAgeByLabel[fmt.Sprint(m.Label)]; ok {
delayMetricAge := d.GetGauge().GetValue()
delayMetricAge := int(d.GetGauge().GetValue())

lastDelaySampleTS := now.Add(time.Duration(-delayMetricAge * float64(time.Second)))
lastDelaySampleTS := now.Add(time.Duration(-delayMetricAge) * time.Second)
if lastDelaySampleTS.After(lastMetricQueryTime) {
timestampMs := timestamp.FromTime(lastDelaySampleTS)
transformedMetric = append(transformedMetric,
Expand Down Expand Up @@ -161,11 +161,11 @@ func StatsPrometheus(ctx context.Context, cc ControlChannel, lastMetricQueryTime
}

now := time.Now()
defer func() { *lastMetricQueryTime = now }()

var mfs map[string]*io_prometheus_client.MetricFamily
if strings.HasPrefix(rsp, StatsHeader) {
mfs, err = createMetricsFromLegacyStats(rsp)
*lastMetricQueryTime = now
return maps.Values(mfs), err
}

Expand Down Expand Up @@ -214,7 +214,6 @@ func StatsPrometheus(ctx context.Context, cc ControlChannel, lastMetricQueryTime
transformEventDelayMetric(delayMetric, delayMetricAge, now, *lastMetricQueryTime, mfs)
}

*lastMetricQueryTime = now
return maps.Values(mfs), err
}

Expand Down

0 comments on commit c28c53a

Please sign in to comment.