Skip to content

Make UUID generation lazy #2826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down