Skip to content
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

Prometheus exporter: handle colliding metric attribute keys #5717

Merged
merged 6 commits into from
Sep 20, 2023

Conversation

dashpole
Copy link
Contributor

From https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/compatibility/prometheus_and_openmetrics.md#metric-attributes

OpenTelemetry Metric Attributes MUST be converted to Prometheus labels. String Attribute values are converted directly to Metric Attributes, and non-string Attribute values MUST be converted to string attributes following the attribute specification. Prometheus metric label keys are required to match the following regex: a-zA-Z_*. Metrics from OpenTelemetry with unsupported Attribute names MUST replace invalid characters with the _ character. Multiple consecutive _ characters MUST be replaced with a single _ character. This may cause ambiguity in scenarios where multiple similar-named attributes share invalid characters at the same location. In such unlikely cases, if multiple key-value pairs are converted to have the same Prometheus key, the values MUST be concatenated together, separated by ;, and ordered by the lexicographical order of the original keys.

This implements the last part of the spec, where values for colliding attributes are concatenated.

Question: are attributes sorted before they are passed to the prometheus exporter?

@dashpole dashpole force-pushed the prom_metadata_deduplication branch 6 times, most recently from 5670ae3 to 1dca626 Compare August 16, 2023 20:57
@codecov
Copy link

codecov bot commented Aug 16, 2023

Codecov Report

Patch coverage is 100.00% of modified lines.

Files Changed Coverage
.../opentelemetry/exporter/prometheus/Serializer.java 100.00%

📢 Thoughts on this report? Let us know!.

@dashpole dashpole marked this pull request as ready for review August 17, 2023 13:04
@dashpole dashpole requested a review from a team as a code owner August 17, 2023 13:04
@mateuszrzeszutek
Copy link
Member

Question: are attributes sorted before they are passed to the prometheus exporter?

Yes:

/**
* Sorts and dedupes the key/value pairs in {@code data}. {@code null} values will be removed.
* Keys will be compared with the given {@link Comparator}.
*/
protected ImmutableKeyValuePairs(Object[] data, Comparator<?> keyComparator) {
this(sortAndFilter(data, keyComparator));
}

Attributes are always sorted by the keys' string values

@jack-berg
Copy link
Member

Question: are attributes sorted before they are passed to the prometheus exporter?

Yes:

Technically that's an implementation detail of a specific Attributes implementation. Its possible to have other Attributes implementations, like the AttributesMap version we use to apply limits as attributes are recorded in the trace and log signals.

@dashpole dashpole force-pushed the prom_metadata_deduplication branch 2 times, most recently from e777f06 to 4e285dd Compare August 22, 2023 19:53
@dashpole dashpole force-pushed the prom_metadata_deduplication branch 3 times, most recently from 993a8ad to 92fe87f Compare September 1, 2023 19:59
@dashpole
Copy link
Contributor Author

@jack-berg is there anything else needed here?

@dashpole
Copy link
Contributor Author

Thanks @jack-berg! Your additional changes LGTM

@jack-berg jack-berg merged commit 9a93155 into open-telemetry:main Sep 20, 2023
18 checks passed
@dashpole dashpole deleted the prom_metadata_deduplication branch September 20, 2023 20:38
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.

None yet

3 participants