Skip to content

Commit 5554152

Browse files
authored
Bug fix: Serialize event attributes properly (open-telemetry#1246)
1 parent beccc3b commit 5554152

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ def _format_context(context):
463463
def _format_attributes(attributes):
464464
if isinstance(attributes, BoundedDict):
465465
return attributes._dict # pylint: disable=protected-access
466+
if isinstance(attributes, MappingProxyType):
467+
return attributes.copy()
466468
return attributes
467469

468470
@staticmethod

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from opentelemetry import trace as trace_api
2323
from opentelemetry.sdk import resources, trace
2424
from opentelemetry.sdk.trace import Resource, sampling
25+
from opentelemetry.sdk.util import ns_to_iso_str
2526
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
2627
from opentelemetry.trace.status import StatusCanonicalCode
2728
from opentelemetry.util import time_ns
@@ -1032,3 +1033,22 @@ def test_to_json(self):
10321033
span.to_json(indent=None),
10331034
'{"name": "span-name", "context": {"trace_id": "0x000000000000000000000000deadbeef", "span_id": "0x00000000deadbef0", "trace_state": "{}"}, "kind": "SpanKind.INTERNAL", "parent_id": null, "start_time": null, "end_time": null, "attributes": {}, "events": [], "links": [], "resource": {}}',
10341035
)
1036+
1037+
def test_attributes_to_json(self):
1038+
context = trace_api.SpanContext(
1039+
trace_id=0x000000000000000000000000DEADBEEF,
1040+
span_id=0x00000000DEADBEF0,
1041+
is_remote=False,
1042+
trace_flags=trace_api.TraceFlags(trace_api.TraceFlags.SAMPLED),
1043+
)
1044+
span = trace._Span("span-name", context)
1045+
span.resource = Resource({})
1046+
span.set_attribute("key", "value")
1047+
span.add_event("event", {"key2": "value2"}, 123)
1048+
date_str = ns_to_iso_str(123)
1049+
self.assertEqual(
1050+
span.to_json(indent=None),
1051+
'{"name": "span-name", "context": {"trace_id": "0x000000000000000000000000deadbeef", "span_id": "0x00000000deadbef0", "trace_state": "{}"}, "kind": "SpanKind.INTERNAL", "parent_id": null, "start_time": null, "end_time": null, "attributes": {"key": "value"}, "events": [{"name": "event", "timestamp": "'
1052+
+ date_str
1053+
+ '", "attributes": {"key2": "value2"}}], "links": [], "resource": {}}',
1054+
)

0 commit comments

Comments
 (0)