diff --git a/exporter_test.go b/exporter_test.go index 84fb967..45eaafb 100644 --- a/exporter_test.go +++ b/exporter_test.go @@ -591,6 +591,51 @@ func TestHistogramUnits(t *testing.T) { t.Fatalf("Received unexpected value for histogram observation %f != .300", *value) } } +func TestCounterIncrement(t *testing.T) { + // Start exporter with a synchronous channel + events := make(chan Events) + go func() { + testMapper := mapper.MetricMapper{} + testMapper.InitCache(0) + ex := NewExporter(&testMapper) + ex.Listen(events) + }() + + // Synchronously send a statsd event to wait for handleEvent execution. + // Then close events channel to stop a listener. + name := "foo_counter" + labels := map[string]string{ + "foo": "bar", + } + c := Events{ + &CounterEvent{ + metricName: name, + value: 1, + labels: labels, + }, + &CounterEvent{ + metricName: name, + value: 1, + labels: labels, + }, + } + events <- c + events <- 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, labels) + if value == nil { + t.Fatal("Counter value should not be nil") + } + if *value != 2 { + t.Fatalf("Counter wasn't incremented properly") + } +} type statsDPacketHandler interface { handlePacket(packet []byte, e chan<- Events) @@ -764,6 +809,37 @@ mappings: } } +func TestHashLabelNames(t *testing.T) { + r := newRegistry(nil) + // Validate value hash changes and name has doesn't when just the value changes. + hash1, _ := r.hashLabels(map[string]string{ + "label": "value1", + }) + hash2, _ := r.hashLabels(map[string]string{ + "label": "value2", + }) + if hash1.names != hash2.names { + t.Fatal("Hash of label names should match, but doesn't") + } + if hash1.values == hash2.values { + t.Fatal("Hash of label names shouldn't match, but do") + } + + // Validate value and name hashes change when the name changes. + hash1, _ = r.hashLabels(map[string]string{ + "label1": "value", + }) + hash2, _ = r.hashLabels(map[string]string{ + "label2": "value", + }) + if hash1.names == hash2.names { + t.Fatal("Hash of label names shouldn't match, but do") + } + if hash1.values == hash2.values { + t.Fatal("Hash of label names shouldn't match, but do") + } +} + // getFloat64 search for metric by name in array of MetricFamily and then search a value by labels. // Method returns a value or nil if metric is not found. func getFloat64(metrics []*dto.MetricFamily, name string, labels prometheus.Labels) *float64 {