Skip to content

Commit

Permalink
[BREAKING CHANGE] datadogexporter: Remove otel. prefix from most metr…
Browse files Browse the repository at this point in the history
…ics (#2308)

- Metrics produced by the hostmetricsreceiver keep the prefix
- Running metrics keep the prefix

Users can use the metrics transform processor to keep the old behavior
  • Loading branch information
mx-psi authored Feb 9, 2021
1 parent 27dbdd3 commit f296b17
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
24 changes: 21 additions & 3 deletions exporter/datadogexporter/metrics/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package metrics

import (
"fmt"
"strings"

"go.uber.org/zap"
"gopkg.in/zorkian/go-datadog-api.v2"
Expand Down Expand Up @@ -88,10 +89,27 @@ func addHostname(metrics []datadog.Metric, logger *zap.Logger, cfg *config.Confi
}
}

// addNamespace prepends all metric names with a given namespace
// shouldPrepend decides if a given metric name should be prepended by `otel.`.
// By default, this happens for
// - hostmetrics receiver metrics (since they clash with Datadog Agent system check) and
// - running metrics
func shouldPrepend(name string) bool {
namespaces := [...]string{"datadog_exporter.", "system.", "process."}
for _, ns := range namespaces {
if strings.HasPrefix(name, ns) {
return true
}
}
return false
}

// addNamespace prepends some metric names with a given namespace.
// This is used to namespace metrics that clash with the Datadog Agent
func addNamespace(metrics []datadog.Metric, namespace string) {
for i := range metrics {
newName := namespace + "." + *metrics[i].Metric
metrics[i].Metric = &newName
if shouldPrepend(*metrics[i].Metric) {
newName := namespace + "." + *metrics[i].Metric
metrics[i].Metric = &newName
}
}
}
31 changes: 27 additions & 4 deletions exporter/datadogexporter/metrics/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,29 @@ func TestProcessMetrics(t *testing.T) {
0,
[]string{"key2:val2"},
),
NewGauge(
"system.cpu.time",
0,
0,
[]string{"key3:val3"},
),
}

ProcessMetrics(ms, logger, cfg)

assert.Equal(t, "test-host", *ms[0].Host)
assert.Equal(t, "otel.metric_name", *ms[0].Metric)
assert.Equal(t, "metric_name", *ms[0].Metric)
assert.ElementsMatch(t,
[]string{"key2:val2"},
ms[0].Tags,
)

assert.Equal(t, "test-host", *ms[1].Host)
assert.Equal(t, "otel.system.cpu.time", *ms[1].Metric)
assert.ElementsMatch(t,
[]string{"key3:val3"},
ms[1].Tags,
)
}

func TestAddHostname(t *testing.T) {
Expand Down Expand Up @@ -165,14 +177,25 @@ func TestAddHostname(t *testing.T) {
assert.Equal(t, "thathost", *ms[0].Host)
}

func TestShouldPrepend(t *testing.T) {
assert.True(t, shouldPrepend("system.memory.usage"))
assert.True(t, shouldPrepend("datadog_exporter.traces.running"))
assert.True(t, shouldPrepend("process.cpu.time"))
assert.False(t, shouldPrepend("processes.cpu.time"))
assert.False(t, shouldPrepend("systemd.metric.name"))
assert.False(t, shouldPrepend("random.metric.name"))
}

func TestAddNamespace(t *testing.T) {
ms := []datadog.Metric{
NewGauge("test.metric", 0, 1.0, []string{}),
NewGauge("test.metric2", 0, 2.0, []string{}),
NewGauge("system.cpu.time", 0, 2.0, []string{}),
NewGauge("process.memory.physical_usage", 0, 3.0, []string{}),
}

addNamespace(ms, "namespace")

assert.Equal(t, "namespace.test.metric", *ms[0].Metric)
assert.Equal(t, "namespace.test.metric2", *ms[1].Metric)
assert.Equal(t, "test.metric", *ms[0].Metric)
assert.Equal(t, "namespace.system.cpu.time", *ms[1].Metric)
assert.Equal(t, "namespace.process.memory.physical_usage", *ms[2].Metric)
}

0 comments on commit f296b17

Please sign in to comment.