Fix operator precedence allowing exemplars on any metric type#1188
Open
sean-kim05 wants to merge 1 commit into
Open
Fix operator precedence allowing exemplars on any metric type#1188sean-kim05 wants to merge 1 commit into
sean-kim05 wants to merge 1 commit into
Conversation
_is_valid_exemplar_metric() guarded histograms with
if metric.type in ('histogram') and sample.name.endswith('_bucket') or sample.name == metric.name:
Because 'and' binds tighter than 'or', this parsed as
(metric.type in ('histogram') and sample.name.endswith('_bucket')) or (sample.name == metric.name)
so the trailing 'sample.name == metric.name' clause fired for every
metric type. As a result the OpenMetrics writer emitted exemplars on
gauges, info, stateset, summary and untyped metrics whenever a sample
name equalled the metric name -- output that the library's own
OpenMetrics parser rejects (only histogram/gaugehistogram buckets,
counter _total, and native histograms may carry exemplars).
Group the histogram condition correctly so the same-name clause (which
exists to allow native-histogram exemplars) only applies to histograms.
While here, replace the 'metric.type in (...)' single-string membership
checks with '==' -- they were doing substring matching, not the
intended equality.
Add test_gauge_exemplar, which asserts a gauge sample carrying an
exemplar raises ValueError, matching the existing untyped/non-bucket
exemplar tests.
Signed-off-by: Sean Kim <skim8705@gmail.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
_is_valid_exemplar_metric()inopenmetrics/exposition.pyguards histograms with:Since
andbinds tighter thanor, this is evaluated as:so the trailing
sample.name == metric.nameclause runs for every metric type, not just histograms. As a result the OpenMetrics writer attaches exemplars to gauge/info/stateset/summary/untyped metrics whenever a sample name equals the metric name — output that this library's own OpenMetrics parser then rejects (only histogram/gaugehistogram buckets and counters can have exemplars).Minimal repro:
The fix groups the histogram condition so the same-name clause — which exists to allow native-histogram exemplars, whose sample name equals the metric name — only applies to histograms. I also changed the two
metric.type in ('...')checks to==; with a single string argument they were doing substring matching rather than the intended equality.Added
test_gauge_exemplarnext to the existing untyped/non-bucket exemplar tests. It fails before the change (ValueError not raised) and passes after. Full test suite is green and flake8/isort/mypy pass.@csmarchbanks