|
22 | 22 | from opentelemetry import trace as trace_api
|
23 | 23 | from opentelemetry.sdk import resources, trace
|
24 | 24 | from opentelemetry.sdk.trace import Resource, sampling
|
| 25 | +from opentelemetry.sdk.util import ns_to_iso_str |
25 | 26 | from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
|
26 | 27 | from opentelemetry.trace.status import StatusCanonicalCode
|
27 | 28 | from opentelemetry.util import time_ns
|
@@ -1032,3 +1033,22 @@ def test_to_json(self):
|
1032 | 1033 | span.to_json(indent=None),
|
1033 | 1034 | '{"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": {}}',
|
1034 | 1035 | )
|
| 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