From fc66d515c08dceef5fca12fd3b88f79afab9ddd8 Mon Sep 17 00:00:00 2001 From: Matthew Leung Date: Wed, 21 Jul 2021 18:41:46 -0700 Subject: [PATCH] Conform zipkin traceId, spanId, parentId specs Using v1 API defined in https://github.com/openzipkin/zipkin-api/blob/7692ca7be4dc3be9225db550d60c4d30e6e9ec59/zipkin-api.yaml traceId to 32 character lowercase hex string spanId to 16 character lowercase hex string parentId to be absent for root span --- baseplate/__init__.py | 5 +++-- baseplate/observers/tracing.py | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/baseplate/__init__.py b/baseplate/__init__.py index 4fe8ccbac..1230fb160 100644 --- a/baseplate/__init__.py +++ b/baseplate/__init__.py @@ -124,8 +124,9 @@ def new(cls) -> "TraceInfo": with any upstream requests. """ - trace_id = str(random.getrandbits(64)) - return cls(trace_id=trace_id, parent_id=None, span_id=trace_id, sampled=None, flags=None) + trace_id = "{0:0{1}x}".format(random.getrandbits(128), 32) + span_id = "{0:0{1}x}".format(random.getrandbits(64), 16) + return cls(trace_id=trace_id, parent_id=None, span_id=span_id, sampled=None, flags=None) @classmethod def from_upstream( diff --git a/baseplate/observers/tracing.py b/baseplate/observers/tracing.py index 7a3021270..594c18530 100644 --- a/baseplate/observers/tracing.py +++ b/baseplate/observers/tracing.py @@ -279,7 +279,9 @@ def _to_span_obj( "binaryAnnotations": binary_annotations, } - span["parentId"] = self.span.parent_id or 0 + if self.span.parent_id: + span["parentId"] = self.span.parent_id + return span def _serialize(self) -> Dict[str, Any]: