Skip to main content
The Apitally SDK captures OpenTelemetry spans during request handling. This allows you to see exactly what happened during each request, including database queries, HTTP calls to external services, and custom operations. If you have an existing OpenTelemetry setup, the SDK automatically registers itself as a span processor with the global TracerProvider. Otherwise, it creates its own. Spans created within a captured request using the standard OpenTelemetry API or instrumentation libraries will be captured.

Instrument libraries

The SDK provides helper functions in apitally.otel to instrument popular libraries. These are thin wrappers around the official OpenTelemetry instrumentation packages, which need to be installed separately. To get started, first install the required packages for the libraries you want to instrument. For example, to instrument httpx and sqlalchemy:
Then call the instrumentation functions at application startup. Some functions accept an optional client, connection or engine argument to instrument a specific instance instead of all instances globally.

Create custom spans

For custom operations that aren’t covered by library instrumentation, you can create spans manually using the helpers provided by the SDK. These are thin wrappers around the standard OpenTelemetry API, which you can also use directly.

@instrument decorator

Use the @instrument decorator to automatically create a span for a function. It works with both sync and async functions:

span() context manager

Use the span() context manager to trace code blocks within a function:

Capture handled exceptions

Apitally includes exceptions in request traces to help you diagnose server errors. Exceptions that propagate through your web framework are captured automatically. If your application catches an exception and handles it without re-raising it, call apitally.capture_exception() to include it in the current request trace.