Skip to content

Commit c28c53a

Browse files
authored
Merge pull request #18 from axoflow/fix-old-event-delay
Fix "out of order" event delay metric values
2 parents badad47 + da58f47 commit c28c53a

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ func main() {
8181
requestTimeout = DEFAULT_TIMEOUT_SYSLOG
8282
}
8383

84-
ctl := syslogngctl.Controller{
85-
ControlChannel: syslogngctl.NewUnixDomainSocketControlChannel(runArgs.SocketAddr),
86-
}
84+
ctl := syslogngctl.NewController(syslogngctl.NewUnixDomainSocketControlChannel(runArgs.SocketAddr))
8785

8886
mux := http.NewServeMux()
8987
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {

pkg/syslog-ng-ctl/cmd/main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ func main() {
3232
os.Exit(1)
3333
}
3434

35-
ctl := syslogngctl.Controller{
36-
ControlChannel: syslogngctl.NewUnixDomainSocketControlChannel(socketAddr),
37-
}
35+
ctl := syslogngctl.NewController(syslogngctl.NewUnixDomainSocketControlChannel(socketAddr))
3836

3937
cmds := []struct {
4038
Args []string

pkg/syslog-ng-ctl/controller.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,37 @@ type Controller struct {
2929
lastMetricQueryTime time.Time
3030
}
3131

32-
func (c Controller) GetLicenseInfo(ctx context.Context) (string, error) {
32+
func NewController(controlChannel ControlChannel) *Controller {
33+
return &Controller{
34+
ControlChannel: controlChannel,
35+
lastMetricQueryTime: time.Now(),
36+
}
37+
}
38+
39+
func (c *Controller) GetLicenseInfo(ctx context.Context) (string, error) {
3340
return GetLicenseInfo(ctx, c.ControlChannel)
3441
}
3542

36-
func (c Controller) Ping(ctx context.Context) error {
43+
func (c *Controller) Ping(ctx context.Context) error {
3744
return Ping(ctx, c.ControlChannel)
3845
}
3946

40-
func (c Controller) Reload(ctx context.Context) error {
47+
func (c *Controller) Reload(ctx context.Context) error {
4148
return Reload(ctx, c.ControlChannel)
4249
}
4350

44-
func (c Controller) Stats(ctx context.Context) ([]Stat, error) {
51+
func (c *Controller) Stats(ctx context.Context) ([]Stat, error) {
4552
return Stats(ctx, c.ControlChannel)
4653
}
4754

48-
func (c Controller) OriginalConfig(ctx context.Context) (string, error) {
55+
func (c *Controller) OriginalConfig(ctx context.Context) (string, error) {
4956
return OriginalConfig(ctx, c.ControlChannel)
5057
}
5158

52-
func (c Controller) PreprocessedConfig(ctx context.Context) (string, error) {
59+
func (c *Controller) PreprocessedConfig(ctx context.Context) (string, error) {
5360
return PreprocessedConfig(ctx, c.ControlChannel)
5461
}
5562

56-
func (c Controller) StatsPrometheus(ctx context.Context) ([]*io_prometheus_client.MetricFamily, error) {
63+
func (c *Controller) StatsPrometheus(ctx context.Context) ([]*io_prometheus_client.MetricFamily, error) {
5764
return StatsPrometheus(ctx, c.ControlChannel, &c.lastMetricQueryTime)
5865
}

pkg/syslog-ng-ctl/stats_prometheus.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ func transformEventDelayMetric(delayMetric *io_prometheus_client.MetricFamily, d
129129
delayMetric := m
130130

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

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

163163
now := time.Now()
164+
defer func() { *lastMetricQueryTime = now }()
164165

165166
var mfs map[string]*io_prometheus_client.MetricFamily
166167
if strings.HasPrefix(rsp, StatsHeader) {
167168
mfs, err = createMetricsFromLegacyStats(rsp)
168-
*lastMetricQueryTime = now
169169
return maps.Values(mfs), err
170170
}
171171

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

217-
*lastMetricQueryTime = now
218217
return maps.Values(mfs), err
219218
}
220219

0 commit comments

Comments
 (0)