Litestar includes optional OpenTelemetry instrumentation that is exported from litestar.contrib.opentelemetry. To use this package, you should first install the required dependencies:

as separate package#
pip install opentelemetry-instrumentation-asgi
as a Litestar extra#
pip install litestar[opentelemetry]

Once these requirements are satisfied, you can instrument your Litestar application by creating an instance of OpenTelemetryConfig and passing the middleware it creates to the Litestar constructor:

from litestar import Litestar
from litestar.contrib.opentelemetry import OpenTelemetryConfig

open_telemetry_config = OpenTelemetryConfig()

app = Litestar(middleware=[open_telemetry_config.middleware])

The above example will work out of the box if you configure a global tracer_provider and/or metric_provider and an exporter to use these (see the OpenTelemetry Exporter docs for further details).

You can also pass con figuration to the OpenTelemetryConfig telling it which providers to use. Consult reference docs regarding the configuration options you can use.