Skip to content
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

Fix tests using _span_recorder #3633

Merged
Merged
Show file tree
Hide file tree
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
27 changes: 17 additions & 10 deletions tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,16 @@ async def handler(request):


@pytest.mark.asyncio
async def test_outgoing_trace_headers(sentry_init, aiohttp_raw_server, aiohttp_client):
async def test_outgoing_trace_headers(
sentry_init, aiohttp_raw_server, aiohttp_client, capture_envelopes
):
sentry_init(
integrations=[AioHttpIntegration()],
traces_sample_rate=1.0,
)

envelopes = capture_envelopes()

async def handler(request):
return web.Response(text="OK")

Expand All @@ -536,15 +540,18 @@ async def handler(request):
) as transaction:
client = await aiohttp_client(raw_server)
resp = await client.get("/")
request_span = transaction._span_recorder.spans[-1]

assert resp.request_info.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction.trace_id,
parent_span_id=request_span.span_id,
sampled=1,
)

(envelope,) = envelopes
transaction = envelope.get_transaction_event()
request_span = transaction["spans"][-1]

assert resp.request_info.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction["contexts"]["trace"]["trace_id"],
parent_span_id=request_span["span_id"],
sampled=1,
)


@pytest.mark.asyncio
Expand Down
61 changes: 38 additions & 23 deletions tests/integrations/httpx/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ def before_breadcrumb(crumb, hint):
"httpx_client",
(httpx.Client(), httpx.AsyncClient()),
)
def test_outgoing_trace_headers(sentry_init, httpx_client):
sentry_init(traces_sample_rate=1.0, integrations=[HttpxIntegration()])
def test_outgoing_trace_headers(sentry_init, httpx_client, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
integrations=[HttpxIntegration()],
)

envelopes = capture_envelopes()

url = "http://example.com/"
responses.add(responses.GET, url, status=200)
Expand All @@ -79,27 +84,34 @@ def test_outgoing_trace_headers(sentry_init, httpx_client):
else:
response = httpx_client.get(url)

request_span = transaction._span_recorder.spans[-1]
assert response.request.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction.trace_id,
parent_span_id=request_span.span_id,
sampled=1,
)
(envelope,) = envelopes
transaction = envelope.get_transaction_event()
request_span = transaction["spans"][-1]

assert response.request.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction["contexts"]["trace"]["trace_id"],
parent_span_id=request_span["span_id"],
sampled=1,
)


@pytest.mark.parametrize(
"httpx_client",
(httpx.Client(), httpx.AsyncClient()),
)
def test_outgoing_trace_headers_append_to_baggage(sentry_init, httpx_client):
def test_outgoing_trace_headers_append_to_baggage(
sentry_init, httpx_client, capture_envelopes
):
sentry_init(
traces_sample_rate=1.0,
integrations=[HttpxIntegration()],
release="d08ebdb9309e1b004c6f52202de58a09c2268e42",
)

envelopes = capture_envelopes()

url = "http://example.com/"
responses.add(responses.GET, url, status=200)

Expand All @@ -115,18 +127,21 @@ def test_outgoing_trace_headers_append_to_baggage(sentry_init, httpx_client):
else:
response = httpx_client.get(url, headers={"baGGage": "custom=data"})

request_span = transaction._span_recorder.spans[-1]
assert response.request.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction.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"
)
(envelope,) = envelopes
transaction = envelope.get_transaction_event()
request_span = transaction["spans"][-1]

assert response.request.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction["contexts"]["trace"]["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"
)


@pytest.mark.parametrize(
Expand Down
73 changes: 42 additions & 31 deletions tests/integrations/stdlib/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_httplib_misuse(sentry_init, capture_events, request):
)


def test_outgoing_trace_headers(sentry_init, monkeypatch):
def test_outgoing_trace_headers(sentry_init, monkeypatch, capture_envelopes):
# HTTPSConnection.send is passed a string containing (among other things)
# the headers on the request. Mock it so we can check the headers, and also
# so it doesn't try to actually talk to the internet.
Expand All @@ -139,6 +139,8 @@ def test_outgoing_trace_headers(sentry_init, monkeypatch):

sentry_init(traces_sample_rate=1.0)

envelopes = capture_envelopes()

headers = {}
headers["baggage"] = (
"other-vendor-value-1=foo;bar;baz, sentry-trace_id=771a43a4192642f0b136d5159a501700, "
Expand All @@ -163,25 +165,28 @@ def test_outgoing_trace_headers(sentry_init, monkeypatch):
key, val = line.split(": ")
request_headers[key] = val

request_span = transaction._span_recorder.spans[-1]
expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction.trace_id,
parent_span_id=request_span.span_id,
sampled=1,
)
assert request_headers["sentry-trace"] == expected_sentry_trace
(envelope,) = envelopes
transaction = envelope.get_transaction_event()
request_span = transaction["spans"][-1]

expected_outgoing_baggage = (
"sentry-trace_id=771a43a4192642f0b136d5159a501700,"
"sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
"sentry-sample_rate=0.01337,"
"sentry-user_id=Am%C3%A9lie"
)
expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction["contexts"]["trace"]["trace_id"],
parent_span_id=request_span["span_id"],
sampled=1,
)
assert request_headers["sentry-trace"] == expected_sentry_trace

expected_outgoing_baggage = (
"sentry-trace_id=771a43a4192642f0b136d5159a501700,"
"sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
"sentry-sample_rate=0.01337,"
"sentry-user_id=Am%C3%A9lie"
)

assert request_headers["baggage"] == expected_outgoing_baggage
assert request_headers["baggage"] == expected_outgoing_baggage


def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch):
def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch, capture_envelopes):
# HTTPSConnection.send is passed a string containing (among other things)
# the headers on the request. Mock it so we can check the headers, and also
# so it doesn't try to actually talk to the internet.
Expand All @@ -192,6 +197,9 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch):
monkeypatch.setattr(random, "random", lambda: 0.1)

sentry_init(traces_sample_rate=0.5, release="foo")

envelopes = capture_envelopes()

transaction = Transaction.continue_from_headers({})

with start_transaction(transaction=transaction, name="Head SDK tx") as transaction:
Expand All @@ -204,23 +212,26 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch):
key, val = line.split(": ")
request_headers[key] = val

request_span = transaction._span_recorder.spans[-1]
expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction.trace_id,
parent_span_id=request_span.span_id,
sampled=1,
)
assert request_headers["sentry-trace"] == expected_sentry_trace
(envelope,) = envelopes
transaction = envelope.get_transaction_event()
request_span = transaction["spans"][-1]

expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction["contexts"]["trace"]["trace_id"],
parent_span_id=request_span["span_id"],
sampled=1,
)
assert request_headers["sentry-trace"] == expected_sentry_trace

expected_outgoing_baggage = (
"sentry-trace_id=%s,"
"sentry-environment=production,"
"sentry-release=foo,"
"sentry-sample_rate=0.5,"
"sentry-sampled=%s"
) % (transaction.trace_id, "true" if transaction.sampled else "false")
expected_outgoing_baggage = (
"sentry-trace_id=%s,"
"sentry-environment=production,"
"sentry-release=foo,"
"sentry-sample_rate=0.5,"
"sentry-sampled=%s"
) % (transaction.trace_id, "true" if transaction.sampled else "false")

assert request_headers["baggage"] == expected_outgoing_baggage
assert request_headers["baggage"] == expected_outgoing_baggage


@pytest.mark.parametrize(
Expand Down
Loading