Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3973](https://github.com/open-telemetry/opentelemetry-python/pull/3966))
- Update semantic conventions to version 1.26.0.
([#3964](https://github.com/open-telemetry/opentelemetry-python/pull/3964))
- Use semconv exception attributes for record exceptions in spans
- Use semconv exception attributes for record exceptions in spans
([#3979](https://github.com/open-telemetry/opentelemetry-python/pull/3979))
- Validate links at span creation
([#3991](https://github.com/open-telemetry/opentelemetry-python/pull/3991))

## Version 1.25.0/0.46b0 (2024-05-30)

Expand Down
30 changes: 18 additions & 12 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,25 +816,31 @@ def __init__(
)
self._events.append(event)

if links is None:
self._links = self._new_links()
else:
for link in links:
link._attributes = BoundedAttributes(
self._limits.max_link_attributes,
link.attributes,
max_value_len=self._limits.max_attribute_length,
)
self._links = BoundedList.from_seq(self._limits.max_links, links)
self._links = self._new_links(links)

def __repr__(self):
return f'{type(self).__name__}(name="{self._name}", context={self._context})'

def _new_events(self):
return BoundedList(self._limits.max_events)

def _new_links(self):
return BoundedList(self._limits.max_links)
def _new_links(self, links: Optional[Sequence[trace_api.Link]]):
if links is None:
return BoundedList(self._limits.max_links)

_links = list(
filter(
lambda link: _is_valid_link(link.context, link.attributes),
links,
)
)
for link in _links:
link._attributes = BoundedAttributes(
self._limits.max_link_attributes,
link.attributes,
max_value_len=self._limits.max_attribute_length,
)
return BoundedList.from_seq(self._limits.max_links, _links)

def get_span_context(self):
return self._context
Expand Down
16 changes: 16 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,11 @@ def test_add_link_with_invalid_span_context(self):
root.add_link(None)
self.assertEqual(len(root.links), 0)

with self.tracer.start_as_current_span(
"root", links=[trace_api.Link(other_context)]
) as root:
self.assertEqual(len(root.links), 0)

def test_add_link_with_invalid_span_context_with_attributes(self):
invalid_context = trace_api.INVALID_SPAN_CONTEXT

Expand All @@ -962,6 +967,12 @@ def test_add_link_with_invalid_span_context_with_attributes(self):
self.assertEqual(len(root.links), 1)
self.assertEqual(root.links[0].attributes, {"name": "neighbor"})

with self.tracer.start_as_current_span(
"root",
links=[trace_api.Link(invalid_context, {"name": "neighbor"})],
) as root:
self.assertEqual(len(root.links), 1)

def test_add_link_with_invalid_span_context_with_tracestate(self):
invalid_context = trace.SpanContext(
trace_api.INVALID_TRACE_ID,
Expand All @@ -975,6 +986,11 @@ def test_add_link_with_invalid_span_context_with_tracestate(self):
self.assertEqual(len(root.links), 1)
self.assertEqual(root.links[0].context.trace_state, "foo=bar")

with self.tracer.start_as_current_span(
"root", links=[trace_api.Link(invalid_context)]
) as root:
self.assertEqual(len(root.links), 1)

def test_update_name(self):
with self.tracer.start_as_current_span("root") as root:
# name
Expand Down