diff --git a/CHANGELOG.md b/CHANGELOG.md index d795eb83155..ad876d36bf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2044](https://github.com/open-telemetry/opentelemetry-python/pull/2044)) - `opentelemetry-sdk` Add support for `OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT` env var ([#2056](https://github.com/open-telemetry/opentelemetry-python/pull/2056)) + ## [0.23.1](https://github.com/open-telemetry/opentelemetry-python/pull/1987) - 2021-07-26 ### Changed diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 8f7b035f218..7acffecd85f 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -593,6 +593,10 @@ def __init__( OTEL_LINK_ATTRIBUTE_COUNT_LIMIT, _DEFAULT_OTEL_LINK_ATTRIBUTE_COUNT_LIMIT, ) + self.max_attribute_length = self._from_env_if_absent( + max_attribute_length, + OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT, + ) self.max_attribute_length = self._from_env_if_absent( max_attribute_length, diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 271af682e10..866eb6e5e60 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -1354,6 +1354,7 @@ def test_limits_attribute_length_limits_code(self): self.assertEqual(limits.max_attribute_length, 22) self.assertEqual(limits.max_span_attribute_length, 33) + def test_limits_values_code(self): max_attributes, max_events, max_links, max_attr_length = ( randint(0, 10000), @@ -1394,6 +1395,102 @@ def test_limits_values_env(self): self.assertEqual(limits.max_links, max_links) + def _test_span_limits( + self, tracer, max_attrs, max_events, max_links, max_attr_len + ): + id_generator = RandomIdGenerator() + some_links = [ + trace_api.Link( + trace_api.SpanContext( + trace_id=id_generator.generate_trace_id(), + span_id=id_generator.generate_span_id(), + is_remote=False, + ), + attributes={"k": self.long_val}, + ) + for _ in range(100) + ] + + some_attrs = { + "init_attribute_{}".format(idx): self.long_val + for idx in range(100) + } + with tracer.start_as_current_span( + "root", links=some_links, attributes=some_attrs + ) as root: + self.assertEqual(len(root.links), max_links) + self.assertEqual(len(root.attributes), max_attrs) + for idx in range(100): + root.set_attribute( + "my_str_attribute_{}".format(idx), self.long_val + ) + root.set_attribute( + "my_byte_attribute_{}".format(idx), self.long_val.encode() + ) + root.set_attribute( + "my_int_attribute_{}".format(idx), self.long_val.encode() + ) + root.add_event( + "my_event_{}".format(idx), attributes={"k": self.long_val} + ) + + self.assertEqual(len(root.attributes), max_attrs) + self.assertEqual(len(root.events), max_events) + + for link in root.links: + for attr_val in link.attributes.values(): + self._assert_attr_length(attr_val, max_attr_len) + + for event in root.events: + for attr_val in event.attributes.values(): + self._assert_attr_length(attr_val, max_attr_len) + + for attr_val in root.attributes.values(): + self._assert_attr_length(attr_val, max_attr_len) + + def _test_span_no_limits(self, tracer): + num_links = int(trace._DEFAULT_OTEL_SPAN_LINK_COUNT_LIMIT) + randint( + 1, 100 + ) + + id_generator = RandomIdGenerator() + some_links = [ + trace_api.Link( + trace_api.SpanContext( + trace_id=id_generator.generate_trace_id(), + span_id=id_generator.generate_span_id(), + is_remote=False, + ) + ) + for _ in range(num_links) + ] + with tracer.start_as_current_span("root", links=some_links) as root: + self.assertEqual(len(root.links), num_links) + + num_events = int(trace._DEFAULT_OTEL_SPAN_EVENT_COUNT_LIMIT) + randint( + 1, 100 + ) + with tracer.start_as_current_span("root") as root: + for idx in range(num_events): + root.add_event( + "my_event_{}".format(idx), attributes={"k": self.long_val} + ) + + self.assertEqual(len(root.events), num_events) + + num_attributes = int( + trace._DEFAULT_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT + ) + randint(1, 100) + with tracer.start_as_current_span("root") as root: + for idx in range(num_attributes): + root.set_attribute( + "my_attribute_{}".format(idx), self.long_val + ) + + self.assertEqual(len(root.attributes), num_attributes) + for attr_val in root.attributes.values(): + self.assertEqual(attr_val, self.long_val) + @mock.patch.dict( "os.environ", { @@ -1422,6 +1519,7 @@ def test_span_limits_env(self): OTEL_SPAN_LINK_COUNT_LIMIT: "30", OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT: "40", OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT: "50", + OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT: "40", }, ) def test_span_limits_default_to_env(self):