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) + } + } +}