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:

  1. sample_rate (spans only)
  2. ignore_errors (errors only)
  3. Automatic scrubbing (all events)
  4. before_send hook (all events)

What it does

  • sample_rate: Keeps a fraction (0.0 to 1.0) of span events. 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, or None to 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.0 to silence errors: sample_rate only applies to span events so crashes are never dropped accidentally.
  • Raising inside before_send: If your before_send callback raises an exception, TraceKit catches it and sends the original event so telemetry isn't lost.