API Reference

Complete signature, parameter, return value, and example reference for every public symbol in tracekit.

Every symbol listed below is exported directly from tracekit (defined in tracekit.__all__).

tracekit.init

Initializes the global TraceKit client and installs sys and threading exception hooks.

def init(    output: str = "console",    *,    environment: str = "production",    release: str | None = None,    sample_rate: float = 1.0,    ignore_errors: list[type[BaseException] | str] | None = None,    before_send: Callable[[dict[str, Any]], dict[str, Any] | None] | None = None,    debug: bool = False,) -> None
  • Returns: None
  • Example: tracekit.init(output="console", environment="production")

tracekit.capture_exception

Manually reports an exception.

def capture_exception(exc: BaseException | None = None) -> None
  • Parameters: exc — exception instance to report. If None, reads the active exception from sys.exc_info().
  • Returns: None (no-op if init() has not been called or no exception is active).
  • Example:
try:    1 / 0except ZeroDivisionError:    tracekit.capture_exception()

tracekit.capture_message

Reports a free-form log/message event.

def capture_message(text: str, level: str = "info") -> None
  • Parameters: text (message text, capped at 1,000 chars), level ("debug", "info", "warning", or "error").
  • Returns: None
  • Example: tracekit.capture_message("Cache warmed", level="info")

tracekit.trace

Decorator that times sync or async def functions and captures any raised Exception before re-raising it.

@overloaddef trace[F: Callable[..., Any]](func: F) -> F: ...
@overloaddef trace(func: None = ..., *, name: str | None = ...) -> Callable[[Any], Any]: ...
  • Parameters: name — optional custom span name; defaults to func.__name__.
  • Returns: Wrapped function preserving signature and return value.
  • Example:
@tracekit.trace(name="checkout.total")def calculate_total(items: list[int]) -> int:    return sum(items)

tracekit.span

Context manager that times a code block and emits a span event (with status "ok" or "error"). Propagates exceptions unchanged without emitting a separate error event.

@contextlib.contextmanagerdef span(name: str) -> Generator[None, None, None]
  • Parameters: name — span name shown in events.
  • Returns: Context manager yielding None.
  • Example:
with tracekit.span("db.query"):    rows = [1, 2, 3]

tracekit.install_asyncio_hook

Installs TraceKit's chained exception handler on an asyncio event loop.

def install_asyncio_hook(loop: Any | None = None) -> None
  • Parameters: loop — optional event loop. If None, uses asyncio.get_event_loop() and silently returns if no loop is available.
  • Returns: None
  • Example: tracekit.install_asyncio_hook(asyncio.get_running_loop())

tracekit.set_user

Sets user context for the current thread or async task (replaces any previous user in that context).

def set_user(id: str | None = None, **fields: Any) -> None
  • Parameters: id — user identifier string, plus any extra keyword fields (e.g. email="alice@example.com").
  • Returns: None
  • Example: tracekit.set_user(id="42", email="alice@example.com")

tracekit.set_tag

Sets a string tag in the current context.

def set_tag(key: str, value: str) -> None
  • Parameters: key (str), value (str).
  • Returns: None
  • Example: tracekit.set_tag("region", "us-east-1")

tracekit.add_breadcrumb

Appends a breadcrumb to the current context (newest 20 kept).

def add_breadcrumb(    message: str,    category: str | None = None,    **extra: Any,) -> None
  • Parameters: message (str), category (str | None), **extra (optional fields such as level="warning").
  • Returns: None
  • Example: tracekit.add_breadcrumb("Opened cart", category="ui")

tracekit.flush

Blocks until all currently queued events are delivered to the sink, or timeout seconds elapse.

def flush(timeout: float = 2.0) -> None
  • Parameters: timeout — max seconds to wait.
  • Returns: None
  • Example: tracekit.flush(timeout=2.0)

tracekit.shutdown

Flushes remaining events and stops the background worker thread. Idempotent and safe before init().

def shutdown() -> None
  • Returns: None
  • Example: tracekit.shutdown()

tracekit.TraceKitHandler

Standard library logging.Handler subclass that routes ERROR+ records to events and lower levels to breadcrumbs.

class TraceKitHandler(logging.Handler)
  • Example: logging.getLogger().addHandler(tracekit.TraceKitHandler())