From 1a18d0f9449ef88db69d1f26b303da08bfc3650b Mon Sep 17 00:00:00 2001 From: Shoham Peller Date: Tue, 2 Oct 2018 17:20:13 +0300 Subject: [PATCH] When shortening span-ids, check if they're already short If you're given an int, there's nothing to do, just return it Change-Id: I289dab8cfabaecf243fdc16b972695e83c83c3e4 Closes-Bug: 1795664 Signed-off-by: Shoham Peller --- osprofiler/_utils.py | 5 ++++- osprofiler/tests/unit/test_profiler.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/osprofiler/_utils.py b/osprofiler/_utils.py index b60a33d..c23d600 100644 --- a/osprofiler/_utils.py +++ b/osprofiler/_utils.py @@ -153,8 +153,11 @@ def import_modules_from_package(package): def shorten_id(span_id): """Convert from uuid4 to 64 bit id for OpenTracing""" + int64_max = (1 << 64) - 1 + if isinstance(span_id, six.integer_types): + return span_id & int64_max try: - short_id = uuid.UUID(span_id).int & (1 << 64) - 1 + short_id = uuid.UUID(span_id).int & int64_max except ValueError: # Return a new short id for this short_id = shorten_id(uuidutils.generate_uuid()) diff --git a/osprofiler/tests/unit/test_profiler.py b/osprofiler/tests/unit/test_profiler.py index 1bd4b3f..00dcd0f 100644 --- a/osprofiler/tests/unit/test_profiler.py +++ b/osprofiler/tests/unit/test_profiler.py @@ -69,6 +69,13 @@ class ProfilerTestCase(test.TestCase): expected = "850409eb1d4b0dee" self.assertEqual(expected, result) + def test_profiler_get_shorten_id_int(self): + short_id_int = 42 + prof = profiler._Profiler("secret", base_id="1", parent_id="2") + result = prof.get_shorten_id(short_id_int) + expected = "2a" + self.assertEqual(expected, result) + def test_profiler_get_base_id(self): prof = profiler._Profiler("secret", base_id="1", parent_id="2") self.assertEqual(prof.get_base_id(), "1")