@@ -491,7 +491,46 @@ with start_span(name="custom1"):
491491 ...
492492```
493493
494-
495494## Scrubbing Data
496495
497- ` <TODO: before_send_span> `
496+ In span streaming mode, the SDK provides a new ` before_send_span ` configuration
497+ option. It accepts a function that takes two arguments, ` span ` and ` hint ` , and
498+ returns a span.
499+
500+ ``` python
501+ import sentry_sdk
502+
503+
504+ def scrub_spans (span , hint ):
505+ if span[" name" ] == " GET /some/endpoint" :
506+ span[" name" ] = " List items"
507+
508+ attributes_to_sanitize = [
509+ " http.request.header.custom-auth" ,
510+ " http.request.header.custom-user-id" ,
511+ ]
512+
513+ for attribute in attributes_to_sanitize:
514+ if span[" attributes" ].get(attribute):
515+ span[" attributes" ][attribute] = " [Sanitized]"
516+
517+ return span
518+
519+
520+ sentry_sdk.init(
521+ _experiments = {
522+ " trace_lifecycle" : " stream" ,
523+ " before_send_span" : scrub_spans,
524+ }
525+ )
526+ ```
527+
528+ The callback must return a span. If the return value is anything other than a
529+ span dictionary, it'll be ignored. ` before_send_span ` can't be used to drop a
530+ span. See the [ Filtering] ( #filtering ) section for that.
531+
532+ ` before_send_span ` can be used to modify a span's name or attributes before it
533+ leaves the SDK, for example to sanitize sensitive values.
534+
535+ Currently, the ` hint ` argument is an empty dictionary, but it might contain
536+ further contextual information in the future.
0 commit comments