diff --git a/opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py b/opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py index f98cbd72430..b203b8ffe7c 100644 --- a/opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py +++ b/opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py @@ -18,7 +18,6 @@ from abc import ABC, abstractmethod from dataclasses import dataclass from logging import getLogger -from re import ASCII from re import compile as re_compile from typing import ( Callable, @@ -39,8 +38,8 @@ _logger = getLogger(__name__) -_name_regex = re_compile(r"[a-zA-Z][-.\w]{0,62}", ASCII) -_unit_regex = re_compile(r"\w{0,63}", ASCII) +_name_regex = re_compile(r"[a-zA-Z][-_.a-zA-Z0-9]{0,62}") +_unit_regex = re_compile(r"[\x00-\x7F]{0,63}") @dataclass(frozen=True) diff --git a/opentelemetry-api/tests/metrics/test_instruments.py b/opentelemetry-api/tests/metrics/test_instruments.py index ff2ab2b3e52..3e1e3fe745d 100644 --- a/opentelemetry-api/tests/metrics/test_instruments.py +++ b/opentelemetry-api/tests/metrics/test_instruments.py @@ -571,6 +571,7 @@ def test_name_regex(self): self.assertTrue(instrument._check_name_and_unit("a.", "unit")[0]) self.assertTrue(instrument._check_name_and_unit("a-", "unit")[0]) self.assertTrue(instrument._check_name_and_unit("a_", "unit")[0]) + self.assertFalse(instrument._check_name_and_unit("a" * 64, "unit")[0]) self.assertFalse(instrument._check_name_and_unit("Ñ", "unit")[0]) self.assertFalse(instrument._check_name_and_unit("_a", "unit")[0]) @@ -582,5 +583,7 @@ def test_unit_regex(self): instrument = ChildInstrument("name") self.assertTrue(instrument._check_name_and_unit("name", "a" * 63)[1]) + self.assertTrue(instrument._check_name_and_unit("name", "{a}")[1]) + self.assertFalse(instrument._check_name_and_unit("name", "a" * 64)[1]) self.assertFalse(instrument._check_name_and_unit("name", "Ñ")[1])