Performance Tracing
Measure function duration with @trace and arbitrary code blocks with span().
TraceKit records execution timing in milliseconds using time.perf_counter().
What it does
@tracekit.tracewraps a sync or async function, measures its duration, emits aspanevent, and also captures anyExceptionbefore re-raising it.with tracekit.span("name"):times a specific block inside a function and emits aspanevent (without emitting a separate error event).
Minimal example
perf.py
import timeimport tracekit
tracekit.init(output="console", environment="production")
@tracekit.tracedef load_dashboard(user_id: int) -> str: with tracekit.span("db.fetch_profile"): time.sleep(0.015)
with tracekit.span("render.template"): time.sleep(0.008)
return f"dashboard:{user_id}"
@tracekit.trace(name="billing.sync")def sync_invoices() -> None: time.sleep(0.005)
load_dashboard(42)sync_invoices()tracekit.flush()Expected output
output
[TraceKit 2026-11-28 09:56:01] SPAN db.fetch_profile (16.12ms) ok[TraceKit 2026-11-28 09:56:01] SPAN render.template (8.94ms) ok[TraceKit 2026-11-28 09:56:01] SPAN load_dashboard (25.31ms) ok[TraceKit 2026-11-28 09:56:01] SPAN billing.sync (5.42ms) okCustom span names
Use @tracekit.trace(name="custom.name") when you want a cleaner name than the Python function name. Without name=, TraceKit uses func.__name__.
Common mistakes
- Expecting
span()to emit an error event: If code insidewith tracekit.span(...):raises, the span status is recorded as"error", and the exception propagates, butspan()does not emit a separateerrorevent. Use@tracekit.traceorcapture_exception()if you also want anerrorevent. - Forgetting parentheses on custom names: Use
@tracekit.trace(no parens) for default naming, or@tracekit.trace(name="...")(with keywordname=).