Skip to content

Commit 272a5d9

Browse files
authored
fix(tests): Fix flaky test_cross_trace_query_with_spans_and_logs (#109572)
## Problem The test was using midnight as a timestamp, which caused timing-related flakiness depending on when the test ran relative to day boundaries. ## Fix Updated the failing test and related tests to use non-midnight timestamps so results are stable regardless of execution time. Fixes GH-106681
1 parent 9a20da1 commit 272a5d9

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

tests/sentry/api/endpoints/test_organization_traces.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class OrganizationTracesEndpointTest(BaseSpansTestCase, APITestCase):
2626
def setUp(self) -> None:
2727
super().setUp()
2828
self.login_as(user=self.user)
29+
# Shared 10am timestamps in the past to avoid midnight flakiness and future timestamps in CI.
30+
self.reference_time_10am = before_now(days=1).replace(
31+
hour=10, minute=0, second=0, microsecond=0
32+
)
33+
self.reference_time_10am_3_days_ago = self.reference_time_10am - timedelta(days=2)
2934

3035
def double_write_segment(
3136
self,
@@ -92,7 +97,7 @@ def create_mock_traces(self):
9297
timestamps = []
9398

9499
# move this 3 days into the past to ensure less flakey tests
95-
now = before_now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=3)
100+
now = self.reference_time_10am_3_days_ago
96101

97102
trace_id_1 = uuid4().hex
98103
timestamps.append(now - timedelta(minutes=10))
@@ -418,7 +423,7 @@ def test_use_first_span_for_name(self) -> None:
418423
trace_id = uuid4().hex
419424
span_id = "1" + uuid4().hex[:15]
420425
parent_span_id = "1" + uuid4().hex[:15]
421-
now = before_now().replace(hour=0, minute=0, second=0, microsecond=0)
426+
now = self.reference_time_10am
422427
ts = now - timedelta(minutes=10)
423428

424429
self.double_write_segment(
@@ -481,7 +486,7 @@ def test_use_root_span_for_name(self) -> None:
481486
trace_id = uuid4().hex
482487
span_id_1 = "1" + uuid4().hex[:15]
483488
span_id_2 = "1" + uuid4().hex[:15]
484-
now = before_now().replace(hour=0, minute=0, second=0, microsecond=0)
489+
now = self.reference_time_10am
485490
ts = now - timedelta(minutes=10)
486491

487492
self.double_write_segment(
@@ -568,7 +573,7 @@ def test_use_pageload_for_name(self) -> None:
568573
trace_id = uuid4().hex
569574
span_id = "1" + uuid4().hex[:15]
570575
parent_span_id = "1" + uuid4().hex[:15]
571-
now = before_now().replace(hour=0, minute=0, second=0, microsecond=0)
576+
now = self.reference_time_10am
572577
ts = now - timedelta(minutes=10)
573578

574579
self.double_write_segment(
@@ -631,7 +636,7 @@ def test_selected_projects(self) -> None:
631636
trace_id = uuid4().hex
632637
span_id = "1" + uuid4().hex[:15]
633638
parent_span_id = "1" + uuid4().hex[:15]
634-
now = before_now().replace(hour=0, minute=0, second=0, microsecond=0)
639+
now = self.reference_time_10am
635640
ts = now - timedelta(minutes=10)
636641
self.create_project()
637642

@@ -695,7 +700,7 @@ def test_case_insensitive_filter(self) -> None:
695700
trace_id = uuid4().hex
696701
span_id = "1" + uuid4().hex[:15]
697702
parent_span_id = "1" + uuid4().hex[:15]
698-
now = before_now().replace(hour=0, minute=0, second=0, microsecond=0)
703+
now = self.reference_time_10am
699704
ts = now - timedelta(minutes=10)
700705

701706
self.double_write_segment(
@@ -756,7 +761,7 @@ def test_case_insensitive_filter(self) -> None:
756761
]
757762

758763
def test_use_separate_referrers(self) -> None:
759-
now = before_now().replace(hour=0, minute=0, second=0, microsecond=0)
764+
now = self.reference_time_10am
760765
start = now - timedelta(days=2)
761766
end = now - timedelta(days=1)
762767
trace_id = uuid4().hex
@@ -932,9 +937,7 @@ def test_matching_tag(self) -> None:
932937
def test_environment_filter(self) -> None:
933938
trace_id = uuid4().hex
934939
span_id = "1" + uuid4().hex[:15]
935-
timestamp = before_now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(
936-
days=3
937-
)
940+
timestamp = self.reference_time_10am_3_days_ago
938941

939942
self.double_write_segment(
940943
project=self.project,
@@ -1159,7 +1162,7 @@ def test_sort_by_timestamp(self) -> None:
11591162

11601163
def test_span_name_as_name(self) -> None:
11611164
project = self.create_project()
1162-
now = before_now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=3)
1165+
now = self.reference_time_10am_3_days_ago
11631166
trace_id = uuid4().hex
11641167

11651168
self.double_write_segment(

tests/snuba/api/endpoints/test_organization_events_timeseries_cross_trace.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ class OrganizationEventsTimeseriesCrossTraceEndpointTest(OrganizationEventsEndpo
1414

1515
def setUp(self) -> None:
1616
super().setUp()
17-
self.day_ago = before_now(days=1).replace(hour=0, minute=0, second=0, microsecond=0)
17+
# Events at 10am (not midnight) to avoid flakiness; query window at midnight so interval=1d yields exactly 2 buckets.
18+
self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
1819
self.ten_mins_ago = self.day_ago
1920
self.nine_mins_ago = self.day_ago + timedelta(minutes=1)
20-
self.start = self.day_ago - timedelta(days=1)
21+
self.start = (self.day_ago - timedelta(days=1)).replace(
22+
hour=0, minute=0, second=0, microsecond=0
23+
)
2124
self.end = self.start + timedelta(days=2)
2225

2326
def test_cross_trace_query_with_logs(self) -> None:

0 commit comments

Comments
 (0)