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

fix(w3c trace context): correctly parse flags hex string to int #424

Merged
Merged
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 instana/w3c_trace_context/traceparent.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ def get_traceparent_fields(traceparent):
version = traceparent_properties[0]
trace_id = traceparent_properties[1]
parent_id = traceparent_properties[2]
flags = int(traceparent_properties[3])
flags = int(traceparent_properties[3], 16)
sampled_flag = (flags & SAMPLED_BITMASK) == SAMPLED_BITMASK
return version, trace_id, parent_id, sampled_flag
except Exception: # This method is intended to be called with a version 00 validated traceparent
except Exception as err: # This method is intended to be called with a version 00 validated traceparent
# This exception handling is added just for making sure we do not throw any unhandled exception
# if somebody calls the method in the future without a validated traceparent
logger.debug("Parsing the traceparent failed: {}".format(err))
return None, None, None, None

def update_traceparent(self, traceparent, in_trace_id, in_span_id, level):
Expand Down
26 changes: 26 additions & 0 deletions tests/w3c_trace_context/test_traceparent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ def test_validate_valid(self):
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
self.assertEqual(traceparent, self.tp.validate(traceparent))

def test_validate_newer_version(self):
# Although the incoming traceparent header sports a newer version number, we should still be able to parse the
# parts that we understand (and consider it valid).
traceparent = "ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd"
self.assertEqual(traceparent, self.tp.validate(traceparent))

def test_validate_unknown_flags(self):
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-ee"
self.assertEqual(traceparent, self.tp.validate(traceparent))

def test_validate_invalid_traceparent(self):
traceparent = "00-4bxxxxx3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
self.assertIsNone(self.tp.validate(traceparent))
Expand All @@ -35,6 +45,22 @@ def test_get_traceparent_fields_unsampled(self):
self.assertEqual(parent_id, "00f067aa0ba902b7")
self.assertFalse(sampled_flag)

def test_get_traceparent_fields_newer_version(self):
# Although the incoming traceparent header sports a newer version number, we should still be able to parse the
# parts that we understand (and consider it valid).
traceparent = "ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd"
version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent)
self.assertEqual(trace_id, "4bf92f3577b34da6a3ce929d0e0e4736")
self.assertEqual(parent_id, "00f067aa0ba902b7")
self.assertTrue(sampled_flag)

def test_get_traceparent_fields_unknown_flags(self):
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-ff"
version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent)
self.assertEqual(trace_id, "4bf92f3577b34da6a3ce929d0e0e4736")
self.assertEqual(parent_id, "00f067aa0ba902b7")
self.assertTrue(sampled_flag)

def test_get_traceparent_fields_None_input(self):
traceparent = None
version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent)
Expand Down