|
15 | 15 | from sentry_sdk.utils import ( |
16 | 16 | capture_internal_exceptions, |
17 | 17 | filename_for_module, |
18 | | - Dsn, |
19 | 18 | logger, |
20 | 19 | match_regex_list, |
21 | 20 | qualname_from_function, |
@@ -453,15 +452,23 @@ def from_incoming_data(cls, incoming_data): |
453 | 452 |
|
454 | 453 | sentry_trace_header = normalized_data.get(SENTRY_TRACE_HEADER_NAME) |
455 | 454 | sentrytrace_data = extract_sentrytrace_data(sentry_trace_header) |
| 455 | + |
| 456 | + # nothing to propagate if no sentry-trace |
456 | 457 | if sentrytrace_data is None: |
457 | 458 | return None |
458 | 459 |
|
| 460 | + baggage_header = normalized_data.get(BAGGAGE_HEADER_NAME) |
| 461 | + baggage = ( |
| 462 | + Baggage.from_incoming_header(baggage_header) if baggage_header else None |
| 463 | + ) |
| 464 | + |
| 465 | + if not _should_continue_trace(baggage): |
| 466 | + return None |
| 467 | + |
459 | 468 | propagation_context = PropagationContext() |
460 | 469 | propagation_context.update(sentrytrace_data) |
461 | | - |
462 | | - baggage_header = normalized_data.get(BAGGAGE_HEADER_NAME) |
463 | | - if baggage_header: |
464 | | - propagation_context.baggage = Baggage.from_incoming_header(baggage_header) |
| 470 | + if baggage: |
| 471 | + propagation_context.baggage = baggage |
465 | 472 |
|
466 | 473 | propagation_context._fill_sample_rand() |
467 | 474 |
|
@@ -1230,6 +1237,41 @@ def _set_output_attributes(span, template, send_pii, result): |
1230 | 1237 | span.update_data(_get_output_attributes(template, send_pii, result) or {}) |
1231 | 1238 |
|
1232 | 1239 |
|
| 1240 | +def _should_continue_trace(baggage): |
| 1241 | + # type: (Optional[Baggage]) -> bool |
| 1242 | + """ |
| 1243 | + Check if we should continue the incoming trace according to the strict_trace_continuation spec. |
| 1244 | + https://develop.sentry.dev/sdk/telemetry/traces/#stricttracecontinuation |
| 1245 | + """ |
| 1246 | + |
| 1247 | + client = sentry_sdk.get_client() |
| 1248 | + parsed_dsn = client.parsed_dsn |
| 1249 | + client_org_id = parsed_dsn.org_id if parsed_dsn else None |
| 1250 | + baggage_org_id = baggage.sentry_items.get("org_id") if baggage else None |
| 1251 | + |
| 1252 | + if ( |
| 1253 | + client_org_id is not None |
| 1254 | + and baggage_org_id is not None |
| 1255 | + and client_org_id != baggage_org_id |
| 1256 | + ): |
| 1257 | + logger.debug( |
| 1258 | + f"Starting a new trace because org IDs don't match (incoming baggage org_id: {baggage_org_id}, SDK org_id: {client_org_id})" |
| 1259 | + ) |
| 1260 | + return False |
| 1261 | + |
| 1262 | + strict_trace_continuation = client.options.get("strict_trace_continuation", False) # type: bool |
| 1263 | + if strict_trace_continuation: |
| 1264 | + if (baggage_org_id is not None and client_org_id is None) or ( |
| 1265 | + baggage_org_id is None and client_org_id is not None |
| 1266 | + ): |
| 1267 | + logger.debug( |
| 1268 | + f"Starting a new trace because strict trace continuation is enabled and one org ID is missing (incoming baggage org_id: {baggage_org_id}, SDK org_id: {client_org_id})" |
| 1269 | + ) |
| 1270 | + return False |
| 1271 | + |
| 1272 | + return True |
| 1273 | + |
| 1274 | + |
1233 | 1275 | # Circular imports |
1234 | 1276 | from sentry_sdk.tracing import ( |
1235 | 1277 | BAGGAGE_HEADER_NAME, |
|
0 commit comments