Skip to content

Commit

Permalink
config: add support for with_resource_constant_labels option (open-te…
Browse files Browse the repository at this point in the history
…lemetry#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]>
  • Loading branch information
2 people authored and luca-filipponi committed Aug 9, 2024
1 parent c41c024 commit e38b3f5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Added option for extracting attributes from the http request in http transport in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#5876)
- 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

Expand Down
19 changes: 16 additions & 3 deletions config/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions config/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
},
},
Expand Down

0 comments on commit e38b3f5

Please sign in to comment.