Skip to content

Commit 9a20504

Browse files
authored
Fix use of link.attributes.dropped, which may not exist (#4119)
1 parent 7291af2 commit 9a20504

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Fix use of `link.attributes.dropped`, which may not exist
11+
([#4119](https://github.com/open-telemetry/opentelemetry-python/pull/4119))
1012
- Running mypy on SDK resources
1113
([#4053](https://github.com/open-telemetry/opentelemetry-python/pull/4053))
1214
- Added py.typed file to top-level module

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/trace_encoder/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _encode_links(links: Sequence[Link]) -> Sequence[PB2SPan.Link]:
157157
trace_id=_encode_trace_id(link.context.trace_id),
158158
span_id=_encode_span_id(link.context.span_id),
159159
attributes=_encode_attributes(link.attributes),
160-
dropped_attributes_count=link.attributes.dropped,
160+
dropped_attributes_count=link.dropped_attributes,
161161
flags=_span_flags(link.context),
162162
)
163163
pb2_links.append(encoded_link)

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def setUp(self):
171171
"attributes": BoundedAttributes(
172172
attributes={"a": 1, "b": False}
173173
),
174+
"dropped_attributes": 0,
174175
"kind": OTLPSpan.SpanKind.SPAN_KIND_INTERNAL, # pylint: disable=no-member
175176
}
176177
)

opentelemetry-api/src/opentelemetry/trace/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
from deprecated import deprecated
8585

8686
from opentelemetry import context as context_api
87+
from opentelemetry.attributes import BoundedAttributes
8788
from opentelemetry.context.context import Context
8889
from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER
8990
from opentelemetry.trace.propagation import (
@@ -149,6 +150,12 @@ def __init__(
149150
def attributes(self) -> types.Attributes:
150151
return self._attributes
151152

153+
@property
154+
def dropped_attributes(self) -> int:
155+
if isinstance(self._attributes, BoundedAttributes):
156+
return self._attributes.dropped
157+
return 0
158+
152159

153160
_Links = Optional[Sequence[Link]]
154161

opentelemetry-sdk/tests/trace/test_trace.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,19 @@ def test_event_dropped_attributes(self):
669669
event2 = trace.Event("foo2", {"bar2": "baz2"})
670670
self.assertEqual(event2.dropped_attributes, 0)
671671

672+
def test_link_dropped_attributes(self):
673+
link1 = trace_api.Link(
674+
mock.Mock(spec=trace_api.SpanContext),
675+
BoundedAttributes(0, attributes={"bar1": "baz1"}),
676+
)
677+
self.assertEqual(link1.dropped_attributes, 1)
678+
679+
link2 = trace_api.Link(
680+
mock.Mock(spec=trace_api.SpanContext),
681+
{"bar2": "baz2"},
682+
)
683+
self.assertEqual(link2.dropped_attributes, 0)
684+
672685

673686
class DummyError(Exception):
674687
pass
@@ -1897,7 +1910,7 @@ def test_dropped_attributes(self):
18971910
self.assertEqual(2, span.dropped_attributes)
18981911
self.assertEqual(3, span.dropped_events)
18991912
self.assertEqual(2, span.events[0].dropped_attributes)
1900-
self.assertEqual(2, span.links[0].attributes.dropped)
1913+
self.assertEqual(2, span.links[0].dropped_attributes)
19011914

19021915
def _test_span_limits(
19031916
self,

0 commit comments

Comments
 (0)