|
15 | 15 |
|
16 | 16 | import abc
|
17 | 17 | import atexit
|
| 18 | +import json |
18 | 19 | import logging
|
| 20 | +import os |
19 | 21 | import random
|
20 | 22 | import threading
|
| 23 | +from collections import OrderedDict |
21 | 24 | from contextlib import contextmanager
|
22 | 25 | from types import TracebackType
|
23 | 26 | from typing import Iterator, MutableSequence, Optional, Sequence, Tuple, Type
|
@@ -276,19 +279,79 @@ def __repr__(self):
|
276 | 279 | type(self).__name__, self.name, self.context
|
277 | 280 | )
|
278 | 281 |
|
279 |
| - def __str__(self): |
280 |
| - return ( |
281 |
| - '{}(name="{}", context={}, kind={}, ' |
282 |
| - "parent={}, start_time={}, end_time={})" |
283 |
| - ).format( |
284 |
| - type(self).__name__, |
285 |
| - self.name, |
286 |
| - self.context, |
287 |
| - self.kind, |
288 |
| - repr(self.parent), |
289 |
| - util.ns_to_iso_str(self.start_time) if self.start_time else "None", |
290 |
| - util.ns_to_iso_str(self.end_time) if self.end_time else "None", |
291 |
| - ) |
| 282 | + @staticmethod |
| 283 | + def _format_context(context): |
| 284 | + x_ctx = OrderedDict() |
| 285 | + x_ctx["trace_id"] = trace_api.format_trace_id(context.trace_id) |
| 286 | + x_ctx["span_id"] = trace_api.format_span_id(context.span_id) |
| 287 | + x_ctx["trace_state"] = repr(context.trace_state) |
| 288 | + return x_ctx |
| 289 | + |
| 290 | + @staticmethod |
| 291 | + def _format_attributes(attributes): |
| 292 | + if isinstance(attributes, BoundedDict): |
| 293 | + return attributes._dict # pylint: disable=protected-access |
| 294 | + return attributes |
| 295 | + |
| 296 | + @staticmethod |
| 297 | + def _format_events(events): |
| 298 | + f_events = [] |
| 299 | + for event in events: |
| 300 | + f_event = OrderedDict() |
| 301 | + f_event["name"] = event.name |
| 302 | + f_event["timestamp"] = util.ns_to_iso_str(event.timestamp) |
| 303 | + f_event["attributes"] = Span._format_attributes(event.attributes) |
| 304 | + f_events.append(f_event) |
| 305 | + return f_events |
| 306 | + |
| 307 | + @staticmethod |
| 308 | + def _format_links(links): |
| 309 | + f_links = [] |
| 310 | + for link in links: |
| 311 | + f_link = OrderedDict() |
| 312 | + f_link["context"] = Span._format_context(link.context) |
| 313 | + f_link["attributes"] = Span._format_attributes(link.attributes) |
| 314 | + f_links.append(f_link) |
| 315 | + return f_links |
| 316 | + |
| 317 | + def to_json(self): |
| 318 | + parent_id = None |
| 319 | + if self.parent is not None: |
| 320 | + if isinstance(self.parent, Span): |
| 321 | + ctx = self.parent.context |
| 322 | + parent_id = trace_api.format_span_id(ctx.span_id) |
| 323 | + elif isinstance(self.parent, SpanContext): |
| 324 | + parent_id = trace_api.format_span_id(self.parent.span_id) |
| 325 | + |
| 326 | + start_time = None |
| 327 | + if self.start_time: |
| 328 | + start_time = util.ns_to_iso_str(self.start_time) |
| 329 | + |
| 330 | + end_time = None |
| 331 | + if self.end_time: |
| 332 | + end_time = util.ns_to_iso_str(self.end_time) |
| 333 | + |
| 334 | + if self.status is not None: |
| 335 | + status = OrderedDict() |
| 336 | + status["canonical_code"] = str(self.status.canonical_code.name) |
| 337 | + if self.status.description: |
| 338 | + status["description"] = self.status.description |
| 339 | + |
| 340 | + f_span = OrderedDict() |
| 341 | + |
| 342 | + f_span["name"] = self.name |
| 343 | + f_span["context"] = self._format_context(self.context) |
| 344 | + f_span["kind"] = str(self.kind) |
| 345 | + f_span["parent_id"] = parent_id |
| 346 | + f_span["start_time"] = start_time |
| 347 | + f_span["end_time"] = end_time |
| 348 | + if self.status is not None: |
| 349 | + f_span["status"] = status |
| 350 | + f_span["attributes"] = self._format_attributes(self.attributes) |
| 351 | + f_span["events"] = self._format_events(self.events) |
| 352 | + f_span["links"] = self._format_links(self.links) |
| 353 | + |
| 354 | + return json.dumps(f_span, indent=4) |
292 | 355 |
|
293 | 356 | def get_context(self):
|
294 | 357 | return self.context
|
|
0 commit comments