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

Make event attributes immutable #1195

Merged
merged 8 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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 opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
([#1105](https://github.com/open-telemetry/opentelemetry-python/pull/1120))
- Allow for Custom Trace and Span IDs Generation - `IdsGenerator` for TracerProvider
([#1153](https://github.com/open-telemetry/opentelemetry-python/pull/1153))
- Event attributes are now immutable
([#1195](https://github.com/open-telemetry/opentelemetry-python/pull/1195))

## Version 0.13b0

Expand Down
15 changes: 12 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
from opentelemetry.sdk import util
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import sampling
from opentelemetry.sdk.util import BoundedDict, BoundedList
from opentelemetry.sdk.util import (
BoundedDict,
BoundedList,
make_immutable_dict,
)
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
from opentelemetry.trace import SpanContext
from opentelemetry.trace.propagation import SPAN_KEY
Expand Down Expand Up @@ -336,6 +340,11 @@ def _filter_attribute_values(attributes: types.Attributes):
attributes.pop(attr_key)


# pylint: disable=protected-access
def _make_event_attributes_immutable(event: EventBase) -> None:
eoinnoble marked this conversation as resolved.
Show resolved Hide resolved
event._attributes = make_immutable_dict(event.attributes)


class Span(trace_api.Span):
"""See `opentelemetry.trace.Span`.

Expand Down Expand Up @@ -399,6 +408,7 @@ def __init__(
if events:
for event in events:
_filter_attribute_values(event.attributes)
_make_event_attributes_immutable(event)
self.events.append(event)

if links is None:
Expand Down Expand Up @@ -557,8 +567,7 @@ def add_event(
timestamp: Optional[int] = None,
) -> None:
_filter_attribute_values(attributes)
if not attributes:
attributes = self._new_attributes()
attributes = make_immutable_dict(attributes)
self._add_event(
Event(
name=name,
Expand Down
5 changes: 5 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import datetime
import threading
from collections import OrderedDict, deque
from types import MappingProxyType

try:
# pylint: disable=ungrouped-imports
Expand Down Expand Up @@ -45,6 +46,10 @@ def get_dict_as_key(labels):
)


def make_immutable_dict(attributes):
eoinnoble marked this conversation as resolved.
Show resolved Hide resolved
return MappingProxyType(attributes.copy() if attributes else {})


class BoundedList(Sequence):
"""An append only list with a fixed max size.

Expand Down
25 changes: 24 additions & 1 deletion opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,6 @@ def test_events(self):
root.add_event("event0")

# event name and attributes
now = time_ns()
root.add_event(
"event1", {"name": "pluto", "some_bools": [True, False]}
)
Expand Down Expand Up @@ -599,6 +598,30 @@ def test_events(self):
root.events[3].attributes, {"name": ("original_contents",)}
)

def test_events_are_immutable(self):
event_properties = [
prop for prop in dir(trace.EventBase) if not prop.startswith("_")
]

with self.tracer.start_as_current_span("root") as root:
root.add_event("event0", {"name": ["birthday"]})
event = root.events[0]

for prop in event_properties:
with self.assertRaises(AttributeError):
setattr(event, prop, "something")

def test_event_attributes_are_immutable(self):
with self.tracer.start_as_current_span("root") as root:
root.add_event("event0", {"name": ["birthday"]})
event = root.events[0]

with self.assertRaises(TypeError):
event.attributes["name"][0] = "happy"

with self.assertRaises(TypeError):
event.attributes["name"] = "hello"

def test_invalid_event_attributes(self):
self.assertEqual(trace_api.get_current_span(), trace_api.INVALID_SPAN)

Expand Down