Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use of link.attributes.dropped, which may not exist #4119

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading