@@ -264,6 +264,16 @@ def __init__(
264264 self ._start_timestamp = datetime .now (timezone .utc )
265265 self ._timestamp : "Optional[datetime]" = None
266266
267+ try :
268+ # profiling depends on this value and requires that
269+ # it is measured in nanoseconds
270+ self ._start_timestamp_monotonic_ns = nanosecond_time ()
271+ except AttributeError :
272+ pass
273+
274+ self ._start_timestamp = datetime .now (timezone .utc )
275+ self ._timestamp : "Optional[datetime]" = None
276+
267277 try :
268278 # profiling depends on this value and requires that
269279 # it is measured in nanoseconds
@@ -294,8 +304,12 @@ def __enter__(self) -> "StreamedSpan":
294304 def __exit__ (
295305 self , ty : "Optional[Any]" , value : "Optional[Any]" , tb : "Optional[Any]"
296306 ) -> None :
307+ if self ._timestamp is not None :
308+ # This span is already finished, ignore
309+ return
310+
297311 if value is not None and should_be_treated_as_error (ty , value ):
298- self .status = SpanStatus .ERROR
312+ self .status = SpanStatus .ERROR . value
299313
300314 self ._end ()
301315
@@ -335,7 +349,9 @@ def _end(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None
335349 del self ._previous_span_on_scope
336350 self ._scope .span = old_span
337351
338- # Set attributes from the segment
352+ # Set attributes from the segment. These are set on span end on purpose
353+ # so that we have the best chance to capture the segment's final name
354+ # (since it might change during its lifetime)
339355 self .set_attribute ("sentry.segment.id" , self ._segment .span_id )
340356 self .set_attribute ("sentry.segment.name" , self ._segment .name )
341357
0 commit comments