From 1bddef857d0e9aa7926498440807f979abc32e57 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Fri, 13 Sep 2019 14:27:35 +1000 Subject: [PATCH] Add test Signed-off-by: Amit Saha --- bridge_test.go | 30 ++++++++++++++++++++++++++++++ exporter.go | 12 +++++------- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/bridge_test.go b/bridge_test.go index fe55afc..9791dea 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -87,6 +87,36 @@ func TestHandlePacket(t *testing.T) { labels: map[string]string{}, }, }, + }, { + name: "distribution with sampling", + in: "foo:0.01|d|@0.2|#tag1:bar,#tag2:baz", + out: Events{ + &TimerEvent{ + metricName: "foo", + value: 0.01, + labels: map[string]string{"tag1": "bar", "tag2": "baz"}, + }, + &TimerEvent{ + metricName: "foo", + value: 0.01, + labels: map[string]string{"tag1": "bar", "tag2": "baz"}, + }, + &TimerEvent{ + metricName: "foo", + value: 0.01, + labels: map[string]string{"tag1": "bar", "tag2": "baz"}, + }, + &TimerEvent{ + metricName: "foo", + value: 0.01, + labels: map[string]string{"tag1": "bar", "tag2": "baz"}, + }, + &TimerEvent{ + metricName: "foo", + value: 0.01, + labels: map[string]string{"tag1": "bar", "tag2": "baz"}, + }, + }, }, { name: "datadog tag extension", in: "foo:100|c|#tag1:bar,tag2:baz", diff --git a/exporter.go b/exporter.go index 65d8ef7..fce1480 100644 --- a/exporter.go +++ b/exporter.go @@ -389,11 +389,7 @@ samples: for _, component := range components[2:] { switch component[0] { case '@': - if statType == "g" { - log.Debugln("Illegal sampling factor for gauge metric type on line", line) - sampleErrors.WithLabelValues("illegal_sample_factor").Inc() - continue - } + samplingFactor, err = strconv.ParseFloat(component[1:], 64) if err != nil { log.Debugf("Invalid sampling factor %s on line %s", component[1:], line) @@ -403,9 +399,11 @@ samples: samplingFactor = 1 } - if statType == "c" { + if statType == "g" { + continue + } else if statType == "c" { value /= samplingFactor - } else if statType == "ms" || statType == "h" { + } else if statType == "ms" || statType == "h" || statType == "d" { multiplyEvents = int(1 / samplingFactor) } case '#':