Skip to content

Commit

Permalink
Fix unit and name regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jul 1, 2022
1 parent f7a3e62 commit d641a65
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions opentelemetry-api/tests/metrics/test_instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])

0 comments on commit d641a65

Please sign in to comment.