Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/5284.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix misleading instrument name validation error message (name max length is 255, not 63).
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down
14 changes: 14 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))