Merge pull request #533 from prometheus/mr/issue-523/test-existing

Add more tests for gauge increment/decrement
This commit is contained in:
Matthias Rampke 2024-01-15 22:52:05 +01:00 committed by GitHub
commit abc3a1f95c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 0 deletions

View file

@ -81,6 +81,58 @@ func TestHandlePacket(t *testing.T) {
GLabels: map[string]string{}, 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", name: "simple timer",
in: "foo:200|ms", in: "foo:200|ms",

View file

@ -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) { func TestScaledMapping(t *testing.T) {
events := make(chan event.Events) events := make(chan event.Events)
testMapper := mapper.MetricMapper{} testMapper := mapper.MetricMapper{}