Skip to content

fix(sdk): detect character pattern wildcards in View instrument_name (#5664) - #5673

Open
somuai wants to merge 6 commits into
open-telemetry:mainfrom
somuai:fix-view-wildcard-character-patterns
Open

somuai wants to merge 6 commits into
open-telemetry:mainfrom
somuai:fix-view-wildcard-character-patterns

Conversation

@somuai

@somuai somuai commented Sep 18, 2026

Copy link
Copy Markdown

Description

Fixes #5664

According to the OpenTelemetry Metrics Specification for Views (https://opentelemetry.io/docs/specs/otel/metrics/sdk/#view):

"If name is provided, the View MUST match at most one instrument. If instrument_name contains wildcard characters (*, ?, [seq], [!seq]), name MUST NOT be provided."

Previously, View.__init__ checked for wildcard characters in instrument_name only by inspecting whether "*" in instrument_name or "?" in instrument_name. However, instrument matching in View._match is executed via fnmatch.fnmatchcase(), which supports character sequence wildcards such as [seq] and [!seq] (for example, instrument_name="http_[0-9]"). Because brackets were not checked, specifying a character set wildcard alongside a custom name bypassed the validation check, permitting multiple instruments to match a single renamed View stream in violation of the specification constraint.

This change updates View.__init__ to detect character pattern wildcards ("[", "]") in instrument_name in addition to "*" and "?", raising an exception whenever name is provided with any wildcard pattern.

Changes

  • In opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py:
    • Update View.__init__ wildcard check to verify any(c in instrument_name for c in "*?[]").
  • In opentelemetry-sdk/tests/metrics/test_view.py:
    • Expand test_view_name with subtests covering *, ?, [0-9], [!a-z], [abc], [, and ], verifying they raise an exception when name is provided.
    • Verify that a valid non-wildcard instrument_name succeeds with name.
  • Add .changelog/5664.fixed.

Verification

  • Ran unit tests locally:
    PYTHONPATH=opentelemetry-api/src:opentelemetry-sdk/src:opentelemetry-semantic-conventions/src:tests/opentelemetry-test-utils/src pytest opentelemetry-sdk/tests/metrics/test_view.py opentelemetry-sdk/tests/metrics/test_view_instrument_match.py -v
    Result: 27 passed, 8 subtests passed (100% green).
  • Formatted with black (zero formatting errors).
  • All commits include Signed-off-by: Soumyajit Ghosh <jobsoumyajit6124@gmail.com>.

…pen-telemetry#5664)

The OpenTelemetry Metrics Specification specifies that if name is provided,
the View MUST match at most one instrument, and if instrument_name contains
wildcard characters (*, ?, [seq], [!seq]), name MUST NOT be provided.

Previously, View.__init__ only checked for '*' and '?' in instrument_name,
allowing character sequence wildcards such as '[' and ']' (e.g. 'http_[0-9]')
to bypass validation when name is set.

Update View.__init__ to detect character pattern wildcards ('[', ']') in
addition to '*' and '?', and add test coverage for character sequence wildcards.

Signed-off-by: Soumyajit Ghosh <jobsoumyajit6124@gmail.com>
@somuai
somuai requested a review from a team as a code owner September 18, 2026 15:55
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 18, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-09-21 20:34 UTC

Review the latest changes.

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.

@somuai

somuai commented Sep 19, 2026

Copy link
Copy Markdown
Author

/dashboard route:reviewers

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

@somuai, this pull request was routed to reviewers. The handoff remains active across pushes until newer actionable human feedback arrives. Top-level feedback through this request will not return; unresolved review threads remain open.

@herin049 herin049 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.

There appears to be some unrelated formatting changes being applied here.

Signed-off-by: Soumyajit Ghosh <jobsoumyajit6124@gmail.com>
Signed-off-by: Soumyajit Ghosh <jobsoumyajit6124@gmail.com>
@somuai

somuai commented Sep 21, 2026

Copy link
Copy Markdown
Author

Thanks for pointing that out @herin049. Reverted the unintentional formatting changes on the constructor signature in commit 7b37ad2. The diff is now strictly limited to the wildcard character check and corresponding unit tests.

/dashboard route:reviewers

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: No status

Development

Successfully merging this pull request may close these issues.

[bug] View initialization fails to detect character pattern wildcards ('[', ']') in instrument_name when name is set

3 participants