Skip to content

Commit e38b3f5

Browse files
codebotenMrAlias
authored andcommitted
config: add support for with_resource_constant_labels option (open-telemetry#5890)
This provides users the ability to apply labels for resource attributes to prometheus exported metrics. --------- Signed-off-by: Alex Boten <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent c41c024 commit e38b3f5

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1515
- Added option for extracting attributes from the http request in http transport in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#5876)
1616
- The `go.opentelemetry.io/contrib/bridges/otelzap` module.
1717
This module provides an OpenTelemetry logging bridge for `go.uber.org/zap`. (#5191)
18+
- The `go.opentelemetry.io/contrib/config` package supports configuring `with_resource_constant_labels` for the prometheus exporter. (#5890)
1819

1920
### Removed
2021

config/metric.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmet
230230
if prometheusConfig.WithoutUnits != nil && *prometheusConfig.WithoutUnits {
231231
opts = append(opts, otelprom.WithoutUnits())
232232
}
233+
if prometheusConfig.WithResourceConstantLabels != nil {
234+
if prometheusConfig.WithResourceConstantLabels.Included != nil {
235+
var keys []attribute.Key
236+
for _, val := range prometheusConfig.WithResourceConstantLabels.Included {
237+
keys = append(keys, attribute.Key(val))
238+
}
239+
otelprom.WithResourceAsConstantLabels(attribute.NewAllowKeysFilter(keys...))
240+
}
241+
if prometheusConfig.WithResourceConstantLabels.Excluded != nil {
242+
var keys []attribute.Key
243+
for _, val := range prometheusConfig.WithResourceConstantLabels.Included {
244+
keys = append(keys, attribute.Key(val))
245+
}
246+
otelprom.WithResourceAsConstantLabels(attribute.NewDenyKeysFilter(keys...))
247+
}
248+
}
233249

234250
reg := prometheus.NewRegistry()
235251
opts = append(opts, otelprom.WithRegisterer(reg))
@@ -246,9 +262,6 @@ func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmet
246262
}
247263
addr := fmt.Sprintf("%s:%d", *prometheusConfig.Host, *prometheusConfig.Port)
248264

249-
// TODO: add support for constant label filter
250-
// otelprom.WithResourceAsConstantLabels(attribute.NewDenyKeysFilter()),
251-
// )
252265
reader, err := otelprom.New(opts...)
253266
if err != nil {
254267
return nil, fmt.Errorf("error creating otel prometheus exporter: %w", err)

config/metric_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,15 @@ func TestReader(t *testing.T) {
148148
Pull: &PullMetricReader{
149149
Exporter: MetricExporter{
150150
Prometheus: &Prometheus{
151-
Host: ptr("localhost"),
152-
Port: ptr(8888),
151+
Host: ptr("localhost"),
152+
Port: ptr(8888),
153+
WithoutScopeInfo: ptr(true),
154+
WithoutUnits: ptr(true),
155+
WithoutTypeSuffix: ptr(true),
156+
WithResourceConstantLabels: &IncludeExclude{
157+
Included: []string{"include"},
158+
Excluded: []string{"exclude"},
159+
},
153160
},
154161
},
155162
},

0 commit comments

Comments
 (0)