Skip to content

Metric units should be treated as opaque strings #2793

@evan-bradley

Description

@evan-bradley

Describe your environment
OpenTelemetry Python API and SDK versions 1.12.0rc1 running under Python 3.10.

Steps to reproduce
Create an instrument with a unit containing characters that are not alphanumeric.

from opentelemetry import metrics

meter = metrics.get_meter("server_meter")

meter.create_counter(
    name="request_count",
    description="Count the number of handled requests.",
    unit="{requests}",
)

What is the expected behavior?
The unit validation should only check that the unit has fewer than 63 ASCII characters.

What is the actual behavior?
Units are restricted to the following regex character set: [a-zA-Z0-9_].

An exception is thrown with the following message:

Expected ASCII string of maximum length 63 characters but got {requests}

Additional context
The unit validation regex being used: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py#L43

The API spec advises that units should be treated as opaque strings and only the length should be validated: https://opentelemetry.io/docs/reference/specification/metrics/api/#instrument-unit

Python defines \w as [a-zA-Z0-9_] when the ASCII flag is used: https://docs.python.org/3/library/re.html#regular-expression-syntax

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingmetrics

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions