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 <shohamp@gmail.com>
This commit is contained in:
Shoham Peller 2018-10-02 17:20:13 +03:00
parent 2b4786b5cc
commit 1a18d0f944
2 changed files with 11 additions and 1 deletions

View File

@ -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())

View File

@ -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")