diff --git a/CHANGELOG.md b/CHANGELOG.md index 187922d7df..5af80680b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Change tracing to use `Resource.to_json()` + ([#2784](https://github.com/open-telemetry/opentelemetry-python/pull/2784)) - Fix get_log_emitter instrumenting_module_version args typo ([#2830](https://github.com/open-telemetry/opentelemetry-python/pull/2830)) - Fix OTLP gRPC exporter warning message diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 7dc65600f4..6d30e94ed5 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -492,7 +492,7 @@ def to_json(self, indent=4): f_span["attributes"] = self._format_attributes(self._attributes) f_span["events"] = self._format_events(self._events) f_span["links"] = self._format_links(self._links) - f_span["resource"] = self._format_attributes(self._resource.attributes) + f_span["resource"] = json.loads(self.resource.to_json()) return json.dumps(f_span, indent=indent) diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 24d7b6fa3d..a6683ebd03 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -1354,12 +1354,15 @@ def test_to_json(self): "attributes": {}, "events": [], "links": [], - "resource": {} + "resource": { + "attributes": {}, + "schema_url": "" + } }""", ) self.assertEqual( span.to_json(indent=None), - '{"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": {}}', + '{"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": {"attributes": {}, "schema_url": ""}}', ) def test_attributes_to_json(self): @@ -1377,7 +1380,7 @@ def test_attributes_to_json(self): 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": {"key": "value"}, "events": [{"name": "event", "timestamp": "' + date_str - + '", "attributes": {"key2": "value2"}}], "links": [], "resource": {}}', + + '", "attributes": {"key2": "value2"}}], "links": [], "resource": {"attributes": {}, "schema_url": ""}}', )