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

Allow user to pass in metrics namespace #5758

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

- `ocb` now exits with an error if it fails to load the build configuration. (#5731)
- Deprecate `HTTPClientSettings.ToClientWithHost` (#5737)
- Add a new field `service.telemetry.metrics.namespace` to allow users to customize the collector's metric namespace (#5692)

### 🧰 Bug fixes 🧰

Expand Down
20 changes: 16 additions & 4 deletions service/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ func ownMetricsTestCases(version string) []ownMetricsTestCase {
}

func testCollectorStartHelper(t *testing.T, telemetry *telemetryInitializer, tc ownMetricsTestCase) {
testCollectorStartHelperWithConfig(t, telemetry, tc, "otelcol", "otelcol-nop.yaml")
}

func testCollectorStartHelperWithConfig(t *testing.T, telemetry *telemetryInitializer, tc ownMetricsTestCase, namespace, configFile string) {
factories, err := componenttest.NopFactories()
zpagesExt := zpagesextension.NewFactory()
factories.Extensions[zpagesExt.Type()] = zpagesExt
Expand Down Expand Up @@ -294,7 +298,7 @@ func testCollectorStartHelper(t *testing.T, telemetry *telemetryInitializer, tc
extraCfgAsProps["service::telemetry::resource::"+k] = v
}

cfgSet := newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")})
cfgSet := newDefaultConfigProviderSettings([]string{filepath.Join("testdata", configFile)})
cfgSet.MapConverters = append([]confmap.Converter{
mapConverter{extraCfgAsProps}},
cfgSet.MapConverters...,
Expand All @@ -318,7 +322,7 @@ func testCollectorStartHelper(t *testing.T, telemetry *telemetryInitializer, tc
}, 2*time.Second, 200*time.Millisecond)
assert.True(t, loggingHookCalled)

assertMetrics(t, metricsAddr, tc.expectedLabels)
assertMetrics(t, metricsAddr, tc.expectedLabels, namespace)

assertZPages(t)
col.signalsChannel <- syscall.SIGTERM
Expand All @@ -335,6 +339,14 @@ func TestCollectorStartWithOpenCensusMetrics(t *testing.T) {
}
}

func TestCollectorStartWithMetricNamespace(t *testing.T) {
for _, tc := range ownMetricsTestCases("test version") {
t.Run(tc.name, func(t *testing.T) {
testCollectorStartHelperWithConfig(t, newColTelemetry(featuregate.NewRegistry()), tc, "customNamespace", "otelcol-metricnamespace.yaml")
})
}
}

func TestCollectorStartWithOpenTelemetryMetrics(t *testing.T) {
for _, tc := range ownMetricsTestCases("test version") {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -431,7 +443,7 @@ func TestCollectorClosedStateOnStartUpError(t *testing.T) {
assert.Equal(t, Closed, col.GetState())
}

func assertMetrics(t *testing.T, metricsAddr string, expectedLabels map[string]labelValue) {
func assertMetrics(t *testing.T, metricsAddr string, expectedLabels map[string]labelValue, namespace string) {
client := &http.Client{}
resp, err := client.Get("http://" + metricsAddr + "/metrics")
require.NoError(t, err)
Expand All @@ -445,7 +457,7 @@ func assertMetrics(t *testing.T, metricsAddr string, expectedLabels map[string]l
parsed, err := parser.TextToMetricFamilies(reader)
require.NoError(t, err)

prefix := "otelcol"
prefix := namespace
for metricName, metricFamily := range parsed {
// require is used here so test fails with a single message.
require.True(
Expand Down
7 changes: 6 additions & 1 deletion service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
// useOtelForInternalMetricsfeatureGateID is the feature gate ID that controls whether the collector uses open
// telemetrySettings for internal metrics.
useOtelForInternalMetricsfeatureGateID = "telemetry.useOtelForInternalMetrics"
defaultNameSpace = "otelcol"
)

type telemetryInitializer struct {
Expand Down Expand Up @@ -173,8 +174,12 @@ func (tel *telemetryInitializer) initOpenCensus(cfg telemetry.Config, telAttrs m
}

// Until we can use a generic metrics exporter, default to Prometheus.
namespace := defaultNameSpace
if cfg.Metrics.Namespace != nil {
namespace = *cfg.Metrics.Namespace
}
opts := prometheus.Options{
Namespace: "otelcol",
Namespace: namespace,
}

opts.ConstLabels = make(map[string]string)
Expand Down
7 changes: 7 additions & 0 deletions service/telemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,11 @@ type MetricsConfig struct {

// Address is the [address]:port that metrics exposition should be bound to.
Address string `mapstructure:"address"`

// Namespace indicates the telemetry namespace the service will set. If nil, a default value 'otelcol' is assigned.
// An example of how the value of Namespace would affect metrics name:
// - if Namespace not set: "otelcol_process_cpu_seconds"
// - if Namespace set to empty string: "process_cpu_seconds"
// - if Namespace set to other string(e.g: "custom_namespace"): "custom_namespace_process_cpu_seconds"
Namespace *string `mapstructure:"namespace"`
}
30 changes: 30 additions & 0 deletions service/testdata/otelcol-metricnamespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
receivers:
nop:

processors:
nop:

exporters:
nop:

extensions:
nop:

service:
telemetry:
metrics:
namespace: customNamespace
extensions: [nop]
pipelines:
traces:
receivers: [nop]
processors: [nop]
exporters: [nop]
metrics:
receivers: [nop]
processors: [nop]
exporters: [nop]
logs:
receivers: [nop]
processors: [nop]
exporters: [nop]