Skip to content

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

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

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

Closed · refreshed 2026-09-25 15:26 UTC

Status above doesn't look right?
  • 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

@tammy-baylis-swi tammy-baylis-swi moved this to Ready for review in Python PR digest Sep 24, 2026
@herin049

Copy link
Copy Markdown
Contributor

Going to close this since it's a duplicate.

@herin049 herin049 closed this Sep 25, 2026
@somuai

somuai commented Sep 25, 2026

Copy link
Copy Markdown
Author

Hi @herin049 and @lzchen,

Thank you for reviewing and maintaining the repository. I wanted to share a quick point of clarification regarding the timeline and technical scope of this PR relative to #5674.

PR #5673 was submitted on 2026-09-18 at 15:55:19 UTC, approximately 2.5 hours prior to PR #5674 (submitted at 18:18:06 UTC).

In addition, this PR includes a couple of technical details that may be relevant to the review comments left on #5674:

  1. Wildcard validation: This PR checks *?[] (any(c in instrument_name for c in "*?[]")), ensuring both [ and ] are caught. PR fix(sdk): detect character pattern wildcards in View instrument_name (#5664) #5674 currently checks *?[, which does not catch ].
  2. Test coverage: This PR contains 8 parameterized subtests covering *, ?, [0-9], [!a-z], [abc], [, ], and valid non-wildcard names, which addresses the additional test cases requested on fix(sdk): detect character pattern wildcards in View instrument_name (#5664) #5674.
  3. Changelog: The changelog entry here already includes the opentelemetry-sdk prefix and references the character set wildcards.

If you would like to proceed with this PR, I would be happy to have it reopened. Alternatively, if you prefer to keep #5674 since it has already received initial review, please feel free to use the test cases and bracket check from here in that PR. Whichever approach works best for the maintainers works for me.

Thank you again for your time and guidance!

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

Labels

None yet

Projects

Status: Done

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

4 participants