Skip to content

Commit

Permalink
Change tracing to use Resource.to_json() (#2784)
Browse files Browse the repository at this point in the history
* 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 <ocelotl@users.noreply.github.com>
Co-authored-by: Leighton Chen <lechen@microsoft.com>
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
  • Loading branch information
4 people authored Jul 18, 2022
1 parent 30a4d45 commit f14d936
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 6 additions & 3 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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": ""}}',
)


Expand Down

0 comments on commit f14d936

Please sign in to comment.