diff --git a/instana/w3c_trace_context/traceparent.py b/instana/w3c_trace_context/traceparent.py index 6c2605ff5..f3a10a417 100644 --- a/instana/w3c_trace_context/traceparent.py +++ b/instana/w3c_trace_context/traceparent.py @@ -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): diff --git a/tests/w3c_trace_context/test_traceparent.py b/tests/w3c_trace_context/test_traceparent.py index 98d4b6f7d..3134a97fe 100644 --- a/tests/w3c_trace_context/test_traceparent.py +++ b/tests/w3c_trace_context/test_traceparent.py @@ -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)) @@ -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)