Skip to content

Commit

Permalink
config: add support for with_resource_constant_labels option
Browse files Browse the repository at this point in the history
This provides users the ability to apply labels for resource attributes to prometheus exported metrics.

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
codeboten committed Jul 10, 2024
1 parent d2d9b4c commit d227465
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
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 d227465

Please sign in to comment.