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