Skip to content

Commit 83a9736

Browse files
committed
[processor/spanmetricsprocessor] Allow set metrics namespace
1 parent 3771eee commit 83a9736

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

processor/spanmetricsprocessor/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ type Config struct {
7272

7373
// MetricsEmitInterval is the time period between when metrics are flushed or emitted to the configured MetricsExporter.
7474
MetricsFlushInterval time.Duration `mapstructure:"metrics_flush_interval"`
75+
76+
// Namespace
77+
Namespace string
7578
}
7679

7780
// GetAggregationTemporality converts the string value given in the config into a AggregationTemporality.

processor/spanmetricsprocessor/processor.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const (
4646
metricKeySeparator = string(byte(0))
4747

4848
defaultDimensionsCacheSize = 1000
49+
50+
metricLatency = "latency"
51+
metricCallsTotal = "calls_total"
4952
)
5053

5154
var defaultLatencyHistogramBucketsMs = []float64{
@@ -317,11 +320,21 @@ func (p *processorImp) buildMetrics() pmetric.Metrics {
317320
return m
318321
}
319322

323+
// buildMetricName builds a metric name by concatenating the namespace and the metric name with an underscore.
324+
// If the namespace is not empty, the namespace and metric name will be separated by an underscore.
325+
// Otherwise, only the metric name will be returned.
326+
func buildMetricName(namespace, metricName string) string {
327+
if namespace != "" {
328+
return namespace + "_" + metricName
329+
}
330+
return metricName
331+
}
332+
320333
// collectLatencyMetrics collects the raw latency metrics, writing the data
321334
// into the given instrumentation library metrics.
322335
func (p *processorImp) collectLatencyMetrics(ilm pmetric.ScopeMetrics) {
323336
mLatency := ilm.Metrics().AppendEmpty()
324-
mLatency.SetName("latency")
337+
mLatency.SetName(buildMetricName(p.config.Namespace, metricLatency))
325338
mLatency.SetUnit("ms")
326339
mLatency.SetEmptyHistogram().SetAggregationTemporality(p.config.GetAggregationTemporality())
327340
dps := mLatency.Histogram().DataPoints()
@@ -349,7 +362,7 @@ func (p *processorImp) collectLatencyMetrics(ilm pmetric.ScopeMetrics) {
349362
// into the given instrumentation library metrics.
350363
func (p *processorImp) collectCallMetrics(ilm pmetric.ScopeMetrics) {
351364
mCalls := ilm.Metrics().AppendEmpty()
352-
mCalls.SetName("calls_total")
365+
mCalls.SetName(buildMetricName(p.config.Namespace, metricCallsTotal))
353366
mCalls.SetEmptySum().SetIsMonotonic(true)
354367
mCalls.Sum().SetAggregationTemporality(p.config.GetAggregationTemporality())
355368
dps := mCalls.Sum().DataPoints()

processor/spanmetricsprocessor/processor_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,3 +1101,22 @@ func TestConsumeTracesEvictedCacheKey(t *testing.T) {
11011101
wg.Wait()
11021102
assert.Empty(t, wantDataPointCounts)
11031103
}
1104+
1105+
func TestBuildMetricName(t *testing.T) {
1106+
tests := []struct {
1107+
namespace string
1108+
metricName string
1109+
expected string
1110+
}{
1111+
{"", "metric", "metric"},
1112+
{"ns", "metric", "ns_metric"},
1113+
{"longer_namespace", "metric", "longer_namespace_metric"},
1114+
}
1115+
1116+
for _, test := range tests {
1117+
actual := buildMetricName(test.namespace, test.metricName)
1118+
if actual != test.expected {
1119+
t.Errorf("buildMetricName(%q, %q) = %q, expected %q", test.namespace, test.metricName, actual, test.expected)
1120+
}
1121+
}
1122+
}

0 commit comments

Comments
 (0)