diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c82de004af..f0d9bf3e926 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1811](https://github.com/open-telemetry/opentelemetry-python/pull/1811)) - Improve warning when failing to decode byte attribute ([#1810](https://github.com/open-telemetry/opentelemetry-python/pull/1810)) +- Fixed inconsistency in parent_id formatting from the ConsoleSpanExporter + ([#1833](https://github.com/open-telemetry/opentelemetry-python/pull/1833)) ### Removed - Moved `opentelemetry-instrumentation` to contrib repository. diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 882a7ad4a0d..ece7da6d225 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -483,9 +483,13 @@ def to_json(self, indent=4): if self.parent is not None: if isinstance(self.parent, Span): ctx = self.parent.context - parent_id = trace_api.format_span_id(ctx.span_id) + parent_id = "0x{}".format( + trace_api.format_span_id(ctx.span_id) + ) elif isinstance(self.parent, SpanContext): - parent_id = trace_api.format_span_id(self.parent.span_id) + parent_id = "0x{}".format( + trace_api.format_span_id(self.parent.span_id) + ) start_time = None if self._start_time: diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index df2454a5215..f12dc7c75cf 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -1221,7 +1221,10 @@ def test_to_json(self): is_remote=False, trace_flags=trace_api.TraceFlags(trace_api.TraceFlags.SAMPLED), ) - span = trace._Span("span-name", context, resource=Resource({})) + parent = trace._Span("parent-name", context, resource=Resource({})) + span = trace._Span( + "span-name", context, resource=Resource({}), parent=parent + ) self.assertEqual( span.to_json(), @@ -1233,7 +1236,7 @@ def test_to_json(self): "trace_state": "[]" }, "kind": "SpanKind.INTERNAL", - "parent_id": null, + "parent_id": "0x00000000deadbef0", "start_time": null, "end_time": null, "status": { @@ -1247,7 +1250,7 @@ def test_to_json(self): ) self.assertEqual( span.to_json(indent=None), - '{"name": "span-name", "context": {"trace_id": "0x000000000000000000000000deadbeef", "span_id": "0x00000000deadbef0", "trace_state": "[]"}, "kind": "SpanKind.INTERNAL", "parent_id": null, "start_time": null, "end_time": null, "status": {"status_code": "UNSET"}, "attributes": {}, "events": [], "links": [], "resource": {}}', + '{"name": "span-name", "context": {"trace_id": "0x000000000000000000000000deadbeef", "span_id": "0x00000000deadbef0", "trace_state": "[]"}, "kind": "SpanKind.INTERNAL", "parent_id": "0x00000000deadbef0", "start_time": null, "end_time": null, "status": {"status_code": "UNSET"}, "attributes": {}, "events": [], "links": [], "resource": {}}', ) def test_attributes_to_json(self):