OpenTelemetry#
Litestar includes optional OpenTelemetry instrumentation that is exported from litestar.contrib.opentelemetry
. To use
this package, you should first install the required dependencies:
pip install opentelemetry-instrumentation-asgi
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, OpenTelemetryPlugin
open_telemetry_config = OpenTelemetryConfig()
app = Litestar(plugins=[OpenTelemetryPlugin(open_telemetry_config)])
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.