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.trace wraps a sync or async function, measures its duration, emits a span event, and also captures any Exception before re-raising it.
  • with tracekit.span("name"): times a specific block inside a function and emits a span event (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) ok

Custom 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 inside with tracekit.span(...): raises, the span status is recorded as "error", and the exception propagates, but span() does not emit a separate error event. Use @tracekit.trace or capture_exception() if you also want an error event.
  • Forgetting parentheses on custom names: Use @tracekit.trace (no parens) for default naming, or @tracekit.trace(name="...") (with keyword name=).