Repository navigation
Description
Background
tracelens instruments Python in-process (AST import-hook / sys.monitoring
on 3.12+, plus OpenTelemetry contrib auto-instrumentation), producing function-level
spans (enter/exit, request_id, component, duration_ms) in trace.jsonl.
Everything there is Python-level. It cannot see time spent outside the interpreter:
off-CPU stalls (blocked on locks / IO / network), syscall latency, native page faults,
or block-IO latency — which are exactly what dominates many real latency incidents, and
are the native domain of eBPF.
Proposed design
Add an optional, out-of-process eBPF collector that runs alongside the existing
in-process tracer and emits a second trace to a sidecar file, correlated offline by
(tid, time window). The AST/OTel path is not modified — eBPF is a sibling
collector with a different lifecycle (privileged, out-of-process, Linux-only) that must
fail independently without touching span capture.
tracelens run --ebpf KINDS -- python -m myapp
|
|- AST + OTel (in-process) -> trace.jsonl spans: request_id, component, depth, duration_ms
|- sampler sidecar (threads) -> trace.jsonl kind:"sample" (cpu/alloc/gc/rss)
`- eBPF collector (subprocess) -> trace.jsonl.ebpf.jsonl kind:"ebpf" (offcpu/syscall/pagefault/blockio)
Why a separate file, not merged into trace.jsonl: eBPF is high-volume (syscalls
fire constantly), the downstream loader keys on event: enter/exit, and a crashed BPF
program must not corrupt the span trace. Same reasoning that already puts determinism
capture in its own sidecar.
Sidecar event contract (trace.jsonl.ebpf.jsonl, one JSON object per line):
{"ts":"2026-07-01T12:00:00.123Z","kind":"ebpf","ebpf_kind":"offcpu",
"pid":4321,"tid":4330,"comm":"python","value":152000,"unit":"ns","mono_ns":9876543210}ebpf_kind— one ofoffcpu|syscall|pagefault|blockio.pid,tid— from the kernel;tidis the correlation key.value/unit— kind-specific magnitude (ns|count|bytes);syscallrows addsyscall_nr,ret.mono_ns— monotonic clock for finer alignment.
Correlation: join key is (tid, wall-clock window) — a span S ran on tid T from
t0..t1; attach eBPF events on tid T whose ts falls in [t0, t1], aggregating
offcpu_ns, syscall_ns, syscall_count, pagefault_count, blockio_bytes per span.
(v1 correlates by time window only; the CPython interpreter ident is not the kernel
tid, so a {interpreter_ident: kernel_tid} map is a follow-up for thread-exact joins.)
Graceful degradation (never break the target): the collector is a strict no-op —
logs one line, returns skipped, never raises into the target — when not on Linux, when
bpftrace isn't on PATH, when it lacks privileges (needs root / CAP_BPF), or when the
kernel rejects the generated program. Same "degrade to AST-only" contract the rss
sampler already uses.
Docker synergy: the AST hook produces zero spans when the app runs inside Docker (the
wrapper sits host-side). A host eBPF collector filtered by the container's PID/cgroup sees
the containerized process from the host kernel — filling the exact gap the in-process path
has today.
What to do
Implement the collector per this design (or an updated version), behind an opt-in flag,
without touching the in-process tracer's happy path.
Acceptance criteria
- Opt-in flag produces the sidecar trace on a privileged Linux host.
- Absent
bpftrace/ non-Linux / unprivileged -> clean no-op, target unaffected. - Correlation aggregates (off-CPU ns, syscall ns/count, page faults, block-IO bytes) are
attached per span and surfaced downstream.
Alternatives considered
- Merge eBPF into
trace.jsonl— rejected: volume + blast radius on existing readers. - Replace AST tracing with eBPF — rejected: eBPF has no Python call semantics
(request_id, qualified component names) without a uprobe/symbolication layer, and is
Linux/root-only. The two are complementary, not substitutes. - In-process
perf_event_open— rejected for v1: still Linux/root, and reimplements
whatbpftracealready does well.
Follow-ups (not v1)
- Record a
{interpreter_ident: kernel_tid}map for thread-exact correlation. - Container-PID / cgroup scoping for Dockerized targets.
- Kernel-version-sensitive
syscall/blockioprograms need a probe-capability preflight. - Wire correlated aggregates into the HTML
reportand the captures loader.
Substantial, Linux-focused feature — good for someone comfortable with
bpftrace.
Activity