From e9fb9077c574f79faed64ef1e0452037df71c67c Mon Sep 17 00:00:00 2001 From: Loki San Date: Thu, 6 Aug 2026 19:02:04 +0530 Subject: [PATCH] fix(sdk): correct instrument name validation error message length Signed-off-by: Loki San --- .changelog/5284.fixed | 1 + .../sdk/metrics/_internal/instrument.py | 13 ++++++++----- opentelemetry-sdk/tests/metrics/test_instrument.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 .changelog/5284.fixed diff --git a/.changelog/5284.fixed b/.changelog/5284.fixed new file mode 100644 index 00000000000..715e07246d0 --- /dev/null +++ b/.changelog/5284.fixed @@ -0,0 +1 @@ +Fix misleading instrument name validation error message (name max length is 255, not 63). diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py index 548aa8ee1f3..e25345426cb 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py @@ -46,7 +46,10 @@ _logger = getLogger(__name__) -_ERROR_MESSAGE = ( +_NAME_ERROR_MESSAGE = ( + "Expected ASCII string of maximum length 255 characters but got {}" +) +_UNIT_ERROR_MESSAGE = ( "Expected ASCII string of maximum length 63 characters but got {}" ) @@ -75,11 +78,11 @@ def __init__( if result["name"] is None: # pylint: disable=broad-exception-raised - raise Exception(_ERROR_MESSAGE.format(name)) + raise Exception(_NAME_ERROR_MESSAGE.format(name)) if result["unit"] is None: # pylint: disable=broad-exception-raised - raise Exception(_ERROR_MESSAGE.format(unit)) + raise Exception(_UNIT_ERROR_MESSAGE.format(unit)) name = result["name"] unit = result["unit"] @@ -114,11 +117,11 @@ def __init__( if result["name"] is None: # pylint: disable=broad-exception-raised - raise Exception(_ERROR_MESSAGE.format(name)) + raise Exception(_NAME_ERROR_MESSAGE.format(name)) if result["unit"] is None: # pylint: disable=broad-exception-raised - raise Exception(_ERROR_MESSAGE.format(unit)) + raise Exception(_UNIT_ERROR_MESSAGE.format(unit)) name = result["name"] unit = result["unit"] diff --git a/opentelemetry-sdk/tests/metrics/test_instrument.py b/opentelemetry-sdk/tests/metrics/test_instrument.py index d8fd813e0a7..ff4beedc552 100644 --- a/opentelemetry-sdk/tests/metrics/test_instrument.py +++ b/opentelemetry-sdk/tests/metrics/test_instrument.py @@ -577,3 +577,17 @@ def test_disallow_direct_histogram_creation(self): with self.assertRaises(TypeError): # pylint: disable=abstract-class-instantiated Histogram("name", Mock(), Mock()) + + +class TestInstrumentValidationMessages(TestCase): + def test_invalid_name_error_message(self): + with self.assertRaises(Exception) as ctx: + _Counter("1-invalid-name", Mock(), Mock()) + self.assertIn("maximum length 255", str(ctx.exception)) + self.assertNotIn("63", str(ctx.exception)) + + def test_invalid_unit_error_message(self): + with self.assertRaises(Exception) as ctx: + _Counter("name", Mock(), Mock(), unit="u" * 64) + self.assertIn("maximum length 63", str(ctx.exception)) + self.assertNotIn("255", str(ctx.exception))