Skip to content

Commit ddb1f79

Browse files
test(profiling): unflake test_stack.py
1 parent 1a4e879 commit ddb1f79

File tree

2 files changed

+121
-62
lines changed

2 files changed

+121
-62
lines changed

tests/profiling/collector/test_stack.py

Lines changed: 107 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import sys
55
import threading
66
import time
7+
from typing import TYPE_CHECKING
78
from typing import Generator
9+
from typing import List
810
from typing import Tuple
911
from unittest.mock import patch
1012
import uuid
@@ -22,6 +24,10 @@
2224
from tests.profiling.collector import test_collector
2325

2426

27+
if TYPE_CHECKING:
28+
from tests.profiling.collector.pprof_pb2 import Sample # pyright: ignore[reportMissingModuleSource]
29+
30+
2531
# Python 3.11.9 is not compatible with gevent, https://github.com/gevent/gevent/issues/2040
2632
# https://github.com/python/cpython/issues/117983
2733
# The fix was not backported to 3.11. The fix was first released in 3.12.5 for
@@ -94,6 +100,7 @@ def test_stack_locations(tmp_path: Path) -> None:
94100
assert ddup.is_available
95101
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
96102
ddup.start()
103+
ddup.upload()
97104

98105
def baz() -> None:
99106
time.sleep(0.1)
@@ -149,6 +156,7 @@ def test_push_span(tmp_path: Path, tracer: Tracer) -> None:
149156
assert ddup.is_available
150157
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
151158
ddup.start()
159+
ddup.upload()
152160

153161
resource = str(uuid.uuid4())
154162
span_type = ext.SpanTypes.WEB
@@ -164,19 +172,27 @@ def test_push_span(tmp_path: Path, tracer: Tracer) -> None:
164172
ddup.upload(tracer=tracer)
165173

166174
profile = pprof_utils.parse_newest_profile(output_filename)
167-
samples = pprof_utils.get_samples_with_label_key(profile, "span id")
168-
assert len(samples) > 0
169-
for sample in samples:
170-
pprof_utils.assert_stack_event(
171-
profile,
172-
sample,
173-
expected_event=pprof_utils.StackEvent(
174-
span_id=span_id,
175-
local_root_span_id=local_root_span_id,
176-
trace_type=span_type,
177-
trace_endpoint=resource,
178-
),
179-
)
175+
samples_with_span_id = pprof_utils.get_samples_with_label_key(profile, "span id")
176+
177+
samples: List[Sample] = []
178+
for sample in samples_with_span_id:
179+
locations = [pprof_utils.get_location_from_id(profile, location_id) for location_id in sample.location_id]
180+
if any(location.filename.endswith("test_stack.py") for location in locations):
181+
samples.append(sample)
182+
183+
assert samples, "No sample found with locations in test_stack.py"
184+
185+
pprof_utils.assert_profile_has_sample(
186+
profile,
187+
samples=samples,
188+
expected_sample=pprof_utils.StackEvent(
189+
span_id=span_id,
190+
local_root_span_id=local_root_span_id,
191+
trace_type=span_type,
192+
trace_endpoint=resource,
193+
),
194+
print_samples_on_failure=True,
195+
)
180196

181197

