From 96fa8af0c23d7f20224e67ffa4c34e68e0b24199 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Fri, 15 Sep 2023 14:57:50 +0000 Subject: [PATCH] Allow instrument names to have '/' and up to 255 characters Fixes #3431 --- CHANGELOG.md | 2 ++ .../metrics/_internal/instrument.py | 2 +- .../tests/metrics/test_instruments.py | 22 ++++++++++++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5a7a983987..2c2f63b4b2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3335](https://github.com/open-telemetry/opentelemetry-python/pull/3335)) - Fix error when no LoggerProvider configured for LoggingHandler ([#3423](https://github.com/open-telemetry/opentelemetry-python/pull/3423)) +- Allow instrument names to have '/' and up to 255 characters + ([#3442](https://github.com/open-telemetry/opentelemetry-python/pull/3442)) ## Version 1.20.0/0.41b0 (2023-09-04) diff --git a/opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py b/opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py index fec2879ef6c..54b2fb7597e 100644 --- a/opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py +++ b/opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py @@ -38,7 +38,7 @@ _logger = getLogger(__name__) -_name_regex = re_compile(r"[a-zA-Z][-_.a-zA-Z0-9]{0,62}") +_name_regex = re_compile(r"[a-zA-Z][-_./a-zA-Z0-9]{0,254}") _unit_regex = re_compile(r"[\x00-\x7F]{0,63}") diff --git a/opentelemetry-api/tests/metrics/test_instruments.py b/opentelemetry-api/tests/metrics/test_instruments.py index 4a3d3d448b3..2e6082d873e 100644 --- a/opentelemetry-api/tests/metrics/test_instruments.py +++ b/opentelemetry-api/tests/metrics/test_instruments.py @@ -564,14 +564,13 @@ def test_observable_up_down_counter_callback(self): ) def test_name_check(self): - instrument = ChildInstrument("name") self.assertEqual( instrument._check_name_unit_description( - "a" * 63, "unit", "description" + "a" * 255, "unit", "description" )["name"], - "a" * 63, + "a" * 255, ) self.assertEqual( instrument._check_name_unit_description( @@ -591,12 +590,25 @@ def test_name_check(self): )["name"], "a_", ) + self.assertEqual( + instrument._check_name_unit_description( + "a/", "unit", "description" + )["name"], + "a/", + ) - self.assertIsNone( + # the old max length + self.assertIsNotNone( instrument._check_name_unit_description( "a" * 64, "unit", "description" )["name"] ) + self.assertIsNone( + instrument._check_name_unit_description( + "a" * 256, "unit", "description" + )["name"] + ) + self.assertIsNone( instrument._check_name_unit_description( "Ñ", "unit", "description" @@ -619,7 +631,6 @@ def test_name_check(self): ) def test_unit_check(self): - instrument = ChildInstrument("name") self.assertEqual( @@ -653,7 +664,6 @@ def test_unit_check(self): ) def test_description_check(self): - instrument = ChildInstrument("name") self.assertEqual(