Skip to content

Commit

Permalink
Merge branch 'main' into fix-2051
Browse files Browse the repository at this point in the history
  • Loading branch information
owais authored Aug 19, 2021
2 parents 6dd996a + 2a726e4 commit 4d32d1f
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
98 changes: 98 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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",
{
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4d32d1f

Please sign in to comment.