Repository: prometheus/statsd_exporter
Pull request #264 (2019-09-09 07:25:07)

Support sampling factor for all statsd metric types
This is an attempted fix for #250 



Patch:
diff --git a/exporter.go b/exporter.go
index c252e82..fce1480 100644
--- a/exporter.go
+++ b/exporter.go
@@ -389,11 +389,7 @@ samples:
 			for _, component := range components[2:] {
 				switch component[0] {
 				case '@':
-					if statType != "c" && statType != "ms" {
-						log.Debugln("Illegal sampling factor for non-counter metric 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" {
+					} else if statType == "ms" || statType == "h" || statType == "d" {
 						multiplyEvents = int(1 / samplingFactor)
 					}
 				case '#':


Test patch:
diff --git a/bridge_test.go b/bridge_test.go
index e243ed1..937022a 100644
--- a/bridge_test.go
+++ b/bridge_test.go
@@ -46,6 +46,16 @@ func TestHandlePacket(t *testing.T) {
 					labels:     map[string]string{},
 				},
 			},
+		}, {
+			name: "gauge with sampling",
+			in:   "foo:3|g|@0.2",
+			out: Events{
+				&GaugeEvent{
+					metricName: "foo",
+					value:      3,
+					labels:     map[string]string{},
+				},
+			},
 		}, {
 			name: "gauge decrement",
 			in:   "foo:-10|g",
@@ -87,6 +97,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",
@@ -157,6 +197,36 @@ func TestHandlePacket(t *testing.T) {
 					labels:     map[string]string{"tag1": "bar", "tag2": "baz"},
 				},
 			},
+		}, {
+			name: "histogram with sampling",
+			in:   "foo:0.01|h|@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 with multiple colons",
 			in:   "foo:100|c|@0.1|#tag1:foo:bar",