diff --git a/.changelog/5647.fixed b/.changelog/5647.fixed new file mode 100644 index 00000000000..2e44244b339 --- /dev/null +++ b/.changelog/5647.fixed @@ -0,0 +1 @@ +`opentelemetry-api`: Added guard for negative value on max_value_len \ No newline at end of file diff --git a/opentelemetry-api/src/opentelemetry/attributes/__init__.py b/opentelemetry-api/src/opentelemetry/attributes/__init__.py index 2ba2d393200..6bc3590f0f1 100644 --- a/opentelemetry-api/src/opentelemetry/attributes/__init__.py +++ b/opentelemetry-api/src/opentelemetry/attributes/__init__.py @@ -146,6 +146,8 @@ def __init__( ) -> None: if maxlen is not None and maxlen < 0: raise ValueError("maxlen must be valid int greater or equal to 0") + if max_value_len is not None and max_value_len < 0: + raise ValueError("max_value_len must be valid int greater or equal to 0") self._dict: dict[str, types.AnyValue] = {} self.maxlen = maxlen self.dropped = 0 diff --git a/opentelemetry-api/tests/attributes/test_attributes.py b/opentelemetry-api/tests/attributes/test_attributes.py index 29e9826481d..2d9c8cb9c29 100644 --- a/opentelemetry-api/tests/attributes/test_attributes.py +++ b/opentelemetry-api/tests/attributes/test_attributes.py @@ -116,6 +116,10 @@ def test_negative_maxlen_not_allowed(self): with self.assertRaises(ValueError): BoundedAttributes(-1) + def test_negative_max_value_len_not_allowed(self): + with self.assertRaises(ValueError): + BoundedAttributes(1, {"first": "value"}, immutable=False, max_value_len=-1) + def test_base_copy_isolated_and_len_works(self): dic_len = len(self.base) base_copy = self.base.copy()