Skip to content

Migrate prometheus exporter to otlptranslator#7044

Merged
dashpole merged 9 commits intoopen-telemetry:mainfrom
dashpole:prom_otlptranslator
Jul 21, 2025
Merged

Migrate prometheus exporter to otlptranslator#7044
dashpole merged 9 commits intoopen-telemetry:mainfrom
dashpole:prom_otlptranslator

Conversation

@dashpole
Copy link
Copy Markdown
Contributor

@dashpole dashpole commented Jul 18, 2025

Fixes #7039
Fixes #6704

This uses the common prometheus/otlptranslator library to handle name conversion. It was a little tricky to work around the fact that the library only lets us configure whether all suffixes are added or not. But we want to keep supporting WithoutUnit and WithoutCounterSuffixes for a while longer. Those will eventually be deprecated and replaced after open-telemetry/opentelemetry-specification#4533 is released.

We decided to go ahead with the changes despite it being a small behavioral change when UTF8 is enabled. See: prometheus/otlptranslator#44 for the rationale.

This adds a unit test to verify that it properly handles bracketed units. The test fails on main with:

--- FAIL: TestPrometheusExporter (0.01s)
    --- FAIL: TestPrometheusExporter/counter_with_bracketed_unit (0.00s)
        exporter_test.go:646:
            	Error Trace:	/usr/local/google/home/dashpole/go/src/go.opentelemetry.io/opentelemetry-go/exporters/prometheus/exporter_test.go:646
            	Error:      	Received unexpected error:
            	            	-# HELP "foo_{spans}_total" a simple counter
            	            	-# TYPE "foo_{spans}_total" counter
            	            	-{"foo_{spans}_total",A="B",C="D",E="true",F="42",otel_scope_fizz="buzz",otel_scope_name="testmeter",otel_scope_schema_url="",otel_scope_version="v0.1.0"} 24.3
            	            	-{"foo_{spans}_total",A="D",C="B",E="true",F="42",otel_scope_fizz="buzz",otel_scope_name="testmeter",otel_scope_schema_url="",otel_scope_version="v0.1.0"} 5
            	            	+# HELP foo_total a simple counter
            	            	+# TYPE foo_total counter
            	            	+foo_total{A="B",C="D",E="true",F="42",otel_scope_fizz="buzz",otel_scope_name="testmeter",otel_scope_schema_url="",otel_scope_version="v0.1.0"} 24.3
            	            	+foo_total{A="D",C="B",E="true",F="42",otel_scope_fizz="buzz",otel_scope_name="testmeter",otel_scope_schema_url="",otel_scope_version="v0.1.0"} 5
            	            	 # 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

            	Test:       	TestPrometheusExporter/counter_with_bracketed_unit
2025/07/18 15:07:47 internal_logging.go:50: "msg"="Using existing type definition." "error"="instrument type conflict" "instrument"="foo_total" "existing"="COUNTER" "dropped"="GAUGE"
2025/07/18 15:07:47 internal_logging.go:50: "msg"="Using existing type definition." "error"="instrument type conflict" "instrument"="foo_bytes" "existing"="GAUGE" "dropped"="HISTOGRAM"
FAIL
FAIL	go.opentelemetry.io/otel/exporters/prometheus	0.054s
FAIL

cc @TylerHelmuth @ywwg @ArthurSens

@ArthurSens
Copy link
Copy Markdown
Member

ArthurSens commented Jul 18, 2025

TODO:

When creating new metrics with OTel-Go-SDK, I don't expect people to add unit/type suffixes manually. So, to increase performance, we decided not to deduplicate suffixes in this code path.

Did I have the wrong expectations? If people prefer Prometheus' ways of doing things, I expected they would use Prometheus SDK instead of OTel's.

@dashpole
Copy link
Copy Markdown
Contributor Author

We can tell people who are relying on the suffix deduplication to disable suffixes and add them themselves if they want. I'll remove those test cases

@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 18, 2025

Codecov Report

Attention: Patch coverage is 95.12195% with 4 lines in your changes missing coverage. Please review.

Project coverage is 82.8%. Comparing base (f6b5fb9) to head (500fe37).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
exporters/prometheus/exporter.go 95.1% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #7044   +/-   ##
=====================================
  Coverage   82.8%   82.8%           
