From 7188ed4292d96c964aa36db20eb793ef8325a915 Mon Sep 17 00:00:00 2001 From: Matthias Rampke Date: Mon, 15 Jan 2024 22:42:36 +0100 Subject: [PATCH] Add more tests for gauge increment/decrement Validate the behavior for relative gauge events as per [the statsd type metric type documentatoin](https://github.com/statsd/statsd/blob/master/docs/metric_types.md#gauges). Reaffirms #523. Signed-off-by: Matthias Rampke --- bridge_test.go | 52 +++++++++++++++++++++++++++++++++++ pkg/exporter/exporter_test.go | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/bridge_test.go b/bridge_test.go index bc92e48..ae2aff7 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -81,6 +81,58 @@ func TestHandlePacket(t *testing.T) { GLabels: map[string]string{}, }, }, + }, { + name: "gauge increment", + in: "foo:+10|g", + out: event.Events{ + &event.GaugeEvent{ + GMetricName: "foo", + GValue: 10, + GRelative: true, + GLabels: map[string]string{}, + }, + }, + }, { + name: "gauge set negative", + in: "foo:0|g\nfoo:-1|g", + out: event.Events{ + &event.GaugeEvent{ + GMetricName: "foo", + GValue: 0, + GRelative: false, + GLabels: map[string]string{}, + }, + &event.GaugeEvent{ + GMetricName: "foo", + GValue: -1, + GRelative: true, + GLabels: map[string]string{}, + }, + }, + }, { + // Test the sequence given here https://github.com/statsd/statsd/blob/master/docs/metric_types.md#gauges + name: "gauge up and down", + in: "gaugor:333|g\ngaugor:-10|g\ngaugor:+4|g", + out: event.Events{ + &event.GaugeEvent{ + GMetricName: "gaugor", + GValue: 333, + GRelative: false, + GLabels: map[string]string{}, + }, + &event.GaugeEvent{ + GMetricName: "gaugor", + GValue: -10, + GRelative: true, + GLabels: map[string]string{}, + }, + &event.GaugeEvent{ + GMetricName: "gaugor", + GValue: 4, + GRelative: true, + GLabels: map[string]string{}, + }, + }, }, { name: "simple timer", in: "foo:200|ms", diff --git a/pkg/exporter/exporter_test.go b/pkg/exporter/exporter_test.go index 2ac0874..da08aee 100644 --- a/pkg/exporter/exporter_test.go +++ b/pkg/exporter/exporter_test.go @@ -902,6 +902,58 @@ func TestCounterIncrement(t *testing.T) { } } +// Test case from https://github.com/statsd/statsd/blob/master/docs/metric_types.md#gauges +func TestGaugeIncrementDecrement(t *testing.T) { + // Start exporter with a synchronous channel + events := make(chan event.Events) + go func() { + testMapper := mapper.MetricMapper{} + ex := NewExporter(prometheus.DefaultRegisterer, &testMapper, log.NewNopLogger(), eventsActions, eventsUnmapped, errorEventStats, eventStats, conflictingEventStats, metricsCount) + ex.Listen(events) + }() + + // Synchronously send a statsd event to wait for handleEvent execution. + // Then close events channel to stop a listener. + name := "gaugor" + c := event.Events{ + &event.GaugeEvent{ + GMetricName: "gaugor", + GValue: 333, + GRelative: false, + GLabels: map[string]string{}, + }, + &event.GaugeEvent{ + GMetricName: "gaugor", + GValue: -10, + GRelative: true, + GLabels: map[string]string{}, + }, + &event.GaugeEvent{ + GMetricName: "gaugor", + GValue: 4, + GRelative: true, + GLabels: map[string]string{}, + }, + } + events <- c + // Push empty event so that we block until the first event is consumed. + events <- event.Events{} + close(events) + + // Check histogram value + metrics, err := prometheus.DefaultGatherer.Gather() + if err != nil { + t.Fatalf("Cannot gather from DefaultGatherer: %v", err) + } + value := getFloat64(metrics, name, nil) + if value == nil { + t.Fatal("gauge value should not be nil") + } + if *value != 327 { + t.Fatalf("gauge wasn't incremented and decremented properly") + } +} + func TestScaledMapping(t *testing.T) { events := make(chan event.Events) testMapper := mapper.MetricMapper{}