diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d22a22c1dd..6554e8f74c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add macOS ARM64 platform to the compatibility testing suite. (#5868) - The `go.opentelemetry.io/contrib/bridges/otelzap` module. This module provides an OpenTelemetry logging bridge for `go.uber.org/zap`. (#5191) +- The `go.opentelemetry.io/contrib/config` package supports configuring `with_resource_constant_labels` for the prometheus exporter. (#5890) ### Removed diff --git a/config/metric.go b/config/metric.go index 72f21cf3850..6c6b34e9f59 100644 --- a/config/metric.go +++ b/config/metric.go @@ -230,6 +230,22 @@ func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmet if prometheusConfig.WithoutUnits != nil && *prometheusConfig.WithoutUnits { opts = append(opts, otelprom.WithoutUnits()) } + if prometheusConfig.WithResourceConstantLabels != nil { + if prometheusConfig.WithResourceConstantLabels.Included != nil { + var keys []attribute.Key + for _, val := range prometheusConfig.WithResourceConstantLabels.Included { + keys = append(keys, attribute.Key(val)) + } + otelprom.WithResourceAsConstantLabels(attribute.NewAllowKeysFilter(keys...)) + } + if prometheusConfig.WithResourceConstantLabels.Excluded != nil { + var keys []attribute.Key + for _, val := range prometheusConfig.WithResourceConstantLabels.Included { + keys = append(keys, attribute.Key(val)) + } + otelprom.WithResourceAsConstantLabels(attribute.NewDenyKeysFilter(keys...)) + } + } reg := prometheus.NewRegistry() opts = append(opts, otelprom.WithRegisterer(reg)) @@ -246,9 +262,6 @@ func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmet } addr := fmt.Sprintf("%s:%d", *prometheusConfig.Host, *prometheusConfig.Port) - // TODO: add support for constant label filter - // otelprom.WithResourceAsConstantLabels(attribute.NewDenyKeysFilter()), - // ) reader, err := otelprom.New(opts...) if err != nil { return nil, fmt.Errorf("error creating otel prometheus exporter: %w", err) diff --git a/config/metric_test.go b/config/metric_test.go index 83aa7a8beb0..34ead1fddb9 100644 --- a/config/metric_test.go +++ b/config/metric_test.go @@ -148,8 +148,15 @@ func TestReader(t *testing.T) { Pull: &PullMetricReader{ Exporter: MetricExporter{ Prometheus: &Prometheus{ - Host: ptr("localhost"), - Port: ptr(8888), + Host: ptr("localhost"), + Port: ptr(8888), + WithoutScopeInfo: ptr(true), + WithoutUnits: ptr(true), + WithoutTypeSuffix: ptr(true), + WithResourceConstantLabels: &IncludeExclude{ + Included: []string{"include"}, + Excluded: []string{"exclude"}, + }, }, }, },