Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 8c8189e

Browse files
ewhauserreyang
authored andcommitted
improve trace_id and span_id generation perf and randomness
1 parent c59a087 commit 8c8189e

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

opencensus/trace/span_context.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import logging
1818
import re
1919
import six
20-
import uuid
20+
import random
21+
import time
2122

2223
from opencensus.trace import trace_options as trace_options_module
2324

@@ -156,8 +157,7 @@ def generate_span_id():
156157
:rtype: str
157158
:returns: 16 digit randomly generated hex trace id.
158159
"""
159-
span_id = uuid.uuid4().hex[:16]
160-
return span_id
160+
return '{:016x}'.format(random.getrandbits(64))
161161

162162

163163
def generate_trace_id():
@@ -167,5 +167,6 @@ def generate_trace_id():
167167
:rtype: str
168168
:returns: 32 digit randomly generated hex trace id.
169169
"""
170-
trace_id = uuid.uuid4().hex
171-
return trace_id
170+
t = int(time.time())
171+
lower_96 = random.getrandbits(96)
172+
return '{:032x}'.format((t << 96) | lower_96)

0 commit comments

Comments
 (0)