Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplified trace to_json to include empty and null values.
  • Loading branch information
jeremydvoss committed Jul 14, 2022
commit 4b61f9ac330d0145445fba48f915afacf7e8554f
7 changes: 1 addition & 6 deletions 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_resource(self._resource)
f_span["resource"] = json.loads(self.resource.to_json())

return json.dumps(f_span, indent=indent)

Expand Down Expand Up @@ -533,11 +533,6 @@ def _format_links(links):
f_links.append(f_link)
return f_links

@staticmethod
def _format_resource(resource):
resource_json_obj = json.loads(resource.to_json())
return {k: v for k, v in resource_json_obj.items() if v}


class SpanLimits:
"""The limits that should be enforce on recorded data such as events, links, attributes etc.
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