> ## Documentation Index
> Fetch the complete documentation index at: https://abxbus.archivebox.io/llms.txt
> Use this file to discover all available pages before exploring further.

# OtelTracingMiddleware

> Emit OpenTelemetry spans for events and handlers.

`OtelTracingMiddleware` creates event and handler spans with parent-child linking.

## Constructor params

* `tracer` / `Tracer`: optional explicit OpenTelemetry tracer instance.
* `trace_api`: optional explicit `opentelemetry.trace` module in Python/TypeScript.
* `root_span_attributes` / `RootSpanAttributes`: attributes added to top-level event spans.

## Setup with EventBus

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from abxbus import EventBus
    from abxbus.middlewares import OtelTracingMiddleware

    bus = EventBus(
        name='AppBus',
        middlewares=[OtelTracingMiddleware()],
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```ts theme={null}
    import { EventBus } from 'abxbus'
    import { OtelTracingMiddleware } from 'abxbus/OtelTracingMiddleware'

    const bus = new EventBus('AppBus', {
      middlewares: [new OtelTracingMiddleware()],
    })
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    // Not implemented in the core Go module.
    // OpenTelemetry is intentionally not a required Go dependency.
    ```
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    // Not implemented in the Rust crate yet.
    // Use typed event JSON serialization with an external transport for now.
    ```
  </Tab>
</Tabs>

## Setup with Sentry

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import sentry_sdk
    from opentelemetry import trace
    from opentelemetry.sdk.trace import TracerProvider
    from sentry_sdk.integrations.opentelemetry import SentrySpanProcessor

    from abxbus import EventBus
    from abxbus.middlewares import OtelTracingMiddleware

    sentry_sdk.init(
        dsn='https://<public>@<host>/<project>',
        traces_sample_rate=1.0,
    )

    provider = TracerProvider()
    provider.add_span_processor(SentrySpanProcessor())
    trace.set_tracer_provider(provider)

    bus = EventBus(
        name='AppBus',
        middlewares=[OtelTracingMiddleware()],
    )
    ```

    Install requirements:

    ```bash theme={null}
    pip install sentry-sdk opentelemetry-api opentelemetry-sdk
    ```
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    // Not implemented in the Rust crate yet.
    // Wire tracing around typed handlers directly until Rust middleware parity lands.
    ```
  </Tab>
</Tabs>

## Behavior

* Starts an event span when an event starts and ends it on completion.
* Starts one child span per handler execution.
* Records handler exceptions on handler spans.
* Links child events to parent handler spans where available.
* With Sentry OpenTelemetry integration enabled, these spans are exported to Sentry performance traces.
