diff --git a/.chloggen/spanmetricsprocessor_support_set_namespace.yaml b/.chloggen/spanmetricsprocessor_support_set_namespace.yaml new file mode 100644 index 000000000000..20bc8652a922 --- /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: [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. +# Use pipe (|) for multiline entries. +subtext: + diff --git a/processor/spanmetricsprocessor/config.go b/processor/spanmetricsprocessor/config.go index b46800bb02dd..5bbc20e0dd32 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 defines the namespace for the metrics exported by the spanmetricsprocessor. + 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..1050655d0dbe 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, metricLatency)) 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..be7910620e1b 100644 --- a/processor/spanmetricsprocessor/processor_test.go +++ b/processor/spanmetricsprocessor/processor_test.go @@ -1101,3 +1101,20 @@ 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) + assert.Equal(t, test.expected, actual) + } +}