|
| 1 | +import pytest |
| 2 | +import requests |
| 3 | +import responses |
| 4 | +import sentry_sdk |
| 5 | + |
| 6 | + |
| 7 | +INCOMING_TRACE_ID = "771a43a4192642f0b136d5159a501700" |
| 8 | +INCOMING_HEADERS = { |
| 9 | + "sentry-trace": f"{INCOMING_TRACE_ID}-1234567890abcdef-1", |
| 10 | + "baggage": ( |
| 11 | + f"sentry-trace_id={INCOMING_TRACE_ID}, " |
| 12 | + "sentry-public_key=frontendpublickey," |
| 13 | + "sentry-sample_rate=0.01337," |
| 14 | + "sentry-sampled=true," |
| 15 | + "sentry-release=myfrontend," |
| 16 | + "sentry-environment=bird," |
| 17 | + "sentry-transaction=bar" |
| 18 | + ), |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +# |
| 23 | +# I want to have propper testing of trace propagation. |
| 24 | +# Testing the matrix of test cases described here: |
| 25 | +# https://docs.google.com/spreadsheets/d/1IyOTYIC2bwu6HeHrxbLHAm6Lq44atVzf2TDJoPCMDZA/edit?gid=0#gid=0 |
| 26 | +# |
| 27 | + |
| 28 | + |
| 29 | +@responses.activate |
| 30 | +@pytest.mark.parametrize( |
| 31 | + "traces_sample_rate", |
| 32 | + [1, 1, 1, 1], # [-1, None, 0, 1.0], |
| 33 | + ids=[ |
| 34 | + "default traces_sample_rate", |
| 35 | + "traces_sample_rate=None", |
| 36 | + "traces_sample_rate=0", |
| 37 | + "traces_sample_rate=1", |
| 38 | + ], |
| 39 | +) |
| 40 | +def test_trace_propagation_no_incoming_trace( |
| 41 | + sentry_init, capture_events, traces_sample_rate |
| 42 | +): |
| 43 | + # TODO: responses mocks away our stdlib putrequest patch, so I need to find a way to mock the request in another way |
| 44 | + responses.add(responses.GET, "http://example.com") |
| 45 | + |
| 46 | + init_kwargs = { |
| 47 | + "debug": True, |
| 48 | + } |
| 49 | + if traces_sample_rate != -1: |
| 50 | + init_kwargs["traces_sample_rate"] = traces_sample_rate |
| 51 | + |
| 52 | + sentry_init(**init_kwargs) |
| 53 | + |
| 54 | + events = capture_events() |
| 55 | + |
| 56 | + with sentry_sdk.start_span(op="test", name="test"): |
| 57 | + requests.get("http://example.com") |
| 58 | + |
| 59 | + # CHECK: performance data (a transaction/span) is sent to sentry |
| 60 | + if traces_sample_rate == 1: |
| 61 | + assert len(events) == 1 |
| 62 | + else: |
| 63 | + assert len(events) == 0 |
| 64 | + |
| 65 | + # CHECK: trace information is added to the outgoing request |
| 66 | + request = responses.calls[0].request |
| 67 | + if traces_sample_rate == 1: |
| 68 | + assert "sentry-trace" in request.headers |
| 69 | + assert "baggage" in request.headers |
| 70 | + assert INCOMING_TRACE_ID not in request.headers["sentry-trace"] |
| 71 | + assert INCOMING_TRACE_ID not in request.headers["baggage"] |
| 72 | + else: |
| 73 | + assert "sentry-trace" not in request.headers |
| 74 | + assert "baggage" not in request.headers |
| 75 | + |
| 76 | + # CHECK: the incoming trace_id and the current trace id do match (or not match) |
0 commit comments