open-telemetry / open-telemetry/opentelemetry-python
[bug] View initialization fails to detect character pattern wildcards ('[', ']') in instrument_name when name is set
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.6k
- Forks
- 1k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 19
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Begin in opentelemetry-sdk/src/opentelemetry/sdk/metrics/internal/view.py at View.init, and compare its wildcard validation with fnmatch.fnmatchcase(), which performs matching later in the file. Add coverage for bracket patterns alongside the existing '*' and '?' cases, then verify that a custom name with instrument_name="http[0-9]" is rejected during initialization.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- observability-sre
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 90/100