From d2274654b00b4cc9301a69b275c890a175b1a595 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Wed, 10 Jul 2024 12:30:10 -0700 Subject: [PATCH] config: add support for with_resource_constant_labels option 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> --- config/metric.go | 19 ++++++++++++++++--- config/metric_test.go | 11 +++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) 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"}, + }, }, }, },