Skip to content

Fix httpx tests #3810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions tests/integrations/httpx/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import responses

import sentry_sdk
from sentry_sdk import capture_message, start_transaction
from sentry_sdk import capture_message, start_span
from sentry_sdk.consts import MATCH_ALL, SPANDATA
from sentry_sdk.integrations.httpx import HttpxIntegration
from tests.conftest import ApproxDict
Expand All @@ -26,7 +26,7 @@ def before_breadcrumb(crumb, hint):
url = "http://example.com/"
responses.add(responses.GET, url, status=200)

with start_transaction():
with start_span():
events = capture_events()

if asyncio.iscoroutinefunction(httpx_client.get):
Expand Down Expand Up @@ -72,11 +72,10 @@ def test_outgoing_trace_headers(sentry_init, httpx_client, capture_envelopes):
url = "http://example.com/"
responses.add(responses.GET, url, status=200)

with start_transaction(
with start_span(
name="/interactions/other-dogs/new-dog",
op="greeting.sniff",
trace_id="01234567890123456789012345678901",
) as transaction:
) as span:
if asyncio.iscoroutinefunction(httpx_client.get):
response = asyncio.get_event_loop().run_until_complete(
httpx_client.get(url)
Expand All @@ -102,7 +101,7 @@ def test_outgoing_trace_headers(sentry_init, httpx_client, capture_envelopes):
(httpx.Client(), httpx.AsyncClient()),
)
def test_outgoing_trace_headers_append_to_baggage(
sentry_init, httpx_client, capture_envelopes
sentry_init, httpx_client, capture_envelopes, SortedBaggage, # noqa: N803
):
sentry_init(
traces_sample_rate=1.0,
Expand All @@ -115,11 +114,10 @@ def test_outgoing_trace_headers_append_to_baggage(
url = "http://example.com/"
responses.add(responses.GET, url, status=200)

with start_transaction(
with start_span(
name="/interactions/other-dogs/new-dog",
op="greeting.sniff",
trace_id="01234567890123456789012345678901",
) as transaction:
):
if asyncio.iscoroutinefunction(httpx_client.get):
response = asyncio.get_event_loop().run_until_complete(
httpx_client.get(url, headers={"baGGage": "custom=data"})
Expand All @@ -130,17 +128,18 @@ def test_outgoing_trace_headers_append_to_baggage(
(envelope,) = envelopes
transaction = envelope.get_transaction_event()
request_span = transaction["spans"][-1]
trace_id = transaction["contexts"]["trace"]["trace_id"]

assert response.request.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction["contexts"]["trace"]["trace_id"],
trace_id=trace_id,
parent_span_id=request_span["span_id"],
sampled=1,
)
assert (
response.request.headers["baggage"]
== "custom=data,sentry-trace_id=01234567890123456789012345678901,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0,sentry-sampled=true"
== SortedBaggage(f"custom=data,sentry-trace_id={trace_id},sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0,sentry-sampled=true")
)


Expand Down Expand Up @@ -274,7 +273,7 @@ def test_option_trace_propagation_targets(
integrations=[HttpxIntegration()],
)

with sentry_sdk.start_transaction(): # Must be in a transaction to propagate headers
with sentry_sdk.start_span(): # Must be in a root span to propagate headers
if asyncio.iscoroutinefunction(httpx_client.get):
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
else:
Expand All @@ -288,7 +287,7 @@ def test_option_trace_propagation_targets(
assert "sentry-trace" not in request_headers


def test_do_not_propagate_outside_transaction(sentry_init, httpx_mock):
def test_propagates_twp_outside_root_span(sentry_init, httpx_mock):
httpx_mock.add_response()

sentry_init(
Expand All @@ -301,7 +300,8 @@ def test_do_not_propagate_outside_transaction(sentry_init, httpx_mock):
httpx_client.get("http://example.com/")

request_headers = httpx_mock.get_request().headers
assert "sentry-trace" not in request_headers
assert "sentry-trace" in request_headers
assert request_headers["sentry-trace"] == sentry_sdk.get_traceparent()
Comment on lines +303 to +304
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why we were expecting to not propagate in the original version of this test case? The updated behavior is what I'd have expected to always be the case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was part of this change
#3070
and I believe this was an unintended side effect of that change...

but now we fixed the span without transaction problem another way with only_if_parent and this is the desired twp behaviour.



@pytest.mark.tests_internal_exceptions
Expand Down Expand Up @@ -352,7 +352,7 @@ def test_span_origin(sentry_init, capture_events, httpx_client):
url = "http://example.com/"
responses.add(responses.GET, url, status=200)

with start_transaction(name="test_transaction"):
with start_span(name="test_root_span"):
if asyncio.iscoroutinefunction(httpx_client.get):
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
else:
Expand Down
Loading