Skip to content

Commit

Permalink
Fix use of link.attributes.dropped, which may not exist (#4119)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinji authored Aug 13, 2024
1 parent 7291af2 commit 9a20504
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fix use of `link.attributes.dropped`, which may not exist
([#4119](https://github.com/open-telemetry/opentelemetry-python/pull/4119))
- Running mypy on SDK resources
([#4053](https://github.com/open-telemetry/opentelemetry-python/pull/4053))
- Added py.typed file to top-level module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def _encode_links(links: Sequence[Link]) -> Sequence[PB2SPan.Link]:
trace_id=_encode_trace_id(link.context.trace_id),
span_id=_encode_span_id(link.context.span_id),
attributes=_encode_attributes(link.attributes),
dropped_attributes_count=link.attributes.dropped,
dropped_attributes_count=link.dropped_attributes,
flags=_span_flags(link.context),
)
pb2_links.append(encoded_link)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def setUp(self):
"attributes": BoundedAttributes(
attributes={"a": 1, "b": False}
),
"dropped_attributes": 0,
"kind": OTLPSpan.SpanKind.SPAN_KIND_INTERNAL, # pylint: disable=no-member
}
)
Expand Down
7 changes: 7 additions & 0 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
from deprecated import deprecated

from opentelemetry import context as context_api
from opentelemetry.attributes import BoundedAttributes
from opentelemetry.context.context import Context
from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER
from opentelemetry.trace.propagation import (
Expand Down Expand Up @@ -149,6 +150,12 @@ def __init__(
def attributes(self) -> types.Attributes:
return self._attributes

@property
def dropped_attributes(self) -> int:
if isinstance(self._attributes, BoundedAttributes):
return self._attributes.dropped
return 0


_Links = Optional[Sequence[Link]]

Expand Down
15 changes: 14 additions & 1 deletion opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,19 @@ def test_event_dropped_attributes(self):
event2 = trace.Event("foo2", {"bar2": "baz2"})
self.assertEqual(event2.dropped_attributes, 0)

def test_link_dropped_attributes(self):
link1 = trace_api.Link(
mock.Mock(spec=trace_api.SpanContext),
BoundedAttributes(0, attributes={"bar1": "baz1"}),
)
self.assertEqual(link1.dropped_attributes, 1)

link2 = trace_api.Link(
mock.Mock(spec=trace_api.SpanContext),
{"bar2": "baz2"},
)
self.assertEqual(link2.dropped_attributes, 0)


class DummyError(Exception):
pass
Expand Down Expand Up @@ -1897,7 +1910,7 @@ def test_dropped_attributes(self):
self.assertEqual(2, span.dropped_attributes)
self.assertEqual(3, span.dropped_events)
self.assertEqual(2, span.events[0].dropped_attributes)
self.assertEqual(2, span.links[0].attributes.dropped)
self.assertEqual(2, span.links[0].dropped_attributes)

def _test_span_limits(
self,
Expand Down

0 comments on commit 9a20504

Please sign in to comment.