Fix prometheus metric name and unit conversion by aabmass · Pull Request #3924 · open-telemetry/opentelemetry-python

Important

This is a breaking change. This PR changes the output prometheus metric names, but the API is not changed.

Description

Fixes #2938

Updates the prometheus exporter name conversion to match the specification by implementing a few fixes:

  • this is a breaking change to prometheus metric names so they comply with the
    specification.
  • you can temporarily opt-out of the unit normalization by setting the environment variable
    OTEL_PYTHON_EXPERIMENTAL_DISABLE_PROMETHEUS_UNIT_NORMALIZATION=true
  • common unit abbreviations are converted to Prometheus conventions (s -> seconds),
    following the collector's implementation
  • consecutive _ are replaced with a single _
  • unit annotations (enclosed in curly braces like {requests}) are stripped away
  • units with slash are converted e.g. m/s -> meters_per_second.
  • The exporter's API is not changed

Type of change

Please delete options that are not relevant.

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

How Has This Been Tested?

I added a ton of test cases. Many were copied from the collector's test cases. I also added tests for semconv metrics so we can be sure what they will look like.

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated