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 _encode_events assumes events.attributes.dropped exists #3965

Merged
merged 11 commits into from
Jun 20, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

- Fix _encode_events assumes events.attributes.dropped exists
([#3965](https://github.com/open-telemetry/opentelemetry-python/pull/3965))
- Fix class BoundedAttributes to have RLock rather than Lock
([#3859](https://github.com/open-telemetry/opentelemetry-python/pull/3859))
- Remove thread lock by loading RuntimeContext explicitly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _encode_events(
name=event.name,
time_unix_nano=event.timestamp,
attributes=_encode_attributes(event.attributes),
dropped_attributes_count=event.attributes.dropped,
dropped_attributes_count=event.dropped_attributes,
soumyadeepm04 marked this conversation as resolved.
Show resolved Hide resolved
)
pb2_events.append(encoded_event)
return pb2_events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def setUp(self):
)

type(event_mock).name = PropertyMock(return_value="a")

type(event_mock).dropped_attributes = PropertyMock(return_value=0)
self.span = _Span(
"a",
context=Mock(
Expand Down
6 changes: 6 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ def __init__(
def attributes(self) -> types.Attributes:
return self._attributes

@property
def dropped_attributes(self) -> int:
soumyadeepm04 marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(self._attributes, BoundedAttributes):
return self._attributes.dropped
return 0


def _check_span_ended(func):
def wrapper(self, *args, **kwargs):
Expand Down
12 changes: 11 additions & 1 deletion opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from unittest.mock import Mock, patch

from opentelemetry import trace as trace_api
from opentelemetry.attributes import BoundedAttributes
from opentelemetry.context import Context
from opentelemetry.sdk import resources, trace
from opentelemetry.sdk.environment_variables import (
Expand Down Expand Up @@ -635,6 +636,15 @@ def test_events(self):
span = trace.ReadableSpan("test", events=events)
self.assertEqual(span.events, tuple(events))

def test_event_dropped_attributes(self):
event1 = trace.Event(
"foo1", BoundedAttributes(0, attributes={"bar1": "baz1"})
)
self.assertEqual(event1.dropped_attributes, 1)

event2 = trace.Event("foo2", {"bar2": "baz2"})
self.assertEqual(event2.dropped_attributes, 0)


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

def _test_span_limits(
Expand Down