182198
def test_push_span_unregister_thread(tmp_path: Path, monkeypatch: MonkeyPatch, tracer: Tracer) -> None:
@@ -190,6 +206,7 @@ def test_push_span_unregister_thread(tmp_path: Path, monkeypatch: MonkeyPatch, t
190206
assert ddup.is_available
191207
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
192208
ddup.start()
209+
ddup.upload()
193210

194211
resource = str(uuid.uuid4())
195212
span_type = ext.SpanTypes.WEB
@@ -211,19 +228,25 @@ def target_fun() -> None:
211228
ddup.upload(tracer=tracer)
212229

213230
profile = pprof_utils.parse_newest_profile(output_filename)
214-
samples = pprof_utils.get_samples_with_label_key(profile, "span id")
215-
assert len(samples) > 0
216-
for sample in samples:
217-
pprof_utils.assert_stack_event(
218-
profile,
219-
sample,
220-
expected_event=pprof_utils.StackEvent(
221-
span_id=span_id,
222-
local_root_span_id=local_root_span_id,
223-
trace_type=span_type,
224-
trace_endpoint=resource,
225-
),
226-
)
231+
samples_with_span_id = pprof_utils.get_samples_with_label_key(profile, "span id")
232+
samples: List[Sample] = []
233+
for sample in samples_with_span_id:
234+
locations = [pprof_utils.get_location_from_id(profile, location_id) for location_id in sample.location_id]
235+
if any(location.filename.endswith("test_stack.py") for location in locations):
236+
samples.append(sample)
237+
238+
assert samples, "No sample found with locations in test_stack.py"
239+
pprof_utils.assert_profile_has_sample(
240+
profile,
241+
samples=samples,
242+
expected_sample=pprof_utils.StackEvent(
243+
span_id=span_id,
244+
local_root_span_id=local_root_span_id,
245+
trace_type=span_type,
246+
trace_endpoint=resource,
247+
),
248+
print_samples_on_failure=True,
249+
)
227250

228251
unregister_thread.assert_called_with(thread_id)
229252

@@ -238,6 +261,7 @@ def test_push_non_web_span(tmp_path: Path, tracer: Tracer) -> None:
238261
assert ddup.is_available
239262
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
240263
ddup.start()
264+
ddup.upload()
241265

242266
resource = str(uuid.uuid4())
243267
span_type = ext.SpanTypes.SQL
@@ -253,19 +277,25 @@ def test_push_non_web_span(tmp_path: Path, tracer: Tracer) -> None:
253277
ddup.upload(tracer=tracer)
254278

255279
profile = pprof_utils.parse_newest_profile(output_filename)
256-
samples = pprof_utils.get_samples_with_label_key(profile, "span id")
257-
assert len(samples) > 0
258-
for sample in samples:
259-
pprof_utils.assert_stack_event(
260-
profile,
261-
sample,
262-
expected_event=pprof_utils.StackEvent(
263-
span_id=span_id,
264-
local_root_span_id=local_root_span_id,
265-
trace_type=span_type,
266-
# trace_endpoint is not set for non-web spans
267-
),
268-
)
280+
samples_with_span_id = pprof_utils.get_samples_with_label_key(profile, "span id")
281+
samples: List[Sample] = []
282+
for sample in samples_with_span_id:
283+
locations = [pprof_utils.get_location_from_id(profile, location_id) for location_id in sample.location_id]
284+
if any(location.filename.endswith("test_stack.py") for location in locations):
285+
samples.append(sample)
286+
287+
assert samples, "No sample found with locations in test_stack.py"
288+
pprof_utils.assert_profile_has_sample(
289+
profile,
290+
samples=samples,
291+
expected_sample=pprof_utils.StackEvent(
292+
span_id=span_id,
293+
local_root_span_id=local_root_span_id,
294+
trace_type=span_type,
295+
# trace_endpoint is not set for non-web spans
296+
),
297+
print_samples_on_failure=True,
298+
)
269299

270300

271301
def test_push_span_none_span_type(tmp_path: Path, tracer: Tracer) -> None:
@@ -277,6 +307,7 @@ def test_push_span_none_span_type(tmp_path: Path, tracer: Tracer) -> None:
277307
assert ddup.is_available
278308
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
279309
ddup.start()
310+
ddup.upload()
280311

281312
tracer._endpoint_call_counter_span_processor.enable()
282313

@@ -295,19 +326,25 @@ def test_push_span_none_span_type(tmp_path: Path, tracer: Tracer) -> None:
295326
ddup.upload(tracer=tracer)
296327

297328
profile = pprof_utils.parse_newest_profile(output_filename)
298-
samples = pprof_utils.get_samples_with_label_key(profile, "span id")
299-
assert len(samples) > 0
300-
for sample in samples:
301-
pprof_utils.assert_stack_event(
302-
profile,
303-
sample,
304-
expected_event=pprof_utils.StackEvent(
305-
span_id=span_id,
306-
local_root_span_id=local_root_span_id,
307-
# span_type is None
308-
# trace_endpoint is not set for non-web spans
309-
),
310-
)
329+
samples_with_span_id = pprof_utils.get_samples_with_label_key(profile, "span id")
330+
samples: List[Sample] = []
331+
for sample in samples_with_span_id:
332+
locations = [pprof_utils.get_location_from_id(profile, location_id) for location_id in sample.location_id]
333+
if any(location.filename.endswith("test_stack.py") for location in locations):
334+
samples.append(sample)
335+
336+
assert samples, "No sample found with locations in test_stack.py"
337+
pprof_utils.assert_profile_has_sample(
338+
profile,
339+
samples=samples,
340+
expected_sample=pprof_utils.StackEvent(
341+
span_id=span_id,
342+
local_root_span_id=local_root_span_id,
343+
# span_type is None
344+
# trace_endpoint is not set for non-web spans
345+
),
346+
print_samples_on_failure=True,
347+
)
311348

312349

313350
def test_exception_collection(tmp_path: Path) -> None:
@@ -318,6 +355,7 @@ def test_exception_collection(tmp_path: Path) -> None:
318355
assert ddup.is_available
319356
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
320357
ddup.start()
358+
ddup.upload()
321359

322360
with stack.StackCollector():
323361
try:
@@ -343,6 +381,7 @@ def test_exception_collection_threads(tmp_path: Path) -> None:
343381
assert ddup.is_available
344382
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
345383
ddup.start()
384+
ddup.upload()
346385

347386
with stack.StackCollector():
348387

@@ -379,6 +418,7 @@ def test_exception_collection_trace(tmp_path: Path, tracer: Tracer) -> None:
379418
assert ddup.is_available
380419
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
381420
ddup.start()
421+
ddup.upload()
382422

383423
with stack.StackCollector(tracer=tracer):
384424
with tracer.trace("foobar", resource="resource", span_type=ext.SpanTypes.WEB):
@@ -412,6 +452,7 @@ def sleep_instance(self) -> None:
412452
assert ddup.is_available
413453
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
414454
ddup.start()
455+
ddup.upload()
415456

416457
with stack.StackCollector():
417458
SomeClass.sleep_class()
@@ -442,7 +483,7 @@ def sleep_instance(self) -> None:
442483
pprof_utils.StackLocation(
443484
function_name="test_collect_once_with_class",
444485
filename="test_stack.py",
445-
line_no=test_collect_once_with_class.__code__.co_firstlineno + 19,
486+
line_no=test_collect_once_with_class.__code__.co_firstlineno + 20,
446487
),
447488
],
448489
),
@@ -472,6 +513,7 @@ def sleep_instance(foobar, self) -> None: # pyright: ignore[reportUnusedParamet
472513
assert ddup.is_available
473514
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
474515
ddup.start()
516+
ddup.upload()
475517

