Skip to content

Commit c9230c5

Browse files
sl0thentr0pysentrivana
authored andcommitted
Make UUID generation lazy
1 parent dd323d6 commit c9230c5

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

sentry_sdk/tracing.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ class Span:
257257
"""
258258

259259
__slots__ = (
260-
"trace_id",
261-
"span_id",
260+
"_trace_id",
261+
"_span_id",
262262
"parent_span_id",
263263
"same_process_as_parent",
264264
"sampled",
@@ -301,8 +301,8 @@ def __init__(
301301
name=None, # type: Optional[str]
302302
):
303303
# type: (...) -> None
304-
self.trace_id = trace_id or uuid.uuid4().hex
305-
self.span_id = span_id or uuid.uuid4().hex[16:]
304+
self._trace_id = trace_id
305+
self._span_id = span_id
306306
self.parent_span_id = parent_span_id
307307
self.same_process_as_parent = same_process_as_parent
308308
self.sampled = sampled
@@ -356,6 +356,32 @@ def init_span_recorder(self, maxlen):
356356
if self._span_recorder is None:
357357
self._span_recorder = _SpanRecorder(maxlen)
358358

359+
@property
360+
def trace_id(self):
361+
# type: () -> str
362+
if not self._trace_id:
363+
self._trace_id = uuid.uuid4().hex
364+
365+
return self._trace_id
366+
367+
@trace_id.setter
368+
def trace_id(self, value):
369+
# type: (str) -> None
370+
self._trace_id = value
371+
372+
@property
373+
def span_id(self):
374+
# type: () -> str
375+
if not self._span_id:
376+
self._span_id = uuid.uuid4().hex[16:]
377+
378+
return self._span_id
379+
380+
@span_id.setter
381+
def span_id(self, value):
382+
# type: (str) -> None
383+
self._span_id = value
384+
359385
def _get_local_aggregator(self):
360386
# type: (...) -> LocalAggregator
361387
rv = self._local_aggregator

0 commit comments

Comments
 (0)