Skip to content

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

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

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions