diff --git a/.changelog/5284.fixed b/.changelog/5284.fixed new file mode 100644 index 0000000000..715e07246d --- /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 48a56f62ff..cc885f40a8 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py @@ -46,7 +46,12 @@ _logger = getLogger(__name__) -_ERROR_MESSAGE = "Expected ASCII string of maximum length 63 characters but got {}" +_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 {}" +) @runtime_checkable @@ -73,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"] @@ -112,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 b6fd8dfe88..aa180a5d88 100644 --- a/opentelemetry-sdk/tests/metrics/test_instrument.py +++ b/opentelemetry-sdk/tests/metrics/test_instrument.py @@ -559,3 +559,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))