From ff078389768454f39a740391df680f78df14f481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Tue, 5 Nov 2024 14:24:52 +0100 Subject: [PATCH] prometheus: Add instrumentation scope attributes to otel_scope_info (#5932) Towards https://github.com/open-telemetry/opentelemetry-go/issues/5846 --- CHANGELOG.md | 1 + exporters/prometheus/exporter.go | 10 ++++++++-- exporters/prometheus/exporter_test.go | 1 + exporters/prometheus/testdata/counter.txt | 2 +- .../prometheus/testdata/counter_disabled_suffix.txt | 2 +- exporters/prometheus/testdata/counter_utf8.txt | 2 +- exporters/prometheus/testdata/custom_resource.txt | 2 +- exporters/prometheus/testdata/empty_resource.txt | 2 +- exporters/prometheus/testdata/gauge.txt | 2 +- exporters/prometheus/testdata/histogram.txt | 2 +- .../non_monotonic_sum_does_not_add_exemplars.txt | 2 +- exporters/prometheus/testdata/sanitized_labels.txt | 2 +- exporters/prometheus/testdata/sanitized_names.txt | 2 +- .../testdata/with_allow_resource_attributes_filter.txt | 2 +- exporters/prometheus/testdata/with_namespace.txt | 2 +- .../testdata/with_resource_attributes_filter.txt | 2 +- exporters/prometheus/testdata/without_target_info.txt | 2 +- 17 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b9589f3053..d3636e22d58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp` adds instrumentation scope attributes. (#5935) - `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc` adds instrumentation scope attributes. (#5933) - `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp` adds instrumentation scope attributes. (#5933) +- `go.opentelemetry.io/otel/exporters/prometheus` adds instrumentation scope attributes in `otel_scope_info` metric as labels. (#5932) ### Fixed diff --git a/exporters/prometheus/exporter.go b/exporters/prometheus/exporter.go index f6ebf6b760d..3cd457a03c3 100644 --- a/exporters/prometheus/exporter.go +++ b/exporters/prometheus/exporter.go @@ -356,8 +356,14 @@ func createInfoMetric(name, description string, res *resource.Resource) (prometh } func createScopeInfoMetric(scope instrumentation.Scope) (prometheus.Metric, error) { - desc := prometheus.NewDesc(scopeInfoMetricName, scopeInfoDescription, []string{scopeNameLabel, scopeVersionLabel}, nil) - return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), scope.Name, scope.Version) + attrs := make([]attribute.KeyValue, 0, scope.Attributes.Len()+2) // resource attrs + scope name + scope version + attrs = append(attrs, scope.Attributes.ToSlice()...) + attrs = append(attrs, attribute.String(scopeNameLabel, scope.Name)) + attrs = append(attrs, attribute.String(scopeVersionLabel, scope.Version)) + + keys, values := getAttrs(attribute.NewSet(attrs...)) + desc := prometheus.NewDesc(scopeInfoMetricName, scopeInfoDescription, keys, nil) + return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), values...) } var unitSuffixes = map[string]string{ diff --git a/exporters/prometheus/exporter_test.go b/exporters/prometheus/exporter_test.go index ad3444a9920..5592071d95b 100644 --- a/exporters/prometheus/exporter_test.go +++ b/exporters/prometheus/exporter_test.go @@ -513,6 +513,7 @@ func TestPrometheusExporter(t *testing.T) { meter := provider.Meter( "testmeter", otelmetric.WithInstrumentationVersion("v0.1.0"), + otelmetric.WithInstrumentationAttributes(attribute.String("fizz", "buzz")), ) tc.recordMetrics(ctx, meter) diff --git a/exporters/prometheus/testdata/counter.txt b/exporters/prometheus/testdata/counter.txt index 4eb32991a64..443c45adcbd 100755 --- a/exporters/prometheus/testdata/counter.txt +++ b/exporters/prometheus/testdata/counter.txt @@ -4,7 +4,7 @@ foo_seconds_total{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_s foo_seconds_total{A="D",C="B",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 5 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/counter_disabled_suffix.txt b/exporters/prometheus/testdata/counter_disabled_suffix.txt index ca6f8967dfb..f9fb68ddbff 100755 --- a/exporters/prometheus/testdata/counter_disabled_suffix.txt +++ b/exporters/prometheus/testdata/counter_disabled_suffix.txt @@ -4,7 +4,7 @@ foo_seconds{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_v foo_seconds{A="D",C="B",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 5 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/counter_utf8.txt b/exporters/prometheus/testdata/counter_utf8.txt index 7587eaffbc9..a3acdcc3bd1 100755 --- a/exporters/prometheus/testdata/counter_utf8.txt +++ b/exporters/prometheus/testdata/counter_utf8.txt @@ -4,7 +4,7 @@ {"foo.things_seconds_total","A.G"="D","C.H"="B","E.I"="true","F.J"="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 5 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{"service.name"="prometheus_test","telemetry.sdk.language"="go","telemetry.sdk.name"="opentelemetry","telemetry.sdk.version"="latest"} 1 diff --git a/exporters/prometheus/testdata/custom_resource.txt b/exporters/prometheus/testdata/custom_resource.txt index 9b2a19ad480..2e5aeceaccb 100755 --- a/exporters/prometheus/testdata/custom_resource.txt +++ b/exporters/prometheus/testdata/custom_resource.txt @@ -3,7 +3,7 @@ foo_total{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 24.3 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{A="B",C="D",service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/empty_resource.txt b/exporters/prometheus/testdata/empty_resource.txt index e313006e34b..5dbcfb3b8a4 100755 --- a/exporters/prometheus/testdata/empty_resource.txt +++ b/exporters/prometheus/testdata/empty_resource.txt @@ -3,7 +3,7 @@ foo_total{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 24.3 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info 1 diff --git a/exporters/prometheus/testdata/gauge.txt b/exporters/prometheus/testdata/gauge.txt index 33d2b218b3f..db321d4c9fb 100644 --- a/exporters/prometheus/testdata/gauge.txt +++ b/exporters/prometheus/testdata/gauge.txt @@ -3,7 +3,7 @@ bar_ratio{A="B",C="D",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} .75 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/histogram.txt b/exporters/prometheus/testdata/histogram.txt index f8016f38583..6701786acbd 100644 --- a/exporters/prometheus/testdata/histogram.txt +++ b/exporters/prometheus/testdata/histogram.txt @@ -15,7 +15,7 @@ histogram_baz_bytes_sum{A="B",C="D",otel_scope_name="testmeter",otel_scope_versi histogram_baz_bytes_count{A="B",C="D",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 4 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/non_monotonic_sum_does_not_add_exemplars.txt b/exporters/prometheus/testdata/non_monotonic_sum_does_not_add_exemplars.txt index 7eb6cec40e2..feda8f30f46 100644 --- a/exporters/prometheus/testdata/non_monotonic_sum_does_not_add_exemplars.txt +++ b/exporters/prometheus/testdata/non_monotonic_sum_does_not_add_exemplars.txt @@ -4,7 +4,7 @@ foo_seconds{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_v foo_seconds{A="D",C="B",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 5 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/sanitized_labels.txt b/exporters/prometheus/testdata/sanitized_labels.txt index 06eee59354e..38d755cb50c 100755 --- a/exporters/prometheus/testdata/sanitized_labels.txt +++ b/exporters/prometheus/testdata/sanitized_labels.txt @@ -3,7 +3,7 @@ foo_total{A_B="Q",C_D="Y;Z",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 24.3 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/sanitized_names.txt b/exporters/prometheus/testdata/sanitized_names.txt index 18773800cf6..4667a41cbbc 100644 --- a/exporters/prometheus/testdata/sanitized_names.txt +++ b/exporters/prometheus/testdata/sanitized_names.txt @@ -29,7 +29,7 @@ invalid_hist_name_sum{A="B",C="D",otel_scope_name="testmeter",otel_scope_version invalid_hist_name_count{A="B",C="D",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/with_allow_resource_attributes_filter.txt b/exporters/prometheus/testdata/with_allow_resource_attributes_filter.txt index 9a4ca7793ea..a25b65cf166 100755 --- a/exporters/prometheus/testdata/with_allow_resource_attributes_filter.txt +++ b/exporters/prometheus/testdata/with_allow_resource_attributes_filter.txt @@ -3,7 +3,7 @@ foo_total{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0",service_name="prometheus_test"} 16.2 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/with_namespace.txt b/exporters/prometheus/testdata/with_namespace.txt index b41329afe95..6d6b0369f2b 100755 --- a/exporters/prometheus/testdata/with_namespace.txt +++ b/exporters/prometheus/testdata/with_namespace.txt @@ -3,7 +3,7 @@ test_foo_total{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 24.3 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/with_resource_attributes_filter.txt b/exporters/prometheus/testdata/with_resource_attributes_filter.txt index b3ab4f80fab..60bc4d00e91 100755 --- a/exporters/prometheus/testdata/with_resource_attributes_filter.txt +++ b/exporters/prometheus/testdata/with_resource_attributes_filter.txt @@ -3,7 +3,7 @@ foo_total{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0",service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 24.9 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1 diff --git a/exporters/prometheus/testdata/without_target_info.txt b/exporters/prometheus/testdata/without_target_info.txt index 69f0e836688..eafb7e20424 100755 --- a/exporters/prometheus/testdata/without_target_info.txt +++ b/exporters/prometheus/testdata/without_target_info.txt @@ -3,4 +3,4 @@ foo_total{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 24.3 # HELP otel_scope_info Instrumentation Scope metadata # TYPE otel_scope_info gauge -otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1 +otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1