Skip to content

Commit 7dedfeb

Browse files
committed
Tests
1 parent 61af741 commit 7dedfeb

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

tests/integrations/stdlib/test_httplib.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import platform
22
import sys
3-
3+
import random
44
import pytest
55

66
try:
@@ -122,9 +122,7 @@ def test_httplib_misuse(sentry_init, capture_events, request):
122122
}
123123

124124

125-
def test_outgoing_trace_headers(
126-
sentry_init, monkeypatch, StringContaining # noqa: N803
127-
):
125+
def test_outgoing_trace_headers(sentry_init, monkeypatch):
128126
# HTTPSConnection.send is passed a string containing (among other things)
129127
# the headers on the request. Mock it so we can check the headers, and also
130128
# so it doesn't try to actually talk to the internet.
@@ -176,3 +174,46 @@ def test_outgoing_trace_headers(
176174
assert sorted(request_headers["baggage"].split(",")) == sorted(
177175
expected_outgoing_baggage_items
178176
)
177+
178+
179+
def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch):
180+
# HTTPSConnection.send is passed a string containing (among other things)
181+
# the headers on the request. Mock it so we can check the headers, and also
182+
# so it doesn't try to actually talk to the internet.
183+
mock_send = mock.Mock()
184+
monkeypatch.setattr(HTTPSConnection, "send", mock_send)
185+
186+
# make sure transaction is always sampled
187+
monkeypatch.setattr(random, "random", lambda: 0.1)
188+
189+
sentry_init(traces_sample_rate=0.5, release="foo")
190+
transaction = Transaction.continue_from_headers({})
191+
192+
with start_transaction(transaction=transaction, name="Head SDK tx") as transaction:
193+
HTTPSConnection("www.squirrelchasers.com").request("GET", "/top-chasers")
194+
195+
(request_str,) = mock_send.call_args[0]
196+
request_headers = {}
197+
for line in request_str.decode("utf-8").split("\r\n")[1:]:
198+
if line:
199+
key, val = line.split(": ")
200+
request_headers[key] = val
201+
202+
request_span = transaction._span_recorder.spans[-1]
203+
expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format(
204+
trace_id=transaction.trace_id,
205+
parent_span_id=request_span.span_id,
206+
sampled=1,
207+
)
208+
assert request_headers["sentry-trace"] == expected_sentry_trace
209+
210+
expected_outgoing_baggage_items = [
211+
"sentry-trace_id=%s" % transaction.trace_id,
212+
"sentry-sample_rate=0.5",
213+
"sentry-release=foo",
214+
"sentry-environment=production",
215+
]
216+
217+
assert sorted(request_headers["baggage"].split(",")) == sorted(
218+
expected_outgoing_baggage_items
219+
)

tests/tracing/test_integration_tests.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import weakref
33
import gc
44
import pytest
5+
import random
56

67
from sentry_sdk import (
78
capture_message,
@@ -142,6 +143,61 @@ def test_continue_from_headers(sentry_init, capture_envelopes, sampled, sample_r
142143
assert message_payload["message"] == "hello"
143144

144145

146+
@pytest.mark.parametrize("sample_rate", [0.5, 1.0])
147+
def test_dynamic_sampling_head_sdk_creates_dsc(
148+
sentry_init, capture_envelopes, sample_rate, monkeypatch
149+
):
150+
sentry_init(traces_sample_rate=sample_rate, release="foo")
151+
envelopes = capture_envelopes()
152+
153+
# make sure transaction is sampled for both cases
154+
monkeypatch.setattr(random, "random", lambda: 0.1)
155+
156+
transaction = Transaction.continue_from_headers({}, name="Head SDK tx")
157+
158+
# will create empty mutable baggage
159+
baggage = transaction._baggage
160+
assert baggage
161+
assert baggage.mutable
162+
assert baggage.sentry_items == {}
163+
assert baggage.third_party_items == ""
164+
165+
with start_transaction(transaction):
166+
with start_span(op="foo", description="foodesc"):
167+
pass
168+
169+
# finish will create a new baggage entry
170+
baggage = transaction._baggage
171+
trace_id = transaction.trace_id
172+
173+
assert baggage
174+
assert not baggage.mutable
175+
assert baggage.third_party_items == ""
176+
assert baggage.sentry_items == {
177+
"environment": "production",
178+
"release": "foo",
179+
"sample_rate": str(sample_rate),
180+
"transaction": "Head SDK tx",
181+
"trace_id": trace_id,
182+
}
183+
184+
expected_baggage = (
185+
"sentry-environment=production,sentry-release=foo,sentry-sample_rate=%s,sentry-transaction=Head%%20SDK%%20tx,sentry-trace_id=%s"
186+
% (sample_rate, trace_id)
187+
)
188+
assert sorted(baggage.serialize().split(",")) == sorted(expected_baggage.split(","))
189+
190+
(envelope,) = envelopes
191+
assert envelope.headers["trace"] == baggage.dynamic_sampling_context()
192+
assert envelope.headers["trace"] == {
193+
"environment": "production",
194+
"release": "foo",
195+
"sample_rate": str(sample_rate),
196+
"transaction": "Head SDK tx",
197+
"trace_id": trace_id,
198+
}
199+
200+
145201
@pytest.mark.parametrize(
146202
"args,expected_refcount",
147203
[({"traces_sample_rate": 1.0}, 100), ({"traces_sample_rate": 0.0}, 0)],

0 commit comments

Comments
 (0)