Skip to content

Commit

Permalink
fix(w3c trace context): correctly parse flags hex string to int
Browse files Browse the repository at this point in the history
Signed-off-by: Bastian Krol <bastian.krol@ibm.com>
  • Loading branch information
Bastian Krol committed May 16, 2023
1 parent b6c0e9b commit 16ad220
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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

0 comments on commit 16ad220

Please sign in to comment.