-
Notifications
You must be signed in to change notification settings - Fork 717
Fix prometheus metric name and unit conversion #3924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0335dd8
Fix prometheus metric name and unit conversion
aabmass 07aff62
Apply suggestions from code review
aabmass 7c729b6
Make annotation parsing more permissive, add test case for consecutiv…
aabmass dad9948
Add test case for metric name already containing the unit
aabmass a5844da
simplify and speed up regex and update TODO
aabmass 78ffe92
Add OTEL_PYTHON_EXPERIMENTAL_DISABLE_PROMETHEUS_UNIT_NORMALIZATION op…
aabmass d35f4b8
Fix RST typo
aabmass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/_mapping.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from re import UNICODE, compile | ||
|
||
_SANITIZE_NAME_RE = compile(r"[^a-zA-Z0-9:]+", UNICODE) | ||
# Same as name, but doesn't allow ":" | ||
_SANITIZE_ATTRIBUTE_KEY_RE = compile(r"[^a-zA-Z0-9]+", UNICODE) | ||
|
||
# UCUM style annotations which are text enclosed in curly braces https://ucum.org/ucum#para-6. | ||
# This regex is more permissive than UCUM allows and matches any character within curly braces. | ||
_UNIT_ANNOTATION = compile(r"{.*}") | ||
|
||
# Remaps common UCUM and SI units to prometheus conventions. Copied from | ||
# https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.101.0/pkg/translator/prometheus/normalize_name.go#L19 | ||
# See specification: | ||
# https://github.com/open-telemetry/opentelemetry-specification/blob/v1.33.0/specification/compatibility/prometheus_and_openmetrics.md#metric-metadata-1 | ||
_UNIT_MAPPINGS = { | ||
# Time | ||
"d": "days", | ||
"h": "hours", | ||
"min": "minutes", | ||
"s": "seconds", | ||
"ms": "milliseconds", | ||
"us": "microseconds", | ||
"ns": "nanoseconds", | ||
# Bytes | ||
"By": "bytes", | ||
"KiBy": "kibibytes", | ||
"MiBy": "mebibytes", | ||
"GiBy": "gibibytes", | ||
"TiBy": "tibibytes", | ||
"KBy": "kilobytes", | ||
"MBy": "megabytes", | ||
"GBy": "gigabytes", | ||
"TBy": "terabytes", | ||
# SI | ||
ocelotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"m": "meters", | ||
"V": "volts", | ||
"A": "amperes", | ||
"J": "joules", | ||
"W": "watts", | ||
"g": "grams", | ||
# Misc | ||
"Cel": "celsius", | ||
ocelotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Hz": "hertz", | ||
# TODO(https://github.com/open-telemetry/opentelemetry-specification/issues/4058): the | ||
# specification says to normalize "1" to ratio but that may change. Update this mapping or | ||
# remove TODO once a decision is made. | ||
"1": "", | ||
"%": "percent", | ||
} | ||
# Similar to _UNIT_MAPPINGS, but for "per" unit denominator. | ||
# Example: s => per second (singular) | ||
# Copied from https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/80317ce83ed87a2dff0c316bb939afbfaa823d5e/pkg/translator/prometheus/normalize_name.go#L58 | ||
_PER_UNIT_MAPPINGS = { | ||
ocelotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"s": "second", | ||
"m": "minute", | ||
"h": "hour", | ||
"d": "day", | ||
"w": "week", | ||
"mo": "month", | ||
"y": "year", | ||
} | ||
|
||
|
||
def sanitize_full_name(name: str) -> str: | ||
"""sanitize the given metric name according to Prometheus rule, including sanitizing | ||
leading digits | ||
|
||
https://github.com/open-telemetry/opentelemetry-specification/blob/v1.33.0/specification/compatibility/prometheus_and_openmetrics.md#metric-metadata-1 | ||
""" | ||
# Leading number special case | ||
if name and name[0].isdigit(): | ||
aabmass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name = "_" + name[1:] | ||
return _sanitize_name(name) | ||
|
||
|
||
def _sanitize_name(name: str) -> str: | ||
"""sanitize the given metric name according to Prometheus rule, but does not handle | ||
sanitizing a leading digit.""" | ||
return _SANITIZE_NAME_RE.sub("_", name) | ||
ocelotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def sanitize_attribute(key: str) -> str: | ||
"""sanitize the given metric attribute key according to Prometheus rule. | ||
|
||
https://github.com/open-telemetry/opentelemetry-specification/blob/v1.33.0/specification/compatibility/prometheus_and_openmetrics.md#metric-attributes | ||
""" | ||
# Leading number special case | ||
if key and key[0].isdigit(): | ||
key = "_" + key[1:] | ||
return _SANITIZE_ATTRIBUTE_KEY_RE.sub("_", key) | ||
lzchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def map_unit(unit: str) -> str: | ||
ocelotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Maps unit to common prometheus metric names if available and sanitizes any invalid | ||
characters | ||
|
||
See: | ||
- https://github.com/open-telemetry/opentelemetry-specification/blob/v1.33.0/specification/compatibility/prometheus_and_openmetrics.md#metric-metadata-1 | ||
- https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.101.0/pkg/translator/prometheus/normalize_name.go#L108 | ||
""" | ||
# remove curly brace unit annotations | ||
unit = _UNIT_ANNOTATION.sub("", unit) | ||
|
||
if unit in _UNIT_MAPPINGS: | ||
return _UNIT_MAPPINGS[unit] | ||
|
||
# replace "/" with "per" units like m/s -> meters_per_second | ||
ratio_unit_subparts = unit.split("/", maxsplit=1) | ||
if len(ratio_unit_subparts) == 2: | ||
bottom = _sanitize_name(ratio_unit_subparts[1]) | ||
if bottom: | ||
top = _sanitize_name(ratio_unit_subparts[0]) | ||
top = _UNIT_MAPPINGS.get(top, top) | ||
bottom = _PER_UNIT_MAPPINGS.get(bottom, bottom) | ||
return f"{top}_per_{bottom}" if top else f"per_{bottom}" | ||
|
||
return ( | ||
# since units end up as a metric name suffix, they must be sanitized | ||
_sanitize_name(unit) | ||
# strip surrounding "_" chars since it will lead to consecutive underscores in the | ||
# metric name | ||
.strip("_") | ||
lzchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.