Skip to content

Commit fcade98

Browse files
sentrivanaclaude
andcommitted
fix(tests): Update httpx2 and strawberry tests for streaming child span guards
httpx2: Wrap HTTP calls in parent span context so child spans are created. strawberry: Remove async/sync branching since both paths now produce 5 spans. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 41192b7 commit fcade98

2 files changed

Lines changed: 78 additions & 98 deletions

File tree

tests/integrations/httpx2/test_httpx2.py

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,11 @@ def test_outgoing_trace_headers_span_streaming(
733733

734734
items = capture_items("span")
735735

736-
if asyncio.iscoroutinefunction(httpx2_client.get):
737-
response = asyncio.run(httpx2_client.get(url))
738-
else:
739-
response = httpx2_client.get(url)
736+
with sentry_sdk.traces.start_span(name="test"):
737+
if asyncio.iscoroutinefunction(httpx2_client.get):
738+
response = asyncio.run(httpx2_client.get(url))
739+
else:
740+
response = httpx2_client.get(url)
740741

741742
sentry_sdk.flush()
742743

@@ -775,12 +776,13 @@ def test_outgoing_trace_headers_append_to_baggage_span_streaming(
775776
items = capture_items("span")
776777

777778
with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=500000):
778-
if asyncio.iscoroutinefunction(httpx2_client.get):
779-
response = asyncio.run(
780-
httpx2_client.get(url, headers={"baGGage": "custom=data"})
781-
)
782-
else:
783-
response = httpx2_client.get(url, headers={"baGGage": "custom=data"})
779+
with sentry_sdk.traces.start_span(name="test"):
780+
if asyncio.iscoroutinefunction(httpx2_client.get):
781+
response = asyncio.run(
782+
httpx2_client.get(url, headers={"baGGage": "custom=data"})
783+
)
784+
else:
785+
response = httpx2_client.get(url, headers={"baGGage": "custom=data"})
784786

785787
sentry_sdk.flush()
786788

@@ -814,10 +816,11 @@ def test_request_source_disabled_span_streaming(
814816

815817
url = "http://example.com/"
816818

817-
if asyncio.iscoroutinefunction(httpx2_client.get):
818-
asyncio.run(httpx2_client.get(url))
819-
else:
820-
httpx2_client.get(url)
819+
with sentry_sdk.traces.start_span(name="test"):
820+
if asyncio.iscoroutinefunction(httpx2_client.get):
821+
asyncio.run(httpx2_client.get(url))
822+
else:
823+
httpx2_client.get(url)
821824

822825
sentry_sdk.flush()
823826

@@ -858,10 +861,11 @@ def test_request_source_enabled_span_streaming(
858861

859862
url = "http://example.com/"
860863

861-
if asyncio.iscoroutinefunction(httpx2_client.get):
862-
asyncio.run(httpx2_client.get(url))
863-
else:
864-
httpx2_client.get(url)
864+
with sentry_sdk.traces.start_span(name="test"):
865+
if asyncio.iscoroutinefunction(httpx2_client.get):
866+
asyncio.run(httpx2_client.get(url))
867+
else:
868+
httpx2_client.get(url)
865869

866870
sentry_sdk.flush()
867871

@@ -894,10 +898,11 @@ def test_request_source_span_streaming(
894898

895899
url = "http://example.com/"
896900

897-
if asyncio.iscoroutinefunction(httpx2_client.get):
898-
asyncio.run(httpx2_client.get(url))
899-
else:
900-
httpx2_client.get(url)
901+
with sentry_sdk.traces.start_span(name="test"):
902+
if asyncio.iscoroutinefunction(httpx2_client.get):
903+
asyncio.run(httpx2_client.get(url))
904+
else:
905+
httpx2_client.get(url)
901906

902907
sentry_sdk.flush()
903908

@@ -951,14 +956,15 @@ def test_request_source_with_module_in_search_path_span_streaming(
951956

952957
url = "http://example.com/"
953958

954-
if asyncio.iscoroutinefunction(httpx2_client.get):
955-
from httpx2_helpers.helpers import async_get_request_with_client
959+
with sentry_sdk.traces.start_span(name="test"):
960+
if asyncio.iscoroutinefunction(httpx2_client.get):
961+
from httpx2_helpers.helpers import async_get_request_with_client
956962

957-
asyncio.run(async_get_request_with_client(httpx2_client, url))
958-
else:
959-
from httpx2_helpers.helpers import get_request_with_client
963+
asyncio.run(async_get_request_with_client(httpx2_client, url))
964+
else:
965+
from httpx2_helpers.helpers import get_request_with_client
960966

961-
get_request_with_client(httpx2_client, url)
967+
get_request_with_client(httpx2_client, url)
962968

963969
sentry_sdk.flush()
964970

@@ -1010,10 +1016,11 @@ def test_no_request_source_if_duration_too_short_span_streaming(
10101016

10111017
url = "http://example.com/"
10121018

1013-
if asyncio.iscoroutinefunction(httpx2_client.get):
1014-
asyncio.run(httpx2_client.get(url))
1015-
else:
1016-
httpx2_client.get(url)
1019+
with sentry_sdk.traces.start_span(name="test"):
1020+
if asyncio.iscoroutinefunction(httpx2_client.get):
1021+
asyncio.run(httpx2_client.get(url))
1022+
else:
1023+
httpx2_client.get(url)
10171024

10181025
sentry_sdk.flush()
10191026

@@ -1047,10 +1054,11 @@ def test_request_source_if_duration_over_threshold_span_streaming(
10471054

10481055
url = "http://example.com/"
10491056

1050-
if asyncio.iscoroutinefunction(httpx2_client.get):
1051-
asyncio.run(httpx2_client.get(url))
1052-
else:
1053-
httpx2_client.get(url)
1057+
with sentry_sdk.traces.start_span(name="test"):
1058+
if asyncio.iscoroutinefunction(httpx2_client.get):
1059+
asyncio.run(httpx2_client.get(url))
1060+
else:
1061+
httpx2_client.get(url)
10541062

10551063
sentry_sdk.flush()
10561064

@@ -1099,10 +1107,11 @@ def test_span_origin_span_streaming(
10991107

11001108
url = "http://example.com/"
11011109

1102-
if asyncio.iscoroutinefunction(httpx2_client.get):
1103-
asyncio.run(httpx2_client.get(url))
1104-
else:
1105-
httpx2_client.get(url)
1110+
with sentry_sdk.traces.start_span(name="test"):
1111+
if asyncio.iscoroutinefunction(httpx2_client.get):
1112+
asyncio.run(httpx2_client.get(url))
1113+
else:
1114+
httpx2_client.get(url)
11061115

11071116
sentry_sdk.flush()
11081117

@@ -1131,10 +1140,11 @@ def test_http_url_attributes_span_streaming(
11311140

11321141
url = "http://example.com/?foo=bar#frag"
11331142

1134-
if asyncio.iscoroutinefunction(httpx2_client.get):
1135-
asyncio.run(httpx2_client.get(url))
1136-
else:
1137-
httpx2_client.get(url)
1143+
with sentry_sdk.traces.start_span(name="test"):
1144+
if asyncio.iscoroutinefunction(httpx2_client.get):
1145+
asyncio.run(httpx2_client.get(url))
1146+
else:
1147+
httpx2_client.get(url)
11381148

11391149
sentry_sdk.flush()
11401150

@@ -1167,10 +1177,11 @@ def test_http_url_attributes_no_query_or_fragment_span_streaming(
11671177

11681178
url = "http://example.com/"
11691179

1170-
if asyncio.iscoroutinefunction(httpx2_client.get):
1171-
asyncio.run(httpx2_client.get(url))
1172-
else:
1173-
httpx2_client.get(url)
1180+
with sentry_sdk.traces.start_span(name="test"):
1181+
if asyncio.iscoroutinefunction(httpx2_client.get):
1182+
asyncio.run(httpx2_client.get(url))
1183+
else:
1184+
httpx2_client.get(url)
11741185

11751186
sentry_sdk.flush()
11761187

@@ -1202,10 +1213,11 @@ def test_http_url_attributes_pii_disabled_span_streaming(
12021213

12031214
url = "http://example.com/?foo=bar#frag"
12041215

1205-
if asyncio.iscoroutinefunction(httpx2_client.get):
1206-
asyncio.run(httpx2_client.get(url))
1207-
else:
1208-
httpx2_client.get(url)
1216+
with sentry_sdk.traces.start_span(name="test"):
1217+
if asyncio.iscoroutinefunction(httpx2_client.get):
1218+
asyncio.run(httpx2_client.get(url))
1219+
else:
1220+
httpx2_client.get(url)
12091221

12101222
sentry_sdk.flush()
12111223

tests/integrations/strawberry/test_strawberry.py

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,8 @@ def test_capture_transaction_on_error(
331331

332332
assert len(error_events) == 1
333333

334-
if async_execution:
335-
# When FastAPI is run, there's an extra span from the httpx client
336-
# so we need to account for that
337-
assert len(spans) == 6
338-
parse_span, validate_span, resolve_span, query_span, segment, _ = spans
339-
else:
340-
assert len(spans) == 5
341-
parse_span, validate_span, resolve_span, query_span, segment = spans
334+
assert len(spans) == 5
335+
parse_span, validate_span, resolve_span, query_span, segment = spans
342336

343337
assert segment["is_segment"] is True
344338
assert segment["name"] == "ErrorQuery"
@@ -473,12 +467,8 @@ def test_capture_transaction_on_success(
473467
sentry_sdk.flush()
474468
spans = [i.payload for i in items]
475469

476-
if async_execution:
477-
assert len(spans) == 6
478-
parse_span, validate_span, resolve_span, query_span, segment, _ = spans
479-
else:
480-
assert len(spans) == 5
481-
parse_span, validate_span, resolve_span, query_span, segment = spans
470+
assert len(spans) == 5
471+
parse_span, validate_span, resolve_span, query_span, segment = spans
482472

483473
assert segment["is_segment"] is True
484474
assert segment["name"] == "GreetingQuery"
@@ -613,12 +603,8 @@ def test_transaction_no_operation_name(
613603
sentry_sdk.flush()
614604
spans = [i.payload for i in items]
615605

616-
if async_execution:
617-
assert len(spans) == 6
618-
parse_span, validate_span, resolve_span, query_span, segment, _ = spans
619-
else:
620-
assert len(spans) == 5
621-
parse_span, validate_span, resolve_span, query_span, segment = spans
606+
assert len(spans) == 5
607+
parse_span, validate_span, resolve_span, query_span, segment = spans
622608

623609
assert segment["is_segment"] is True
624610
if async_execution:
@@ -758,12 +744,8 @@ def test_transaction_mutation(
758744
sentry_sdk.flush()
759745
spans = [i.payload for i in items]
760746

761-
if async_execution:
762-
assert len(spans) == 6
763-
parse_span, validate_span, resolve_span, mutation_span, segment, _ = spans
764-
else:
765-
assert len(spans) == 5
766-
parse_span, validate_span, resolve_span, mutation_span, segment = spans
747+
assert len(spans) == 5
748+
parse_span, validate_span, resolve_span, mutation_span, segment = spans
767749

768750
assert segment["is_segment"] is True
769751
assert segment["name"] == "Change"
@@ -924,12 +906,8 @@ def test_span_origin(
924906
sentry_sdk.flush()
925907
spans = [i.payload for i in items]
926908

927-
if async_execution:
928-
assert len(spans) == 6
929-
parse_span, validate_span, resolve_span, mutation_span, segment, _ = spans
930-
else:
931-
assert len(spans) == 5
932-
parse_span, validate_span, resolve_span, mutation_span, segment = spans
909+
assert len(spans) == 5
910+
parse_span, validate_span, resolve_span, mutation_span, segment = spans
933911

934912
assert segment["is_segment"] is True
935913
if is_flask:
@@ -995,12 +973,8 @@ def test_span_origin2(
995973
sentry_sdk.flush()
996974
spans = [i.payload for i in items]
997975

998-
if async_execution:
999-
assert len(spans) == 6
1000-
parse_span, validate_span, resolve_span, query_span, segment, _ = spans
1001-
else:
1002-
assert len(spans) == 5
1003-
parse_span, validate_span, resolve_span, query_span, segment = spans
976+
assert len(spans) == 5
977+
parse_span, validate_span, resolve_span, query_span, segment = spans
1004978

1005979
assert segment["is_segment"] is True
1006980
if is_flask:
@@ -1066,14 +1040,8 @@ def test_span_origin3(
10661040
sentry_sdk.flush()
10671041
spans = [i.payload for i in items]
10681042

1069-
if async_execution:
1070-
assert len(spans) == 6
1071-
parse_span, validate_span, resolve_span, subscription_span, segment, _ = (
1072-
spans
1073-
)
1074-
else:
1075-
assert len(spans) == 5
1076-
parse_span, validate_span, resolve_span, subscription_span, segment = spans
1043+
assert len(spans) == 5
1044+
parse_span, validate_span, resolve_span, subscription_span, segment = spans
10771045

10781046
assert segment["is_segment"] is True
10791047
if is_flask:

0 commit comments

Comments
 (0)