Skip to content

Commit 575edf9

Browse files
authored
ref: Rename _timestamp to _end_timestamp (#6235)
The field capturing the end timestamp of a span used to be called `timestamp` in legacy mode (on the `Span` class), with `start_timestamp` as its counterpart. In span streaming, we originally followed the same convention on the new `StreamedSpan` class, but there's actually no reason to and it'd be better to use `end_timestamp` instead: - the field on the serialized span v2 is actually called `end_timestamp` - `end_timestamp` is a clearer name in general, and complements `start_timestamp` nicely
1 parent 3d59242 commit 575edf9

5 files changed

Lines changed: 35 additions & 25 deletions

File tree

sentry_sdk/_span_batcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ def _to_transport_format(item: "StreamedSpan") -> "Any":
168168
"start_timestamp": item._start_timestamp.timestamp(),
169169
}
170170

171-
if item._timestamp:
172-
res["end_timestamp"] = item._timestamp.timestamp()
171+
if item._end_timestamp:
172+
res["end_timestamp"] = item._end_timestamp.timestamp()
173173

174174
if item._parent_span_id:
175175
res["parent_span_id"] = item._parent_span_id

sentry_sdk/traces.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class StreamedSpan:
245245
"_parent_sampled",
246246
"_start_timestamp",
247247
"_start_timestamp_monotonic_ns",
248-
"_timestamp",
248+
"_end_timestamp",
249249
"_status",
250250
"_scope",
251251
"_previous_span_on_scope",
@@ -292,7 +292,7 @@ def __init__(
292292
self._sample_rate = sample_rate
293293

294294
self._start_timestamp = datetime.now(timezone.utc)
295-
self._timestamp: "Optional[datetime]" = None
295+
self._end_timestamp: "Optional[datetime]" = None
296296

297297
# profiling depends on this value and requires that
298298
# it is measured in nanoseconds
@@ -328,7 +328,7 @@ def __enter__(self) -> "StreamedSpan":
328328
def __exit__(
329329
self, ty: "Optional[Any]", value: "Optional[Any]", tb: "Optional[Any]"
330330
) -> None:
331-
if self._timestamp is not None:
331+
if self._end_timestamp is not None:
332332
# This span is already finished, ignore
333333
return
334334

@@ -362,7 +362,7 @@ def _start(self) -> None:
362362
self._previous_span_on_scope = old_span
363363

364364
def _end(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None:
365-
if self._timestamp is not None:
365+
if self._end_timestamp is not None:
366366
# This span is already finished, ignore.
367367
return
368368

@@ -393,15 +393,15 @@ def _end(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None
393393
pass
394394

395395
if isinstance(end_timestamp, datetime):
396-
self._timestamp = end_timestamp
396+
self._end_timestamp = end_timestamp
397397
else:
398398
logger.debug(
399399
"[Tracing] Failed to set end_timestamp. Using current time instead."
400400
)
401401

402-
if self._timestamp is None:
402+
if self._end_timestamp is None:
403403
elapsed = nanosecond_time() - self._start_timestamp_monotonic_ns
404-
self._timestamp = self._start_timestamp + timedelta(
404+
self._end_timestamp = self._start_timestamp + timedelta(
405405
microseconds=elapsed / 1000
406406
)
407407

@@ -480,8 +480,8 @@ def start_timestamp(self) -> "Optional[datetime]":
480480
return self._start_timestamp
481481

482482
@property
483-
def timestamp(self) -> "Optional[datetime]":
484-
return self._timestamp
483+
def end_timestamp(self) -> "Optional[datetime]":
484+
return self._end_timestamp
485485

486486
def _is_segment(self) -> bool:
487487
return self._segment is self
@@ -700,7 +700,7 @@ def start_timestamp(self) -> "Optional[datetime]":
700700
return None
701701

702702
@property
703-
def timestamp(self) -> "Optional[datetime]":
703+
def end_timestamp(self) -> "Optional[datetime]":
704704
return None
705705

706706

sentry_sdk/tracing_utils.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,12 @@ def add_query_source(
399399
if not should_add_query_source:
400400
return
401401

402-
end_timestamp = (
403-
datetime.now(timezone.utc) if span.timestamp is None else span.timestamp
404-
)
402+
if isinstance(span, StreamedSpan):
403+
end_timestamp = span.end_timestamp
404+
else:
405+
end_timestamp = span.timestamp
406+
407+
end_timestamp = end_timestamp or datetime.now(timezone.utc)
405408

406409
duration = end_timestamp - span.start_timestamp
407410
threshold = client.options.get("db_query_source_threshold_ms", 0)
@@ -442,9 +445,12 @@ def add_http_request_source(
442445
if not should_add_request_source:
443446
return
444447

445-
end_timestamp = (
446-
datetime.now(timezone.utc) if span.timestamp is None else span.timestamp
447-
)
448+
if isinstance(span, StreamedSpan):
449+
end_timestamp = span.end_timestamp
450+
else:
451+
end_timestamp = span.timestamp
452+
453+
end_timestamp = end_timestamp or datetime.now(timezone.utc)
448454

449455
duration = end_timestamp - span.start_timestamp
450456
threshold = client.options.get("http_request_source_threshold_ms", 0)

tests/integrations/sqlalchemy/test_sqlalchemy.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,9 @@ def __init__(self, *args, **kwargs):
939939

940940
if span_streaming:
941941
self.span._start_timestamp = datetime(2024, 1, 1, microsecond=0)
942-
self.span._timestamp = datetime(2024, 1, 1, microsecond=99999)
942+
self.span._end_timestamp = datetime(
943+
2024, 1, 1, microsecond=99999
944+
)
943945
else:
944946
self.span.start_timestamp = datetime(2024, 1, 1, microsecond=0)
945947
self.span.timestamp = datetime(2024, 1, 1, microsecond=99999)
@@ -1003,7 +1005,9 @@ def __init__(self, *args, **kwargs):
10031005

10041006
if span_streaming:
10051007
self.span._start_timestamp = datetime(2024, 1, 1, microsecond=0)
1006-
self.span._timestamp = datetime(2024, 1, 1, microsecond=99999)
1008+
self.span._end_timestamp = datetime(
1009+
2024, 1, 1, microsecond=99999
1010+
)
10071011
else:
10081012
self.span.start_timestamp = datetime(2024, 1, 1, microsecond=0)
10091013
self.span.timestamp = datetime(2024, 1, 1, microsecond=99999)
@@ -1082,7 +1086,7 @@ def __init__(self, *args, **kwargs):
10821086
self.span = span
10831087

10841088
self.span._start_timestamp = datetime(2024, 1, 1, microsecond=0)
1085-
self.span._timestamp = datetime(2024, 1, 1, microsecond=101000)
1089+
self.span._end_timestamp = datetime(2024, 1, 1, microsecond=101000)
10861090

10871091
def __enter__(self):
10881092
return self.span

tests/integrations/stdlib/test_httplib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,9 @@ def test_no_request_source_if_duration_too_short(
873873
def add_http_request_source_with_pinned_timestamps(span):
874874
if span_streaming:
875875
span._start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
876-
span._timestamp = datetime.datetime(2024, 1, 1, microsecond=99999)
876+
span._end_timestamp = datetime.datetime(2024, 1, 1, microsecond=99999)
877877
result = add_http_request_source(span)
878-
span._timestamp = None
878+
span._end_timestamp = None
879879
return result
880880
else:
881881
span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
@@ -947,9 +947,9 @@ def test_request_source_if_duration_over_threshold(
947947
def add_http_request_source_with_pinned_timestamps(span):
948948
if span_streaming:
949949
span._start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
950-
span._timestamp = datetime.datetime(2024, 1, 1, microsecond=100001)
950+
span._end_timestamp = datetime.datetime(2024, 1, 1, microsecond=100001)
951951
result = add_http_request_source(span)
952-
span._timestamp = None
952+
span._end_timestamp = None
953953
return result
954954
else:
955955
span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)

0 commit comments

Comments
 (0)