From 64e0ec53cbc2e4f3b57a5b45562583ce1130b049 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Fri, 15 Mar 2024 14:38:47 +0100 Subject: [PATCH] Make UUID generation lazy --- sentry_sdk/tracing.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index bac1ceaa60..8bef387147 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -92,8 +92,8 @@ class Span(object): Spans can have multiple child spans thus forming a span tree.""" __slots__ = ( - "trace_id", - "span_id", + "_trace_id", + "_span_id", "parent_span_id", "same_process_as_parent", "sampled", @@ -142,8 +142,8 @@ def __init__( start_timestamp=None, # type: Optional[Union[datetime, float]] ): # type: (...) -> None - self.trace_id = trace_id or uuid.uuid4().hex - self.span_id = span_id or uuid.uuid4().hex[16:] + self._trace_id = trace_id + self._span_id = span_id self.parent_span_id = parent_span_id self.same_process_as_parent = same_process_as_parent self.sampled = sampled @@ -179,6 +179,32 @@ def init_span_recorder(self, maxlen): if self._span_recorder is None: self._span_recorder = _SpanRecorder(maxlen) + @property + def trace_id(self): + # type: () -> str + if not self._trace_id: + self._trace_id = uuid.uuid4().hex + + return self._trace_id + + @trace_id.setter + def trace_id(self, value): + # type: (str) -> None + self._trace_id = value + + @property + def span_id(self): + # type: () -> str + if not self._span_id: + self._span_id = uuid.uuid4().hex[16:] + + return self._span_id + + @span_id.setter + def span_id(self, value): + # type: (str) -> None + self._span_id = value + def _get_local_aggregator(self): # type: (...) -> LocalAggregator rv = self._local_aggregator