diff --git a/.changelog/5513.fixed b/.changelog/5513.fixed new file mode 100644 index 0000000000..6af842a7e8 --- /dev/null +++ b/.changelog/5513.fixed @@ -0,0 +1 @@ +`opentelemetry-sdk`: 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..c3ee9c47ae 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py @@ -46,7 +46,8 @@ _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 +74,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 +113,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))