Skip to content

Commit

Permalink
Fix unit and name regexes (#2796)
Browse files Browse the repository at this point in the history
* Fix unit and name regexes

Fixes #2793

* Add changelog entry
  • Loading branch information
ocelotl authored Jul 4, 2022
1 parent 0590617 commit e27f2b8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD)

- Fix instrument name and unit regexes
([#2796](https://github.com/open-telemetry/opentelemetry-python/pull/2796))
- Add optional sessions parameter to all Exporters leveraging requests.Session
([#2783](https://github.com/open-telemetry/opentelemetry-python/pull/2783))
([#2783](https://github.com/open-telemetry/opentelemetry-python/pull/2783))
- Add min/max fields to Histogram
([#2759](https://github.com/open-telemetry/opentelemetry-python/pull/2759))
- `opentelemetry-exporter-otlp-proto-http` Add support for OTLP/HTTP log exporter
Expand Down
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 e27f2b8

Please sign in to comment.