From 3e19b62d8a2e74b16ffb337ddefd5d8ee9fe867a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20V=C3=A1rady?= Date: Tue, 2 Apr 2024 11:09:44 +0200 Subject: [PATCH 1/4] fix(syslog-ng-ctl): add constructor to initialize lastMetricQueryTime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In order not to return too old ("out of order sample") event delay metric values, lastMetricQueryTime is now initialized so that we skip old data points. Signed-off-by: László Várady --- main.go | 4 +--- pkg/syslog-ng-ctl/cmd/main.go | 4 +--- pkg/syslog-ng-ctl/controller.go | 7 +++++++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index fbb70ae..1bd232a 100644 --- a/main.go +++ b/main.go @@ -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) { diff --git a/pkg/syslog-ng-ctl/cmd/main.go b/pkg/syslog-ng-ctl/cmd/main.go index 9effca9..9c1d2d2 100644 --- a/pkg/syslog-ng-ctl/cmd/main.go +++ b/pkg/syslog-ng-ctl/cmd/main.go @@ -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 diff --git a/pkg/syslog-ng-ctl/controller.go b/pkg/syslog-ng-ctl/controller.go index e6b2ae5..f7f54ad 100644 --- a/pkg/syslog-ng-ctl/controller.go +++ b/pkg/syslog-ng-ctl/controller.go @@ -29,6 +29,13 @@ type Controller struct { lastMetricQueryTime time.Time } +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) } From b04f683ee34a0f5dc83fcd8c359c05361b5ba564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20V=C3=A1rady?= Date: Tue, 2 Apr 2024 12:42:23 +0200 Subject: [PATCH 2/4] fix(syslog-ng-ctl): update lastMetricQueryTime by using pointer receivers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: László Várady --- pkg/syslog-ng-ctl/controller.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/syslog-ng-ctl/controller.go b/pkg/syslog-ng-ctl/controller.go index f7f54ad..8cb66db 100644 --- a/pkg/syslog-ng-ctl/controller.go +++ b/pkg/syslog-ng-ctl/controller.go @@ -29,37 +29,37 @@ type Controller struct { lastMetricQueryTime time.Time } -func NewController(controlChannel ControlChannel) Controller { - return Controller{ +func NewController(controlChannel ControlChannel) *Controller { + return &Controller{ ControlChannel: controlChannel, lastMetricQueryTime: time.Now(), } } -func (c Controller) GetLicenseInfo(ctx context.Context) (string, error) { +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) } From 287b50f255f0233a4e209399afe875a0fcd7c3a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20V=C3=A1rady?= Date: Tue, 2 Apr 2024 12:43:09 +0200 Subject: [PATCH 3/4] refactor(syslog-ng-ctl): defer lastMetricQueryTime update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: László Várady --- pkg/syslog-ng-ctl/stats_prometheus.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/syslog-ng-ctl/stats_prometheus.go b/pkg/syslog-ng-ctl/stats_prometheus.go index c6aa9e0..28150a9 100644 --- a/pkg/syslog-ng-ctl/stats_prometheus.go +++ b/pkg/syslog-ng-ctl/stats_prometheus.go @@ -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 } @@ -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 } From da58f47978e3f2a97c9616b013c274cfcc39f495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20V=C3=A1rady?= Date: Tue, 2 Apr 2024 13:10:43 +0200 Subject: [PATCH 4/4] fix(syslog-ng-ctl): fix event delay timestamp precision issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: László Várady --- pkg/syslog-ng-ctl/stats_prometheus.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/syslog-ng-ctl/stats_prometheus.go b/pkg/syslog-ng-ctl/stats_prometheus.go index 28150a9..e46bac1 100644 --- a/pkg/syslog-ng-ctl/stats_prometheus.go +++ b/pkg/syslog-ng-ctl/stats_prometheus.go @@ -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,