Filtering & PII Scrubbing
Sample spans, ignore expected errors, redact sensitive fields, and customize events with before_send.
Before any event enters the background queue, TraceKit passes it through four steps in order:
sample_rate(spans only)ignore_errors(errors only)- Automatic scrubbing (all events)
before_sendhook (all events)
What it does
sample_rate: Keeps a fraction (0.0to1.0) ofspanevents. Error and message events are never sampled out.ignore_errors: Drops error events matching an exception class (e.g.ValueError) or name string (e.g."TimeoutError").- Scrubbing: Recursively replaces values of sensitive keys with
"[Filtered]"without mutating your original dictionaries. before_send: Calls your function with the scrubbed event dict. Return the modified dict to keep it, orNoneto drop it.
Minimal example
filtering.py
import tracekit
def drop_health_checks(event: dict) -> dict | None: if event.get("span", {}).get("name") == "health_check": return None return event
tracekit.init( output="console", sample_rate=1.0, ignore_errors=[KeyError, "TimeoutError"], before_send=drop_health_checks,)
# Sensitive keys in tags or user fields are scrubbed automatically:tracekit.set_tag("api_key", "sk_live_12345")tracekit.capture_message("Auth check complete")tracekit.flush()Expected output
When written to JSON (e.g. via file:events.jsonl), the scrubbed tag looks like:
events.jsonl
{ "type": "message", "message": "Auth check complete", "tags": { "api_key": "[Filtered]" }}Scrubbed key names
TraceKit matches dictionary keys case-insensitively against these names:
password, passwd, secret, token, api_key, apikey, authorization, auth, cookie, cookies, credit_card, card_number, cvv.
Common mistakes
- Expecting
sample_rate=0.0to silence errors:sample_rateonly applies tospanevents so crashes are never dropped accidentally. - Raising inside
before_send: If yourbefore_sendcallback raises an exception, TraceKit catches it and sends the original event so telemetry isn't lost.