You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix prometheus metric name and unit conversion (#3924)
* Fix prometheus metric name and unit conversion
* Apply suggestions from code review
Co-authored-by: Diego Hurtado <[email protected]>
* Make annotation parsing more permissive, add test case for consecutive underscores
* Add test case for metric name already containing the unit
* simplify and speed up regex and update TODO
* Add OTEL_PYTHON_EXPERIMENTAL_DISABLE_PROMETHEUS_UNIT_NORMALIZATION opt-out mechanism
* Fix RST typo
---------
Co-authored-by: Diego Hurtado <[email protected]>
- common unit abbreviations are converted to Prometheus conventions (`s` -> `seconds`),
52
+
following the [collector's implementation](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/c0b51136575aa7ba89326d18edb4549e7e1bbdb9/pkg/translator/prometheus/normalize_name.go#L108)
53
+
- repeated `_` are replaced with a single `_`
54
+
- unit annotations (enclosed in curly braces like `{requests}`) are stripped away
55
+
- units with slash are converted e.g. `m/s` -> `meters_per_second`.
# TODO(https://github.com/open-telemetry/opentelemetry-specification/issues/4058): the
59
+
# specification says to normalize "1" to ratio but that may change. Update this mapping or
60
+
# remove TODO once a decision is made.
61
+
"1": "",
62
+
"%": "percent",
63
+
}
64
+
# Similar to _UNIT_MAPPINGS, but for "per" unit denominator.
65
+
# Example: s => per second (singular)
66
+
# Copied from https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/80317ce83ed87a2dff0c316bb939afbfaa823d5e/pkg/translator/prometheus/normalize_name.go#L58
67
+
_PER_UNIT_MAPPINGS= {
68
+
"s": "second",
69
+
"m": "minute",
70
+
"h": "hour",
71
+
"d": "day",
72
+
"w": "week",
73
+
"mo": "month",
74
+
"y": "year",
75
+
}
76
+
77
+
78
+
defsanitize_full_name(name: str) ->str:
79
+
"""sanitize the given metric name according to Prometheus rule, including sanitizing
0 commit comments