Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[processor/spanmetricsprocessor] Allow set metrics namespace #18199

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions processor/spanmetricsprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 15 additions & 2 deletions processor/spanmetricsprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const (
metricKeySeparator = string(byte(0))

defaultDimensionsCacheSize = 1000

metricLatency = "latency"
metricCallsTotal = "calls_total"
)

var defaultLatencyHistogramBucketsMs = []float64{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions processor/spanmetricsprocessor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}