=====================================
  Files        261     261           
  Lines      24361   24371   +10     
=====================================
+ Hits       20173   20201   +28     
+ Misses      3813    3795   -18     
  Partials     375     375           
Files with missing lines Coverage Δ
exporters/prometheus/config.go 100.0% <ø> (+7.2%) ⬆️
exporters/prometheus/exporter.go 85.7% <95.1%> (+3.3%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dashpole dashpole force-pushed the prom_otlptranslator branch from a467273 to 019acff Compare July 18, 2025 15:22
@dashpole dashpole marked this pull request as ready for review July 18, 2025 15:23
@dashpole
Copy link
Copy Markdown
Contributor Author

Thanks everyone! This is ready for review.

@ArthurSens
Copy link
Copy Markdown
Member

It would be nice to test this in the collector. I expect that the instrumented metrics don't have unit/type suffixes in their names (which would cause that duplicated suffix problem), but I haven't checked yet

Comment thread CHANGELOG.md Outdated
Comment thread exporters/prometheus/testdata/counter.txt Outdated
dashpole and others added 2 commits July 18, 2025 13:18
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
@pellared
Copy link
Copy Markdown
Member

pellared commented Jul 21, 2025

@dashpole, should this PR be blocked by:

  1. Retract most recent prometheus exporter release #7046,
  2. AND a "patch release" of the Prometheus exporter module?

@dashpole
Copy link
Copy Markdown
Contributor Author

@pellared do you think it is OK to include this PR in a patch release? Otherwise, I can open a revert PR for the offending change, rather than fix it this way

@dashpole
Copy link
Copy Markdown
Contributor Author

I would prefer including this PR in the patch release

@MrAlias
Copy link
Copy Markdown
Contributor

MrAlias commented Jul 21, 2025

+1 for merging this and including in the patch release. If it is not included that patch release itself needs to be retracted and that is not the original plan.

@dashpole dashpole merged commit 8e6e28f into open-telemetry:main Jul 21, 2025
33 checks passed
@dashpole dashpole deleted the prom_otlptranslator branch July 21, 2025 15:58
dashpole added a commit that referenced this pull request Jul 21, 2025
Patch release for
#7039

### Changed

- Retract `v0.59.0` release of
`go.opentelemetry.io/otel/exporters/prometheus` module which appends
incorrect unit suffixes. (#7046)
- Change `go.opentelemetry.io/otel/exporters/prometheus` to no longer
deduplicate suffixes when UTF8 is enabled.
It is recommended to disable unit and counter suffixes in the exporter,
and manually add suffixes if you rely on the existing behavior. (#7044)

### Fixed

- Fix `go.opentelemetry.io/otel/exporters/prometheus` to properly handle
unit suffixes when the unit is in brackets.
  E.g. `{spans}`. (#7044)
bogdandrutu pushed a commit to open-telemetry/opentelemetry-collector that referenced this pull request Jul 21, 2025
Reverts #13429 so that we can bump
to the to-be-released bugfix version that adds
open-telemetry/opentelemetry-go/pull/7044
@dfawley
Copy link
Copy Markdown

dfawley commented Jul 31, 2025

@dashpole - Is this the same process that is followed for other languages when exporting to prometheus? Since this was a behavior change, and it broke grpc-go, I'm wondering if it will impact grpc-java/c++/etc, or if they are already doing something to avoid the translation.

@dashpole
Copy link
Copy Markdown
Contributor Author

This won't impact other languages @dfawley

@dfawley
Copy link
Copy Markdown

dfawley commented Jul 31, 2025

Sorry if it wasn't clear, but my question is whether this is the same behavior that other languages have. But also it seems like we were using the library incorrectly by not including braces around our non-standard units, and if we had done so we would not have been broken. So perhaps the question is moot.

@dashpole
Copy link
Copy Markdown
Contributor Author

dashpole commented Aug 1, 2025

It should be the same behavior across languages. If you find that it isn't, let me know and i'll make sure it gets fixed. Sorry for any breakage this caused!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prometheus exporter incorrectly adding units to metric name Migrate prometheus exporter to use prom/otlptranslator for name translation

7 participants