Skip to content

Commit f14d936

Browse files
jeremydvossocelotllzchensrikanthccv
authored
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 <[email protected]> Co-authored-by: Leighton Chen <[email protected]> Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent 30a4d45 commit f14d936

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- Change tracing to use `Resource.to_json()`
11+
([#2784](https://github.com/open-telemetry/opentelemetry-python/pull/2784))
1012
- Fix get_log_emitter instrumenting_module_version args typo
1113
([#2830](https://github.com/open-telemetry/opentelemetry-python/pull/2830))
1214
- Fix OTLP gRPC exporter warning message

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ def to_json(self, indent=4):
492492
f_span["attributes"] = self._format_attributes(self._attributes)
493493
f_span["events"] = self._format_events(self._events)
494494
f_span["links"] = self._format_links(self._links)
495-
f_span["resource"] = self._format_attributes(self._resource.attributes)
495+
f_span["resource"] = json.loads(self.resource.to_json())
496496

497497
return json.dumps(f_span, indent=indent)
498498

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,12 +1354,15 @@ def test_to_json(self):
13541354
"attributes": {},
13551355
"events": [],
13561356
"links": [],
1357-
"resource": {}
1357+
"resource": {
1358+
"attributes": {},
1359+
"schema_url": ""
1360+
}
13581361
}""",
13591362
)
13601363
self.assertEqual(
13611364
span.to_json(indent=None),
1362-
'{"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": {}}',
1365+
'{"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": ""}}',
13631366
)
13641367

13651368
def test_attributes_to_json(self):
@@ -1377,7 +1380,7 @@ def test_attributes_to_json(self):
13771380
span.to_json(indent=None),
13781381
'{"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": "'
13791382
+ date_str
1380-
+ '", "attributes": {"key2": "value2"}}], "links": [], "resource": {}}',
1383+
+ '", "attributes": {"key2": "value2"}}], "links": [], "resource": {"attributes": {}, "schema_url": ""}}',
13811384
)
13821385

13831386

0 commit comments

Comments
 (0)