476518
with stack.StackCollector():
477519
SomeClass.sleep_class(123)
@@ -502,7 +544,7 @@ def sleep_instance(foobar, self) -> None: # pyright: ignore[reportUnusedParamet
502544
pprof_utils.StackLocation(
503545
function_name="test_collect_once_with_class_not_right_type",
504546
filename="test_stack.py",
505-
line_no=test_collect_once_with_class_not_right_type.__code__.co_firstlineno + 25,
547+
line_no=test_collect_once_with_class_not_right_type.__code__.co_firstlineno + 26,
506548
),
507549
],
508550
),
@@ -543,6 +585,7 @@ def test_collect_gevent_thread_task() -> None:
543585
assert ddup.is_available
544586
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
545587
ddup.start()
588+
ddup.upload()
546589

547590
# Start some (green)threads
548591
def _dofib() -> None:
@@ -633,6 +676,7 @@ def test_stress_threads_run_as_thread(tmp_path: Path) -> None:
633676
assert ddup.is_available
634677
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
635678
ddup.start()
679+
ddup.upload()
636680

637681
quit_thread = threading.Event()
638682

@@ -672,6 +716,7 @@ def tracer_and_collector(
672716
assert ddup.is_available
673717
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
674718
ddup.start()
719+
ddup.upload()
675720

676721
c = stack.StackCollector(tracer=tracer)
677722
c.start()
@@ -690,6 +735,7 @@ def test_collect_span_id(tracer: Tracer, tmp_path: Path) -> None:
690735
assert ddup.is_available
691736
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
692737
ddup.start()
738+
ddup.upload()
693739

694740
tracer._endpoint_call_counter_span_processor.enable()
695741
with stack.StackCollector(tracer=tracer):
@@ -718,7 +764,7 @@ def test_collect_span_id(tracer: Tracer, tmp_path: Path) -> None:
718764
pprof_utils.StackLocation(
719765
filename=os.path.basename(__file__),
720766
function_name=test_name,
721-
line_no=test_collect_span_id.__code__.co_firstlineno + 15,
767+
line_no=test_collect_span_id.__code__.co_firstlineno + 16,
722768
)
723769
],
724770
),
@@ -732,6 +778,7 @@ def test_collect_span_resource_after_finish(tracer: Tracer, tmp_path: Path, requ
732778

733779
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
734780
ddup.start()
781+
ddup.upload()
735782

736783
tracer._endpoint_call_counter_span_processor.enable()
737784
with stack.StackCollector(tracer=tracer):
@@ -758,7 +805,7 @@ def test_collect_span_resource_after_finish(tracer: Tracer, tmp_path: Path, requ
758805
pprof_utils.StackLocation(
759806
filename=os.path.basename(__file__),
760807
function_name=test_name,
761-
line_no=test_collect_span_resource_after_finish.__code__.co_firstlineno + 14,
808+
line_no=test_collect_span_resource_after_finish.__code__.co_firstlineno + 15,
762809
)
763810
],
764811
),
@@ -773,6 +820,7 @@ def test_resource_not_collected(tmp_path: Path, tracer: Tracer) -> None:
773820
assert ddup.is_available
774821
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
775822
ddup.start()
823+
ddup.upload()
776824

