mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2024-11-25 08:41:00 +00:00
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 <matthias@prometheus.io>
This commit is contained in:
parent
8fdc626bfc
commit
7188ed4292
2 changed files with 104 additions and 0 deletions
|
@ -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",
|
||||
|
|
|
@ -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{}
|
||||
|
|
Loading…
Reference in a new issue