|
5 | 5 | import responses |
6 | 6 |
|
7 | 7 | from sentry_sdk import capture_message, start_transaction |
| 8 | +from sentry_sdk.consts import MATCH_ALL |
8 | 9 | from sentry_sdk.integrations.httpx import HttpxIntegration |
9 | 10 |
|
10 | 11 |
|
@@ -81,3 +82,146 @@ def test_outgoing_trace_headers(sentry_init, httpx_client): |
81 | 82 | parent_span_id=request_span.span_id, |
82 | 83 | sampled=1, |
83 | 84 | ) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize( |
| 88 | + "httpx_client,trace_propagation_targets,url,trace_propagated", |
| 89 | + [ |
| 90 | + [ |
| 91 | + httpx.Client(), |
| 92 | + None, |
| 93 | + "https://example.com/", |
| 94 | + False, |
| 95 | + ], |
| 96 | + [ |
| 97 | + httpx.Client(), |
| 98 | + [], |
| 99 | + "https://example.com/", |
| 100 | + False, |
| 101 | + ], |
| 102 | + [ |
| 103 | + httpx.Client(), |
| 104 | + [MATCH_ALL], |
| 105 | + "https://example.com/", |
| 106 | + True, |
| 107 | + ], |
| 108 | + [ |
| 109 | + httpx.Client(), |
| 110 | + ["https://example.com/"], |
| 111 | + "https://example.com/", |
| 112 | + True, |
| 113 | + ], |
| 114 | + [ |
| 115 | + httpx.Client(), |
| 116 | + ["https://example.com/"], |
| 117 | + "https://example.com", |
| 118 | + False, |
| 119 | + ], |
| 120 | + [ |
| 121 | + httpx.Client(), |
| 122 | + ["https://example.com"], |
| 123 | + "https://example.com", |
| 124 | + True, |
| 125 | + ], |
| 126 | + [ |
| 127 | + httpx.Client(), |
| 128 | + ["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"], |
| 129 | + "https://example.net", |
| 130 | + False, |
| 131 | + ], |
| 132 | + [ |
| 133 | + httpx.Client(), |
| 134 | + ["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"], |
| 135 | + "https://good.example.net", |
| 136 | + True, |
| 137 | + ], |
| 138 | + [ |
| 139 | + httpx.Client(), |
| 140 | + ["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"], |
| 141 | + "https://good.example.net/some/thing", |
| 142 | + True, |
| 143 | + ], |
| 144 | + [ |
| 145 | + httpx.AsyncClient(), |
| 146 | + None, |
| 147 | + "https://example.com/", |
| 148 | + False, |
| 149 | + ], |
| 150 | + [ |
| 151 | + httpx.AsyncClient(), |
| 152 | + [], |
| 153 | + "https://example.com/", |
| 154 | + False, |
| 155 | + ], |
| 156 | + [ |
| 157 | + httpx.AsyncClient(), |
| 158 | + [MATCH_ALL], |
| 159 | + "https://example.com/", |
| 160 | + True, |
| 161 | + ], |
| 162 | + [ |
| 163 | + httpx.AsyncClient(), |
| 164 | + ["https://example.com/"], |
| 165 | + "https://example.com/", |
| 166 | + True, |
| 167 | + ], |
| 168 | + [ |
| 169 | + httpx.AsyncClient(), |
| 170 | + ["https://example.com/"], |
| 171 | + "https://example.com", |
| 172 | + False, |
| 173 | + ], |
| 174 | + [ |
| 175 | + httpx.AsyncClient(), |
| 176 | + ["https://example.com"], |
| 177 | + "https://example.com", |
| 178 | + True, |
| 179 | + ], |
| 180 | + [ |
| 181 | + httpx.AsyncClient(), |
| 182 | + ["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"], |
| 183 | + "https://example.net", |
| 184 | + False, |
| 185 | + ], |
| 186 | + [ |
| 187 | + httpx.AsyncClient(), |
| 188 | + ["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"], |
| 189 | + "https://good.example.net", |
| 190 | + True, |
| 191 | + ], |
| 192 | + [ |
| 193 | + httpx.AsyncClient(), |
| 194 | + ["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"], |
| 195 | + "https://good.example.net/some/thing", |
| 196 | + True, |
| 197 | + ], |
| 198 | + ], |
| 199 | +) |
| 200 | +def test_option_trace_propagation_targets( |
| 201 | + sentry_init, |
| 202 | + httpx_client, |
| 203 | + httpx_mock, # this comes from pytest-httpx |
| 204 | + trace_propagation_targets, |
| 205 | + url, |
| 206 | + trace_propagated, |
| 207 | +): |
| 208 | + httpx_mock.add_response() |
| 209 | + |
| 210 | + sentry_init( |
| 211 | + release="test", |
| 212 | + trace_propagation_targets=trace_propagation_targets, |
| 213 | + traces_sample_rate=1.0, |
| 214 | + integrations=[HttpxIntegration()], |
| 215 | + ) |
| 216 | + |
| 217 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 218 | + asyncio.get_event_loop().run_until_complete(httpx_client.get(url)) |
| 219 | + else: |
| 220 | + httpx_client.get(url) |
| 221 | + |
| 222 | + request_headers = httpx_mock.get_request().headers |
| 223 | + |
| 224 | + if trace_propagated: |
| 225 | + assert "sentry-trace" in request_headers |
| 226 | + else: |
| 227 | + assert "sentry-trace" not in request_headers |
0 commit comments