777825
with stack.StackCollector(tracer=tracer):
778826
resource = str(uuid.uuid4())
@@ -794,7 +842,7 @@ def test_resource_not_collected(tmp_path: Path, tracer: Tracer) -> None:
794842
pprof_utils.StackLocation(
795843
filename=os.path.basename(__file__),
796844
function_name=test_name,
797-
line_no=test_resource_not_collected.__code__.co_firstlineno + 13,
845+
line_no=test_resource_not_collected.__code__.co_firstlineno + 14,
798846
)
799847
],
800848
),
@@ -809,6 +857,7 @@ def test_collect_nested_span_id(tmp_path: Path, tracer: Tracer, request: Fixture
809857
assert ddup.is_available
810858
ddup.config(env="test", service=test_name, version="my_version", output_filename=pprof_prefix)
811859
ddup.start()
860+
ddup.upload()
812861

813862
tracer._endpoint_call_counter_span_processor.enable()
814863
with stack.StackCollector(tracer=tracer):
@@ -835,7 +884,7 @@ def test_collect_nested_span_id(tmp_path: Path, tracer: Tracer, request: Fixture
835884
pprof_utils.StackLocation(
836885
filename=os.path.basename(__file__),
837886
function_name=test_name,
838-
line_no=test_collect_nested_span_id.__code__.co_firstlineno + 16,
887+
line_no=test_collect_nested_span_id.__code__.co_firstlineno + 17,
839888
)
840889
],
841890
),

0 commit comments

Comments
 (0)