Skip to content

Commit

Permalink
[processor/spanmetricsprocessor] Allow set metrics namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Cluas committed Jan 31, 2023
1 parent 3771eee commit 83a9736
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
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
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
19 changes: 19 additions & 0 deletions processor/spanmetricsprocessor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 83a9736

Please sign in to comment.