diff --git a/.chloggen/anup_statsd_receiver_distribution_support.yaml b/.chloggen/anup_statsd_receiver_distribution_support.yaml new file mode 100755 index 0000000000000..4181e1fbdb8ba --- /dev/null +++ b/.chloggen/anup_statsd_receiver_distribution_support.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: receiver/statsdreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Add support for distribution type metrics in the statsdreceiver." + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [24768] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/receiver/statsdreceiver/README.md b/receiver/statsdreceiver/README.md index d6dce9aab4996..db55b5aa773c2 100644 --- a/receiver/statsdreceiver/README.md +++ b/receiver/statsdreceiver/README.md @@ -38,7 +38,7 @@ The Following settings are optional: - `timer_histogram_mapping:`(default value is below): Specify what OTLP type to convert received timing/histogram data to. -`"statsd_type"` specifies received Statsd data type. Possible values for this setting are `"timing"`, `"timer"` and `"histogram"`. +`"statsd_type"` specifies received Statsd data type. Possible values for this setting are `"timing"`, `"timer"`, `"histogram"` and `"distribution"`. `"observer_type"` specifies OTLP data type to convert to. We support `"gauge"`, `"summary"`, and `"histogram"`. For `"gauge"`, it does not perform any aggregation. For `"summary`, the statsD receiver will aggregate to one OTLP summary metric for one metric description (the same metric name with the same tags). It will send percentile 0, 10, 50, 90, 95, 100 to the downstream. The `"histogram"` setting selects an [auto-scaling exponential histogram configured with only a maximum size](https://github.com/lightstep/go-expohisto#readme), as shown in the example below. @@ -61,6 +61,10 @@ receivers: observer_type: "histogram" histogram: max_size: 100 + - statsd_type: "distribution" + observer_type: "histogram" + histogram: + max_size: 50 ``` The full list of settings exposed for this receiver are documented [here](./config.go) @@ -138,6 +142,10 @@ receivers: observer_type: "histogram" histogram: max_size: 50 + - statsd_type: "distribution" + observer_type: "histogram" + histogram: + max_size: 50 - statsd_type: "timing" observer_type: "summary" diff --git a/receiver/statsdreceiver/config.go b/receiver/statsdreceiver/config.go index 67a92b493f275..76dd700b63740 100644 --- a/receiver/statsdreceiver/config.go +++ b/receiver/statsdreceiver/config.go @@ -39,7 +39,7 @@ func (c *Config) Validate() error { } switch eachMap.StatsdType { - case protocol.TimingTypeName, protocol.TimingAltTypeName, protocol.HistogramTypeName: + case protocol.TimingTypeName, protocol.TimingAltTypeName, protocol.HistogramTypeName, protocol.DistributionTypeName: // do nothing case protocol.CounterTypeName, protocol.GaugeTypeName: fallthrough diff --git a/receiver/statsdreceiver/config_test.go b/receiver/statsdreceiver/config_test.go index 73cad547990b2..ecfb38ecc0e65 100644 --- a/receiver/statsdreceiver/config_test.go +++ b/receiver/statsdreceiver/config_test.go @@ -54,6 +54,13 @@ func TestLoadConfig(t *testing.T) { MaxSize: 170, }, }, + { + StatsdType: "distribution", + ObserverType: "histogram", + Histogram: protocol.HistogramConfig{ + MaxSize: 170, + }, + }, }, }, }, diff --git a/receiver/statsdreceiver/factory.go b/receiver/statsdreceiver/factory.go index a83cc2255885f..52e8abc48375f 100644 --- a/receiver/statsdreceiver/factory.go +++ b/receiver/statsdreceiver/factory.go @@ -25,7 +25,7 @@ const ( ) var ( - defaultTimerHistogramMapping = []protocol.TimerHistogramMapping{{StatsdType: "timer", ObserverType: "gauge"}, {StatsdType: "histogram", ObserverType: "gauge"}} + defaultTimerHistogramMapping = []protocol.TimerHistogramMapping{{StatsdType: "timer", ObserverType: "gauge"}, {StatsdType: "histogram", ObserverType: "gauge"}, {StatsdType: "distribution", ObserverType: "gauge"}} ) // NewFactory creates a factory for the StatsD receiver. diff --git a/receiver/statsdreceiver/internal/protocol/statsd_parser.go b/receiver/statsdreceiver/internal/protocol/statsd_parser.go index 6969de362c4db..d375f950ed3cb 100644 --- a/receiver/statsdreceiver/internal/protocol/statsd_parser.go +++ b/receiver/statsdreceiver/internal/protocol/statsd_parser.go @@ -33,16 +33,18 @@ type ( const ( tagMetricType = "metric_type" - CounterType MetricType = "c" - GaugeType MetricType = "g" - HistogramType MetricType = "h" - TimingType MetricType = "ms" - - CounterTypeName TypeName = "counter" - GaugeTypeName TypeName = "gauge" - HistogramTypeName TypeName = "histogram" - TimingTypeName TypeName = "timing" - TimingAltTypeName TypeName = "timer" + CounterType MetricType = "c" + GaugeType MetricType = "g" + HistogramType MetricType = "h" + TimingType MetricType = "ms" + DistributionType MetricType = "d" + + CounterTypeName TypeName = "counter" + GaugeTypeName TypeName = "gauge" + HistogramTypeName TypeName = "histogram" + TimingTypeName TypeName = "timing" + TimingAltTypeName TypeName = "timer" + DistributionTypeName TypeName = "distribution" GaugeObserver ObserverType = "gauge" SummaryObserver ObserverType = "summary" @@ -143,6 +145,8 @@ func (t MetricType) FullName() TypeName { return TimingTypeName case HistogramType: return HistogramTypeName + case DistributionType: + return DistributionTypeName } return TypeName(fmt.Sprintf("unknown(%s)", t)) } @@ -162,7 +166,7 @@ func (p *StatsDParser) Initialize(enableMetricType bool, isMonotonicCounter bool // Note: validation occurs in ("../".Config).validate() for _, eachMap := range sendTimerHistogram { switch eachMap.StatsdType { - case HistogramTypeName: + case HistogramTypeName, DistributionTypeName: p.histogramEvents.method = eachMap.ObserverType p.histogramEvents.histogramConfig = expoHistogramConfig(eachMap.Histogram) case TimingTypeName, TimingAltTypeName: @@ -255,7 +259,7 @@ var timeNowFunc = time.Now func (p *StatsDParser) observerCategoryFor(t MetricType) ObserverCategory { switch t { - case HistogramType: + case HistogramType, DistributionType: return p.histogramEvents case TimingType: return p.timerEvents @@ -301,7 +305,7 @@ func (p *StatsDParser) Aggregate(line string, addr net.Addr) error { point.SetIntValue(point.IntValue() + parsedMetric.counterValue()) } - case TimingType, HistogramType: + case TimingType, HistogramType, DistributionType: category := p.observerCategoryFor(parsedMetric.description.metricType) switch category.method { case GaugeObserver: @@ -372,7 +376,7 @@ func parseMessageToMetric(line string, enableMetricType bool) (statsDMetric, err inType := MetricType(parts[1]) switch inType { - case CounterType, GaugeType, HistogramType, TimingType: + case CounterType, GaugeType, HistogramType, TimingType, DistributionType: result.description.metricType = inType default: return result, fmt.Errorf("unsupported metric type: %s", inType) diff --git a/receiver/statsdreceiver/internal/protocol/statsd_parser_test.go b/receiver/statsdreceiver/internal/protocol/statsd_parser_test.go index d2b0e5a6704b8..db562409c2edb 100644 --- a/receiver/statsdreceiver/internal/protocol/statsd_parser_test.go +++ b/receiver/statsdreceiver/internal/protocol/statsd_parser_test.go @@ -409,6 +409,17 @@ func Test_ParseMessageToMetricWithMetricType(t *testing.T) { []string{"metric_type"}, []string{"histogram"}), }, + { + name: "int distribution", + input: "test.metric:42|d", + wantMetric: testStatsDMetric( + "test.metric", + 42, + false, + "d", 0, + []string{"metric_type"}, + []string{"distribution"}), + }, } for _, tt := range tests { @@ -632,12 +643,14 @@ func TestStatsDParser_Aggregate(t *testing.T) { }, }, { - name: "counter and gauge: 2 timings and 2 histograms", + name: "counter and gauge: 2 timings and 2 histograms and 2 distribution", input: []string{ "statsdTestMetric1:500|ms|#mykey:myvalue", "statsdTestMetric1:400|h|#mykey:myvalue", "statsdTestMetric1:300|ms|#mykey:myvalue", "statsdTestMetric1:10|h|@0.1|#mykey:myvalue", + "statsdTestMetric1:100|d|#mykey:myvalue", + "statsdTestMetric1:200|d|#mykey:myvalue", }, expectedGauges: map[statsDMetricDescription]pmetric.ScopeMetrics{}, expectedCounters: map[statsDMetricDescription]pmetric.ScopeMetrics{}, @@ -646,6 +659,8 @@ func TestStatsDParser_Aggregate(t *testing.T) { buildGaugeMetric(testStatsDMetric("statsdTestMetric1", 400, false, "h", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)), buildGaugeMetric(testStatsDMetric("statsdTestMetric1", 300, false, "ms", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)), buildGaugeMetric(testStatsDMetric("statsdTestMetric1", 10, false, "h", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)), + buildGaugeMetric(testStatsDMetric("statsdTestMetric1", 100, false, "d", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)), + buildGaugeMetric(testStatsDMetric("statsdTestMetric1", 200, false, "d", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)), }, }, } @@ -933,6 +948,30 @@ func TestStatsDParser_AggregateTimerWithSummary(t *testing.T) { }, }, }, + { + name: "distribution", + input: []string{ + "statsdTestMetric1:1|d|#mykey:myvalue", + "statsdTestMetric2:2|d|#mykey:myvalue", + "statsdTestMetric1:1|d|#mykey:myvalue", + "statsdTestMetric1:10|d|#mykey:myvalue", + "statsdTestMetric1:20|d|#mykey:myvalue", + "statsdTestMetric2:5|d|#mykey:myvalue", + "statsdTestMetric2:10|d|#mykey:myvalue", + }, + expectedSummaries: map[statsDMetricDescription]summaryMetric{ + testDescription("statsdTestMetric1", "d", + []string{"mykey"}, []string{"myvalue"}): { + points: []float64{1, 1, 10, 20}, + weights: []float64{1, 1, 1, 1}, + }, + testDescription("statsdTestMetric2", "d", + []string{"mykey"}, []string{"myvalue"}): { + points: []float64{2, 5, 10}, + weights: []float64{1, 1, 1}, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1319,6 +1358,33 @@ func TestStatsDParser_AggregateTimerWithHistogram(t *testing.T) { }(), mapping: normalMapping, }, + { + name: "one_each_distribution", + input: []string{ + "expohisto:1|d|#mykey:myvalue", + "expohisto:0|d|#mykey:myvalue", + "expohisto:-1|d|#mykey:myvalue", + }, + expected: func() pmetric.Metrics { + data, dp := newPoint() + dp.SetCount(3) + dp.SetSum(0) + dp.SetMin(-1) + dp.SetMax(1) + dp.SetZeroCount(1) + dp.SetScale(logarithm.MaxScale) + dp.Positive().SetOffset(-1) + dp.Negative().SetOffset(-1) + dp.Positive().BucketCounts().FromRaw([]uint64{ + 1, + }) + dp.Negative().BucketCounts().FromRaw([]uint64{ + 1, + }) + return data + }(), + mapping: normalMapping, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/receiver/statsdreceiver/testdata/config.yaml b/receiver/statsdreceiver/testdata/config.yaml index cec588b989aac..d3984fb9684cb 100644 --- a/receiver/statsdreceiver/testdata/config.yaml +++ b/receiver/statsdreceiver/testdata/config.yaml @@ -11,3 +11,7 @@ statsd/receiver_settings: observer_type: "histogram" histogram: max_size: 170 + - statsd_type: "distribution" + observer_type: "histogram" + histogram: + max_size: 170