|
25 | 25 | from ddtrace._trace.span import _get_64_highest_order_bits_as_hex |
26 | 26 | from ddtrace._trace.span import _get_64_lowest_order_bits_as_int |
27 | 27 | from ddtrace._trace.span import _MetaDictType |
| 28 | +from ddtrace.appsec._constants import APPSEC |
| 29 | +from ddtrace.settings.asm import config as asm_config |
28 | 30 |
|
29 | 31 | from ..constants import AUTO_KEEP |
30 | 32 | from ..constants import AUTO_REJECT |
@@ -230,6 +232,11 @@ def _inject(span_context, headers): |
230 | 232 | log.debug("tried to inject invalid context %r", span_context) |
231 | 233 | return |
232 | 234 |
|
| 235 | + # When in appsec standalone mode, only distributed traces with the `_dd.p.appsec` tag |
| 236 | + # are propagated. If the tag is not present, we should not propagate downstream. |
| 237 | + if asm_config._appsec_standalone_enabled and (APPSEC.PROPAGATION_HEADER not in span_context._meta): |
| 238 | + return |
| 239 | + |
233 | 240 | if span_context.trace_id > _MAX_UINT_64BITS: |
234 | 241 | # set lower order 64 bits in `x-datadog-trace-id` header. For backwards compatibility these |
235 | 242 | # bits should be converted to a base 10 integer. |
@@ -343,6 +350,16 @@ def _extract(headers): |
343 | 350 | if meta: |
344 | 351 | meta = validate_sampling_decision(meta) |
345 | 352 |
|
| 353 | + if asm_config._appsec_standalone_enabled: |
| 354 | + # When in appsec standalone mode, only distributed traces with the `_dd.p.appsec` tag |
| 355 | + # are propagated downstream, however we need 1 trace per minute sent to the backend, so |
| 356 | + # we unset sampling priority so the rate limiter decides. |
| 357 | + if not meta or APPSEC.PROPAGATION_HEADER not in meta: |
| 358 | + sampling_priority = None |
| 359 | + # If the trace has appsec propagation tag, the default priority is user keep |
| 360 | + elif meta and APPSEC.PROPAGATION_HEADER in meta: |
| 361 | + sampling_priority = 2 # type: ignore[assignment] |
| 362 | + |
346 | 363 | return Context( |
347 | 364 | # DEV: Do not allow `0` for trace id or span id, use None instead |
348 | 365 | trace_id=trace_id or None, |
|
0 commit comments