|
4 | 4 |
|
5 | 5 | from opentelemetry.trace.propagation import get_current_span
|
6 | 6 | from opentelemetry.propagators.textmap import DefaultSetter
|
| 7 | +from opentelemetry.semconv.trace import SpanAttributes |
7 | 8 |
|
8 | 9 | import sentry_sdk
|
| 10 | +from sentry_sdk.consts import MATCH_ALL |
9 | 11 | from sentry_sdk.opentelemetry.consts import (
|
10 | 12 | SENTRY_BAGGAGE_KEY,
|
11 | 13 | SENTRY_TRACE_KEY,
|
@@ -208,3 +210,67 @@ def test_inject_head_sdk(sentry_init):
|
208 | 210 | assert carrier["baggage"] == SortedBaggage(
|
209 | 211 | expected_baggage.format(trace_id=span.trace_id)
|
210 | 212 | )
|
| 213 | + |
| 214 | + |
| 215 | +@pytest.mark.parametrize( |
| 216 | + "trace_propagation_targets,url,trace_propagated", |
| 217 | + [ |
| 218 | + # No targets - should not propagate |
| 219 | + ([], "https://example.com/api/users", False), |
| 220 | + (None, "https://example.com/api/users", False), |
| 221 | + # MATCH_ALL - should propagate |
| 222 | + ([MATCH_ALL], "https://example.com/api/users", True), |
| 223 | + # Exact match - should propagate |
| 224 | + (["https://example.com"], "https://example.com/api/users", True), |
| 225 | + (["https://example.com/"], "https://example.com/api/users", True), |
| 226 | + # No match - should not propagate |
| 227 | + (["https://example.com"], "https://other-domain.com/api/users", False), |
| 228 | + (["https://example.com/"], "https://other-domain.com/api/users", False), |
| 229 | + # Regex patterns |
| 230 | + ( |
| 231 | + ["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"], |
| 232 | + "https://good.example.net/api", |
| 233 | + True, |
| 234 | + ), |
| 235 | + ( |
| 236 | + ["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"], |
| 237 | + "https://example.net/api", |
| 238 | + False, |
| 239 | + ), |
| 240 | + # HTTP vs HTTPS |
| 241 | + (["https://example.com"], "http://example.com/api/users", False), |
| 242 | + (["http://example.com"], "https://example.com/api/users", False), |
| 243 | + # Path matching |
| 244 | + (["https://example.com/api"], "https://example.com/api/users", True), |
| 245 | + (["https://example.com/api"], "https://example.com/other/path", False), |
| 246 | + ], |
| 247 | +) |
| 248 | +def test_propagator_trace_propagation_targets( |
| 249 | + sentry_init, |
| 250 | + trace_propagation_targets, |
| 251 | + url, |
| 252 | + trace_propagated, |
| 253 | +): |
| 254 | + """Test that the propagator respects trace_propagation_targets for HTTP spans.""" |
| 255 | + sentry_init( |
| 256 | + trace_propagation_targets=trace_propagation_targets, |
| 257 | + traces_sample_rate=1.0, |
| 258 | + ) |
| 259 | + |
| 260 | + carrier = {} |
| 261 | + setter = DefaultSetter() |
| 262 | + |
| 263 | + # Create a real HTTP span with the test URL |
| 264 | + with sentry_sdk.start_span(name="http.client") as span: |
| 265 | + span.set_attribute(SpanAttributes.HTTP_METHOD, "GET") |
| 266 | + span.set_attribute(SpanAttributes.HTTP_URL, url) |
| 267 | + |
| 268 | + # Test the propagator |
| 269 | + SentryPropagator().inject(carrier, setter=setter) |
| 270 | + |
| 271 | + if trace_propagated: |
| 272 | + assert "sentry-trace" in carrier |
| 273 | + assert "baggage" in carrier |
| 274 | + else: |
| 275 | + assert "sentry-trace" not in carrier |
| 276 | + assert "baggage" not in carrier |
0 commit comments