From 83a9736e399a3ca4b7950a97b265edd42393148c Mon Sep 17 00:00:00 2001 From: Cluas Date: Tue, 31 Jan 2023 23:12:49 +0800 Subject: [PATCH 1/7] [processor/spanmetricsprocessor] Allow set metrics namespace --- processor/spanmetricsprocessor/config.go | 3 +++ processor/spanmetricsprocessor/processor.go | 17 +++++++++++++++-- .../spanmetricsprocessor/processor_test.go | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/processor/spanmetricsprocessor/config.go b/processor/spanmetricsprocessor/config.go index b46800bb02dd..7b4c1d655afc 100644 --- a/processor/spanmetricsprocessor/config.go +++ b/processor/spanmetricsprocessor/config.go @@ -72,6 +72,9 @@ type Config struct { // MetricsEmitInterval is the time period between when metrics are flushed or emitted to the configured MetricsExporter. MetricsFlushInterval time.Duration `mapstructure:"metrics_flush_interval"` + + // Namespace + Namespace string } // GetAggregationTemporality converts the string value given in the config into a AggregationTemporality. diff --git a/processor/spanmetricsprocessor/processor.go b/processor/spanmetricsprocessor/processor.go index dd056a83401b..15eee60ed799 100644 --- a/processor/spanmetricsprocessor/processor.go +++ b/processor/spanmetricsprocessor/processor.go @@ -46,6 +46,9 @@ const ( metricKeySeparator = string(byte(0)) defaultDimensionsCacheSize = 1000 + + metricLatency = "latency" + metricCallsTotal = "calls_total" ) var defaultLatencyHistogramBucketsMs = []float64{ @@ -317,11 +320,21 @@ func (p *processorImp) buildMetrics() pmetric.Metrics { return m } +// buildMetricName builds a metric name by concatenating the namespace and the metric name with an underscore. +// If the namespace is not empty, the namespace and metric name will be separated by an underscore. +// Otherwise, only the metric name will be returned. +func buildMetricName(namespace, metricName string) string { + if namespace != "" { + return namespace + "_" + metricName + } + return metricName +} + // collectLatencyMetrics collects the raw latency metrics, writing the data // into the given instrumentation library metrics. func (p *processorImp) collectLatencyMetrics(ilm pmetric.ScopeMetrics) { mLatency := ilm.Metrics().AppendEmpty() - mLatency.SetName("latency") + mLatency.SetName(buildMetricName(p.config.Namespace, metricLatency)) mLatency.SetUnit("ms") mLatency.SetEmptyHistogram().SetAggregationTemporality(p.config.GetAggregationTemporality()) dps := mLatency.Histogram().DataPoints() @@ -349,7 +362,7 @@ func (p *processorImp) collectLatencyMetrics(ilm pmetric.ScopeMetrics) { // into the given instrumentation library metrics. func (p *processorImp) collectCallMetrics(ilm pmetric.ScopeMetrics) { mCalls := ilm.Metrics().AppendEmpty() - mCalls.SetName("calls_total") + mCalls.SetName(buildMetricName(p.config.Namespace, metricCallsTotal)) mCalls.SetEmptySum().SetIsMonotonic(true) mCalls.Sum().SetAggregationTemporality(p.config.GetAggregationTemporality()) dps := mCalls.Sum().DataPoints() diff --git a/processor/spanmetricsprocessor/processor_test.go b/processor/spanmetricsprocessor/processor_test.go index d99e48c32490..8937f172311c 100644 --- a/processor/spanmetricsprocessor/processor_test.go +++ b/processor/spanmetricsprocessor/processor_test.go @@ -1101,3 +1101,22 @@ func TestConsumeTracesEvictedCacheKey(t *testing.T) { wg.Wait() assert.Empty(t, wantDataPointCounts) } + +func TestBuildMetricName(t *testing.T) { + tests := []struct { + namespace string + metricName string + expected string + }{ + {"", "metric", "metric"}, + {"ns", "metric", "ns_metric"}, + {"longer_namespace", "metric", "longer_namespace_metric"}, + } + + for _, test := range tests { + actual := buildMetricName(test.namespace, test.metricName) + if actual != test.expected { + t.Errorf("buildMetricName(%q, %q) = %q, expected %q", test.namespace, test.metricName, actual, test.expected) + } + } +} From 08346fa237f90e341885d1833fc9af4fbaf16f16 Mon Sep 17 00:00:00 2001 From: Cluas Date: Tue, 31 Jan 2023 23:31:19 +0800 Subject: [PATCH 2/7] [processor/spanmetricsprocessor] Add namespace comment --- processor/spanmetricsprocessor/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/processor/spanmetricsprocessor/config.go b/processor/spanmetricsprocessor/config.go index 7b4c1d655afc..5bbc20e0dd32 100644 --- a/processor/spanmetricsprocessor/config.go +++ b/processor/spanmetricsprocessor/config.go @@ -73,7 +73,7 @@ type Config struct { // MetricsEmitInterval is the time period between when metrics are flushed or emitted to the configured MetricsExporter. MetricsFlushInterval time.Duration `mapstructure:"metrics_flush_interval"` - // Namespace + // Namespace defines the namespace for the metrics exported by the spanmetricsprocessor. Namespace string } From 121272d9b37add09d85ba57b5e198c5be1be966a Mon Sep 17 00:00:00 2001 From: Cluas Date: Sat, 4 Feb 2023 00:36:51 +0800 Subject: [PATCH 3/7] feat: use assert.Equal --- processor/spanmetricsprocessor/processor_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/processor/spanmetricsprocessor/processor_test.go b/processor/spanmetricsprocessor/processor_test.go index 8937f172311c..be7910620e1b 100644 --- a/processor/spanmetricsprocessor/processor_test.go +++ b/processor/spanmetricsprocessor/processor_test.go @@ -1115,8 +1115,6 @@ func TestBuildMetricName(t *testing.T) { for _, test := range tests { actual := buildMetricName(test.namespace, test.metricName) - if actual != test.expected { - t.Errorf("buildMetricName(%q, %q) = %q, expected %q", test.namespace, test.metricName, actual, test.expected) - } + assert.Equal(t, test.expected, actual) } } From a9d19ecce9187c640aa33f9b79d950a03e6893dc Mon Sep 17 00:00:00 2001 From: Cluas Date: Thu, 9 Feb 2023 23:28:19 +0800 Subject: [PATCH 4/7] feat: add changelog --- ...nmetricsprocessor_support_set_namespace.yaml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .chloggen/spanmetricsprocessor_support_set_namespace.yaml diff --git a/.chloggen/spanmetricsprocessor_support_set_namespace.yaml b/.chloggen/spanmetricsprocessor_support_set_namespace.yaml new file mode 100644 index 000000000000..2de1e3be509e --- /dev/null +++ b/.chloggen/spanmetricsprocessor_support_set_namespace.yaml @@ -0,0 +1,17 @@ +# 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: spanmetricsprocessor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Support set metrics namespace + +# One or more tracking issues related to the change +issues: [] + +# (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: + From 16cbed2840a8aec05bdea026a01ad4150ab4a130 Mon Sep 17 00:00:00 2001 From: Cluas Date: Sat, 11 Feb 2023 16:57:57 +0800 Subject: [PATCH 5/7] feat: metrics name ust otlp spec --- processor/spanmetricsprocessor/processor.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/processor/spanmetricsprocessor/processor.go b/processor/spanmetricsprocessor/processor.go index 15eee60ed799..031778316ea5 100644 --- a/processor/spanmetricsprocessor/processor.go +++ b/processor/spanmetricsprocessor/processor.go @@ -47,8 +47,8 @@ const ( defaultDimensionsCacheSize = 1000 - metricLatency = "latency" - metricCallsTotal = "calls_total" + metricLatency = "latency" + metricCalls = "calls" ) var defaultLatencyHistogramBucketsMs = []float64{ @@ -325,7 +325,7 @@ func (p *processorImp) buildMetrics() pmetric.Metrics { // Otherwise, only the metric name will be returned. func buildMetricName(namespace, metricName string) string { if namespace != "" { - return namespace + "_" + metricName + return namespace + "." + metricName } return metricName } @@ -362,7 +362,7 @@ func (p *processorImp) collectLatencyMetrics(ilm pmetric.ScopeMetrics) { // into the given instrumentation library metrics. func (p *processorImp) collectCallMetrics(ilm pmetric.ScopeMetrics) { mCalls := ilm.Metrics().AppendEmpty() - mCalls.SetName(buildMetricName(p.config.Namespace, metricCallsTotal)) + mCalls.SetName(buildMetricName(p.config.Namespace, metricCalls)) mCalls.SetEmptySum().SetIsMonotonic(true) mCalls.Sum().SetAggregationTemporality(p.config.GetAggregationTemporality()) dps := mCalls.Sum().DataPoints() From 47d4a5b6d1d083dea537f7b36ef9b182a911bdb3 Mon Sep 17 00:00:00 2001 From: Cluas Date: Sat, 11 Feb 2023 20:31:51 +0800 Subject: [PATCH 6/7] feat: revert calls_total naming change --- processor/spanmetricsprocessor/processor.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/processor/spanmetricsprocessor/processor.go b/processor/spanmetricsprocessor/processor.go index 031778316ea5..1050655d0dbe 100644 --- a/processor/spanmetricsprocessor/processor.go +++ b/processor/spanmetricsprocessor/processor.go @@ -47,8 +47,8 @@ const ( defaultDimensionsCacheSize = 1000 - metricLatency = "latency" - metricCalls = "calls" + metricLatency = "latency" + metricCallsTotal = "calls_total" ) var defaultLatencyHistogramBucketsMs = []float64{ @@ -362,7 +362,7 @@ func (p *processorImp) collectLatencyMetrics(ilm pmetric.ScopeMetrics) { // into the given instrumentation library metrics. func (p *processorImp) collectCallMetrics(ilm pmetric.ScopeMetrics) { mCalls := ilm.Metrics().AppendEmpty() - mCalls.SetName(buildMetricName(p.config.Namespace, metricCalls)) + mCalls.SetName(buildMetricName(p.config.Namespace, metricLatency)) mCalls.SetEmptySum().SetIsMonotonic(true) mCalls.Sum().SetAggregationTemporality(p.config.GetAggregationTemporality()) dps := mCalls.Sum().DataPoints() From 83b7b71132b303da70b698b45eb05cefb29a688a Mon Sep 17 00:00:00 2001 From: Cluas Date: Sun, 12 Feb 2023 18:04:09 +0800 Subject: [PATCH 7/7] fix: tests and chlog --- .chloggen/spanmetricsprocessor_support_set_namespace.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/spanmetricsprocessor_support_set_namespace.yaml b/.chloggen/spanmetricsprocessor_support_set_namespace.yaml index 2de1e3be509e..20bc8652a922 100644 --- a/.chloggen/spanmetricsprocessor_support_set_namespace.yaml +++ b/.chloggen/spanmetricsprocessor_support_set_namespace.yaml @@ -8,7 +8,7 @@ component: spanmetricsprocessor note: Support set metrics namespace # One or more tracking issues related to the change -issues: [] +issues: [18199] # (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.