From f14d93609fbf542a6270d97bf83647782e70ced6 Mon Sep 17 00:00:00 2001 From: Jeremy Voss Date: Mon, 18 Jul 2022 13:46:06 -0700 Subject: [PATCH] Change tracing to use `Resource.to_json()` (#2784) * Change tracing to use Resource.to_json() * Edited CHANGELOG * Added PR # to CHANGELOG * Change tracing to use Resource.to_json() * Edited CHANGELOG * Added PR # to CHANGELOG * Generalized removal of empty or blank values * Simplified trace to_json to include empty and null values. Co-authored-by: Diego Hurtado Co-authored-by: Leighton Chen Co-authored-by: Srikanth Chekuri --- CHANGELOG.md | 2 ++ .../src/opentelemetry/sdk/trace/__init__.py | 2 +- opentelemetry-sdk/tests/trace/test_trace.py | 9 ++++++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 187922d7dfe..5af80680b26 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 7dc65600f4c..6d30e94ed53 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 24d7b6fa3d7..a6683ebd032 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": ""}}', )