Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conform zipkin traceId, spanId, parentId specs #604

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions baseplate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

padded string with leading 0's to reach 32 characters - necessary in case hex string comes up short.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
trace_id = "{0:0{1}x}".format(random.getrandbits(128), 32)
trace_id = f"{random.getrandbits(128):032x}"

Parameterizing the 32 makes it quite hard to read the format string, and using f-strings makes this a little easier to understand as well.

span_id = "{0:0{1}x}".format(random.getrandbits(64), 16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
span_id = "{0:0{1}x}".format(random.getrandbits(64), 16)
span_id = f"{random.getrandbits(64):016x}"

return cls(trace_id=trace_id, parent_id=None, span_id=span_id, sampled=None, flags=None)

@classmethod
def from_upstream(
Expand Down
4 changes: 3 additions & 1 deletion baseplate/observers/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ def _to_span_obj(
"binaryAnnotations": binary_annotations,
}

span["parentId"] = self.span.parent_id or 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. This means we don't send a parentId at all if it's undefined. Is that right? No default?

if self.span.parent_id:
span["parentId"] = self.span.parent_id

return span

def _serialize(self) -> Dict[str, Any]:
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/pyramid_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ def _register_mock(context, server_span):

@mock.patch("random.getrandbits")
def test_no_trace_headers(self, getrandbits):
getrandbits.return_value = 1234
getrandbits.return_value = 3735928559
self.test_app.get("/example")

self.assertEqual(self.observer.on_server_span_created.call_count, 1)

context, server_span = self.observer.on_server_span_created.call_args[0]
self.assertEqual(server_span.trace_id, "1234")
self.assertEqual(server_span.trace_id, "0" * 24 + "deadbeef")
self.assertEqual(server_span.parent_id, None)
self.assertEqual(server_span.id, "1234")
self.assertEqual(server_span.id, "0" * 8 + "deadbeef")

self.assertTrue(self.server_observer.on_start.called)
self.assertTrue(self.server_observer.on_finish.called)
Expand Down Expand Up @@ -222,17 +222,17 @@ def test_exception_in_exception_view_caught(self):

@mock.patch("random.getrandbits")
def test_distrust_headers(self, getrandbits):
getrandbits.return_value = 9999
getrandbits.return_value = 3735928559
self.baseplate_configurator.header_trust_handler.trust_headers = False

self.test_app.get(
"/example", headers={"X-Trace": "1234", "X-Parent": "2345", "X-Span": "3456"}
)

context, server_span = self.observer.on_server_span_created.call_args[0]
self.assertEqual(server_span.trace_id, str(getrandbits.return_value))
self.assertEqual(server_span.trace_id, "0" * 24 + "deadbeef")
self.assertEqual(server_span.parent_id, None)
self.assertEqual(server_span.id, str(getrandbits.return_value))
self.assertEqual(server_span.id, "0" * 8 + "deadbeef")

def test_local_trace_in_context(self):
self.test_app.get("/trace_context")
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/tracing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_trace_on_inbound_request(self):
span = self.server_span_observer._serialize()
self.assertEqual(span["name"], "example")
self.assertEqual(len(span["annotations"]), 2)
self.assertEqual(span["parentId"], 0)
self.assertFalse("parentId" in span)

def test_local_tracing_embedded(self):
with mock.patch.object(
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/observers/tracing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ def test_to_span_obj_sets_parent_id(self):
span_obj = self.test_span_observer._to_span_obj([], [])
self.assertEqual(span_obj["parentId"], self.span.parent_id)

def test_to_span_obj_sets_default_parent_id(self):
def test_to_span_obj_default_no_parent_id(self):
self.span.parent_id = None
span_obj = self.test_span_observer._to_span_obj([], [])
self.assertEqual(span_obj["parentId"], 0)
self.assertFalse("parentId" in span_obj)

def test_incr_tag_adds_binary_annotation(self):
self.test_span_observer.binary_annotations = []
Expand Down