| 1 |
OpenTelemetry |
| 2 |
============= |
| 3 |
|
| 4 |
Litestar includes optional OpenTelemetry instrumentation that is exported from ``litestar.plugins.opentelemetry``. To use |
| 5 |
this package, you should first install the required dependencies: |
| 6 |
|
| 7 |
.. code-block:: bash |
| 8 |
:caption: as separate package |
| 9 |
|
| 10 |
pip install opentelemetry-instrumentation-asgi |
| 11 |
|
| 12 |
|
| 13 |
.. code-block:: bash |
| 14 |
:caption: as a Litestar extra |
| 15 |
|
| 16 |
pip install 'litestar[opentelemetry]' |
| 17 |
|
| 18 |
Once these requirements are satisfied, you can instrument your Litestar application by creating an instance |
| 19 |
of :class:`OpenTelemetryConfig <litestar.plugins.opentelemetry.OpenTelemetryConfig>` and passing the middleware it creates to |
| 20 |
the Litestar constructor: |
| 21 |
|
| 22 |
.. code-block:: python |
| 23 |
|
| 24 |
from litestar import Litestar |
| 25 |
from litestar.plugins.opentelemetry import OpenTelemetryConfig, OpenTelemetryPlugin |
| 26 |
|
| 27 |
open_telemetry_config = OpenTelemetryConfig() |
| 28 |
|
| 29 |
app = Litestar(plugins=[OpenTelemetryPlugin(open_telemetry_config)]) |
| 30 |
|
| 31 |
The above example will work out of the box if you configure a global ``tracer_provider`` and/or ``metric_provider`` and an |
| 32 |
exporter to use these (see the |
| 33 |
`OpenTelemetry Exporter docs <https://opentelemetry.io/docs/instrumentation/python/exporters/>`_ for further details). |
| 34 |
|
| 35 |
You can also pass con figuration to the ``OpenTelemetryConfig`` telling it which providers to use. Consult |
| 36 |
:class:`reference docs <litestar.plugins.opentelemetry.OpenTelemetryConfig>` regarding the configuration options you can use. |