From e37bee9bf2520fcdaae79113185c105068bfa552 Mon Sep 17 00:00:00 2001 From: Dave Rawks Date: Mon, 14 Aug 2017 11:20:47 -0700 Subject: [PATCH 1/3] Resolves #88 - histograms use wrong units --- exporter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter.go b/exporter.go index 3ebdf44..6f6e20e 100644 --- a/exporter.go +++ b/exporter.go @@ -327,7 +327,7 @@ func (b *Exporter) Listen(e <-chan Events) { mapping, ) if err == nil { - histogram.Observe(event.Value()) + histogram.Observe(event.Value() / 1000) // prometheus presumes seconds, statsd millisecond eventStats.WithLabelValues("timer").Inc() } else { log.Errorf(regErrF, metricName, err) From b2082eda2b101f3a02f78c254c94a76bbed513ee Mon Sep 17 00:00:00 2001 From: Dave Rawks Date: Mon, 14 Aug 2017 15:20:45 -0700 Subject: [PATCH 2/3] Added test for histogram unit conversion --- exporter_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/exporter_test.go b/exporter_test.go index cbfb910..d37f0ba 100644 --- a/exporter_test.go +++ b/exporter_test.go @@ -15,6 +15,7 @@ package main import ( "fmt" + "github.com/prometheus/client_golang/prometheus" "net" "testing" "time" @@ -74,6 +75,44 @@ func TestInvalidUtf8InDatadogTagValue(t *testing.T) { } } +type MockHistogram struct { + prometheus.Metric + prometheus.Collector + value float64 +} + +func (h *MockHistogram) Observe(n float64) { + h.value = n + +} + +func TestHistogramUnits(t *testing.T) { + events := make(chan Events, 1) + c := Events{ + &TimerEvent{ + metricName: "foo", + value: 300, + }, + } + events <- c + ex := NewExporter(&metricMapper{}, true) + ex.mapper.Defaults.TimerType = timerTypeHistogram + + // Close channel to signify we are done with the listener after a short period. + go func() { + time.Sleep(time.Millisecond * 100) + close(events) + }() + mock := &MockHistogram{} + ex.Histograms.Elements[7787632782521330630] = mock + ex.Listen(events) + if mock.value == 300 { + t.Fatalf("Histogram observations not scaled into Seconds") + } else if mock.value != .300 { + t.Fatalf("Received unexpected value for histogram observation %f != .300", mock.value) + } +} + type statsDPacketHandler interface { handlePacket(packet []byte, e chan<- Events) } From 06444ed5c0411bc64e473e81533a2da5bc0df87c Mon Sep 17 00:00:00 2001 From: Dave Rawks Date: Tue, 15 Aug 2017 08:51:13 -0700 Subject: [PATCH 3/3] Minor fix-ups per PR discussion * Removed extraneous newline * Reformatted/ordered imports * Used hash function to derive index of `Elements` instead of hardcoding hash result --- exporter_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/exporter_test.go b/exporter_test.go index d37f0ba..3d81f99 100644 --- a/exporter_test.go +++ b/exporter_test.go @@ -15,10 +15,11 @@ package main import ( "fmt" - "github.com/prometheus/client_golang/prometheus" "net" "testing" "time" + + "github.com/prometheus/client_golang/prometheus" ) // TestNegativeCounter validates when we send a negative @@ -83,14 +84,14 @@ type MockHistogram struct { func (h *MockHistogram) Observe(n float64) { h.value = n - } func TestHistogramUnits(t *testing.T) { events := make(chan Events, 1) + name := "foo" c := Events{ &TimerEvent{ - metricName: "foo", + metricName: name, value: 300, }, } @@ -104,7 +105,8 @@ func TestHistogramUnits(t *testing.T) { close(events) }() mock := &MockHistogram{} - ex.Histograms.Elements[7787632782521330630] = mock + key := hashNameAndLabels(name+"_timer", nil) + ex.Histograms.Elements[key] = mock ex.Listen(events) if mock.value == 300 { t.Fatalf("Histogram observations not scaled into Seconds")