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
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):
In
opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py,View.__init__checks for wildcard characters when a customnameis 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 customname(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