Skip to content

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

Closed

Description

@Dotify71

Describe your environment

OS: macOS / Linux
Python version: 3.10 / 3.11 / 3.12
SDK version: main
API version: main

What happened?

According to the OpenTelemetry Metrics Specification - View (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."

In opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py, View.__init__ checks for wildcard characters when a custom name is provided:

if name is not None and instrument_name is not None and ("*" in instrument_name or "?" in instrument_name):
raise Exception(f"View {name} declared with wildcard characters in instrument_name")

However, instrument matching is performed using fnmatch.fnmatchcase() (line 153), which supports character sequence wildcards such as [seq] and [!seq] (e.g. http_[0-9]).

Because View.__init__ only checks for * and ?, passing a wildcard pattern containing [ or ] (e.g. instrument_name="http_[0-9]") alongside a custom name (e.g. name="custom_name") fails to raise an Exception. This allows a single renamed View stream to match multiple instruments, violating the specification constraint.

Steps to Reproduce

from opentelemetry.sdk.metrics.view import View

Should raise Exception because 'http_[0-9]' contains fnmatch wildcard '[', but currently succeeds:

v = View(name="custom_name", instrument_name="http_[0-9]")

Expected Result

View(name="custom_name", instrument_name="http_[0-9]") should raise an Exception during initialization, matching the behavior when '*' or '?' is used.

Actual Result

View initializes without error and matches multiple instruments (e.g., http_1, http_2), renaming all of them to custom_name.

Additional context

Suggested fix in opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py:

if name is not None and instrument_name is not None and any(c in instrument_name for c in "*?["):
raise Exception(f"View {name} declared with wildcard characters in instrument_name")

Would you like to implement a fix?

Yes

Activity

  1. axiom-of-choice commented on Sep 18, 2026

    @axiom-of-choice

    If it's still free, i can take it.
    I checked it against main: View(name="custom_name", instrument_name="http_[0-9]") initialises without error, and fnmatchcase then matches both http_1 and http_2, so a named view can match more than one instrument, which is what the spec forbids. The guard at view.py:122 only looks for * and ?. I would extend it to reject [ as well, since fnmatch treats *, ? and [ as the metacharacters, and add a regression test next to test_view_name.
    Does that direction work, and are you still planning to open the PR yourself?

  2. Dotify71 commented on Sep 18, 2026

    @Dotify71
    ContributorAuthor

    Hey @somuai @axiom-of-choice! Thanks for looking into this. Since I had already prepared the fix when filing the issue, I've opened PR #5674 for review. Appreciate it!

  3. somuai commented on Sep 19, 2026

    @somuai

    Thanks @Dotify71! I had opened PR #5673 earlier with bracket wildcard validation and test coverage across *, ?, [0-9], and [!a-z]. Both PRs address the issue cleanly, so happy for maintainers to review and pick whichever approach they prefer to land!

  4. Dotify71 commented on Sep 19, 2026

    @Dotify71
    ContributorAuthor

    Sounds great, thanks @somuai for contributing! Happy to let the maintainers review both PRs.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions