Skip to content

fix(sdk): exact-match the View unit selection criterion - #5816

Open
RichardoMrMu wants to merge 5 commits into
open-telemetry:mainfrom
RichardoMrMu:fix/view-unit-exact-match
Open

RichardoMrMu wants to merge 5 commits into
open-telemetry:mainfrom
RichardoMrMu:fix/view-unit-exact-match

Conversation

@RichardoMrMu

Copy link
Copy Markdown
Contributor

Description

A View selected by instrument_unit matched the unit through fnmatchcase, copied from the instrument-name path. The Metrics SDK specification defines unit as an exact, case-sensitive criterion with no wildcard matching (only instrument name MAY match */?). UCUM annotated units use square brackets - e.g. [degF], [degC], [in_i] - and fnmatchcase read those as character classes, so View(instrument_unit="[degF]") never matched an instrument whose unit is [degF]; */? in a unit pattern also behaved as wildcards against the spec.

Compare the unit literally in View._match (case-sensitive, as required by UCUM). Instrument-name matching is unchanged.

Type of change

  • Bug fix

Verification

  • Added test_instrument_unit_ucum_annotated_units_match_exactly and test_instrument_unit_wildcard_characters_are_literal; both fail on main ([degF] does not match; request_* matches request_count) and pass with the fix.
  • opentelemetry-sdk/tests/metrics: 339 passed, 11 skipped, 0 failed.
  • ruff check and ruff format --check clean on both files.

Does this PR require a contrib change?

  • No.

The Metrics SDK specification defines unit selection as an exact,
case-sensitive comparison and gives no wildcard semantics for units.
Matching instrument_unit with fnmatchcase therefore misread UCUM
annotated units such as "[degF]" as character classes, so a View for
that unit never matched. Compare the unit literally and cover the
bracketed units and wildcard characters with regression tests.

Assisted-by: Doubao
@RichardoMrMu
RichardoMrMu requested a review from a team as a code owner October 23, 2026 14:53
Assisted-by: Doubao
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on maintainers · refreshed 2026-11-02 12:03 UTC

Merge when ready.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@knowprasad knowprasad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch on the fnmatchcase issue with UCUM brackets. Makes total sense since the metrics spec only specifies wildcard matching on instrument names anyway, so exact string comparison for units is much cleaner.

Pulled the branch and verified the new unit tests pass locally alongside the rest of the metrics suite.

LGTM!

@github-project-automation github-project-automation Bot moved this to Approved PRs in Python PR digest Oct 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change to exact, case-sensitive View unit matching.

No open issues 🎉

What changed in this PR

Fixes Metrics SDK View selection to match instrument units exactly and case-sensitively, as required by the specification. Instrument-name matching remains unchanged.

Changes:

  • Replaces wildcard-based unit matching with literal equality.
  • Adds regression tests for bracketed UCUM units and literal wildcard characters.
  • Documents the fix in a changelog entry.
File Description
opentelemetry-sdk/​tests/​metrics/​test_view.py Adds regression coverage for exact unit matching.
opentelemetry-sdk/​src/​opentelemetry/​sdk/​metrics/​_internal/​view.py Uses literal equality for unit selection.
.changelog/​5816.fixed Records the corrected View unit-matching behavior.

Review effort: Balanced

# across platforms.
if not fnmatchcase(instrument.unit, self._instrument_unit):
# The unit selection criterion is an exact, case-sensitive match:
# the Metrics SDK specification defines no wildcard matching for

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no point in even referencing fnmatch here, we should shorten this to something like: "Unit comparison is performed in an exact case-sensitive manner".

@github-project-automation github-project-automation Bot moved this from Approved PRs to Reviewed PRs that need fixes in Python PR digest Nov 2, 2026
Address review feedback: drop the fnmatch reference and condense the
four-line comment to a single sentence stating unit comparison is an
exact, case-sensitive match. No behavior change.

Assisted-by: Doubao

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

5 participants