|
1 | 1 | import pytest |
2 | 2 | import requests |
3 | | -import responses |
4 | 3 | import sentry_sdk |
| 4 | +from http.client import HTTPConnection |
5 | 5 |
|
| 6 | +USE_DEFAULT_TRACES_SAMPLE_RATE = -1 |
6 | 7 |
|
7 | 8 | INCOMING_TRACE_ID = "771a43a4192642f0b136d5159a501700" |
8 | 9 | INCOMING_HEADERS = { |
|
20 | 21 |
|
21 | 22 |
|
22 | 23 | # |
23 | | -# I want to have propper testing of trace propagation. |
| 24 | +# I want to have proper testing of trace propagation. |
24 | 25 | # Testing the matrix of test cases described here: |
25 | 26 | # https://docs.google.com/spreadsheets/d/1IyOTYIC2bwu6HeHrxbLHAm6Lq44atVzf2TDJoPCMDZA/edit?gid=0#gid=0 |
26 | 27 | # |
27 | 28 |
|
28 | 29 |
|
29 | | -@responses.activate |
| 30 | +@pytest.fixture |
| 31 | +def mock_putheader(monkeypatch): |
| 32 | + """ |
| 33 | + Mock HTTPConnection.putheader to capture calls to it. |
| 34 | + """ |
| 35 | + putheader_calls = [] |
| 36 | + original_putheader = HTTPConnection.putheader |
| 37 | + |
| 38 | + def mock_putheader_fn(self, header, value): |
| 39 | + putheader_calls.append((header, value)) |
| 40 | + return original_putheader(self, header, value) |
| 41 | + |
| 42 | + monkeypatch.setattr(HTTPConnection, "putheader", mock_putheader_fn) |
| 43 | + return putheader_calls |
| 44 | + |
| 45 | + |
30 | 46 | @pytest.mark.parametrize( |
31 | 47 | "traces_sample_rate", |
32 | | - [1, 1, 1, 1], # [-1, None, 0, 1.0], |
| 48 | + [ |
| 49 | + USE_DEFAULT_TRACES_SAMPLE_RATE, |
| 50 | + None, |
| 51 | + 0, |
| 52 | + 1.0, |
| 53 | + ], |
33 | 54 | ids=[ |
34 | 55 | "default traces_sample_rate", |
35 | 56 | "traces_sample_rate=None", |
|
38 | 59 | ], |
39 | 60 | ) |
40 | 61 | def test_trace_propagation_no_incoming_trace( |
41 | | - sentry_init, capture_events, traces_sample_rate |
| 62 | + sentry_init, capture_events, mock_putheader, traces_sample_rate |
42 | 63 | ): |
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: |
| 64 | + init_kwargs = {} |
| 65 | + if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE: |
50 | 66 | init_kwargs["traces_sample_rate"] = traces_sample_rate |
51 | | - |
52 | 67 | sentry_init(**init_kwargs) |
53 | 68 |
|
54 | 69 | events = capture_events() |
55 | 70 |
|
56 | | - with sentry_sdk.start_span(op="test", name="test"): |
57 | | - requests.get("http://example.com") |
| 71 | + with sentry_sdk.continue_trace({}): |
| 72 | + with sentry_sdk.start_span(op="test", name="test"): |
| 73 | + requests.get( |
| 74 | + "http://example.com", headers={"custom-header": "custom-value"} |
| 75 | + ) |
58 | 76 |
|
59 | | - # CHECK: performance data (a transaction/span) is sent to sentry |
| 77 | + # CHECK if performance data (a transaction/span) is sent to Sentry |
60 | 78 | if traces_sample_rate == 1: |
61 | 79 | assert len(events) == 1 |
62 | 80 | else: |
63 | 81 | assert len(events) == 0 |
64 | 82 |
|
65 | | - # CHECK: trace information is added to the outgoing request |
66 | | - request = responses.calls[0].request |
| 83 | + outgoing_request_headers = {key: value for key, value in mock_putheader} |
| 84 | + assert "custom-header" in outgoing_request_headers |
| 85 | + |
| 86 | + # CHECK if trace information is added to the outgoing request |
| 87 | + assert "sentry-trace" in outgoing_request_headers |
| 88 | + assert "baggage" in outgoing_request_headers |
| 89 | + |
| 90 | + # CHECK if incoming trace is continued |
| 91 | + # as no incoming data is given to continue_trace() the incoming trace is never continued |
| 92 | + assert INCOMING_TRACE_ID not in outgoing_request_headers["sentry-trace"] |
| 93 | + assert INCOMING_TRACE_ID not in outgoing_request_headers["baggage"] |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.parametrize( |
| 97 | + "traces_sample_rate", |
| 98 | + [ |
| 99 | + USE_DEFAULT_TRACES_SAMPLE_RATE, |
| 100 | + None, |
| 101 | + 0, |
| 102 | + 1.0, |
| 103 | + ], |
| 104 | + ids=[ |
| 105 | + "default traces_sample_rate", |
| 106 | + "traces_sample_rate=None", |
| 107 | + "traces_sample_rate=0", |
| 108 | + "traces_sample_rate=1", |
| 109 | + ], |
| 110 | +) |
| 111 | +def test_trace_propagation_with_incoming_trace( |
| 112 | + sentry_init, capture_events, mock_putheader, traces_sample_rate |
| 113 | +): |
| 114 | + init_kwargs = {} |
| 115 | + if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE: |
| 116 | + init_kwargs["traces_sample_rate"] = traces_sample_rate |
| 117 | + sentry_init(**init_kwargs) |
| 118 | + |
| 119 | + events = capture_events() |
| 120 | + |
| 121 | + with sentry_sdk.continue_trace(INCOMING_HEADERS): |
| 122 | + with sentry_sdk.start_span(op="test", name="test"): |
| 123 | + requests.get( |
| 124 | + "http://example.com", headers={"custom-header": "custom-value"} |
| 125 | + ) |
| 126 | + |
| 127 | + # CHECK if performance data (a transaction/span) is sent to Sentry |
67 | 128 | 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"] |
| 129 | + assert len(events) == 1 |
72 | 130 | else: |
73 | | - assert "sentry-trace" not in request.headers |
74 | | - assert "baggage" not in request.headers |
| 131 | + assert len(events) == 0 |
| 132 | + |
| 133 | + outgoing_request_headers = {key: value for key, value in mock_putheader} |
| 134 | + assert "custom-header" in outgoing_request_headers |
| 135 | + |
| 136 | + # CHECK if trace information is added to the outgoing request |
| 137 | + assert "sentry-trace" in outgoing_request_headers |
| 138 | + assert "baggage" in outgoing_request_headers |
75 | 139 |
|
76 | | - # CHECK: the incoming trace_id and the current trace id do match (or not match) |
| 140 | + # CHECK if incoming trace is continued |
| 141 | + if traces_sample_rate in (0, 1, USE_DEFAULT_TRACES_SAMPLE_RATE): |
| 142 | + # continue the incoming trace |
| 143 | + assert INCOMING_TRACE_ID in outgoing_request_headers["sentry-trace"] |
| 144 | + assert INCOMING_TRACE_ID in outgoing_request_headers["baggage"] |
| 145 | + elif traces_sample_rate is None: |
| 146 | + # do NOT continue the incoming trace |
| 147 | + assert INCOMING_TRACE_ID not in outgoing_request_headers["sentry-trace"] |
| 148 | + assert INCOMING_TRACE_ID not in outgoing_request_headers["baggage"] |
0 commit comments