Skip to content

Commit 6c8590d

Browse files
committed
Add litestar integrations docs
1 parent 7d928f9 commit 6c8590d

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

docs/integrations/index.rst

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.. _FastAPI: https://fastapi.tiangolo.com
22
.. _Starlette: https://www.starlette.io
3+
.. _Litestar: https://litestar.dev
34
.. _real_world: https://github.com/draincoder/asgi-monitor/tree/master/examples/real_world
45

56
.. _integrations:
@@ -11,6 +12,7 @@ Integration with the following frameworks is **now** implemented:
1112

1213
* FastAPI_
1314
* Starlette_
15+
* Litestar_
1416

1517
But other integrations are planned in the near future, and you can also implement your own through **PR**.
1618

@@ -65,3 +67,78 @@ These frameworks have the **same** integration api, so here I will show you how
6567
For **Starlette**, simply replace ``fastapi`` with ``starlette`` in the **integration** import line.
6668
6769
Check out the real_world_ example to figure out how it works.
70+
71+
72+
Litestar
73+
====================
74+
75+
.. important::
76+
77+
The API is incompatible with FastAPI and Starlette, the rules for determining the path are different, the type of error is not detected.
78+
79+
Litestar out of the box has many add-ons:
80+
81+
1. **Prometheus** support
82+
2. **OpenTelemetry** support
83+
3. **Structlog** plugin
84+
85+
But they all have disadvantages, for example, you can use only **global metrics** with a **global registry**, metrics are not compatible with the trace context, logs also do not support the trace context.
86+
87+
So **asgi-monitor** is rushing to the rescue.
88+
89+
.. code-block:: python
90+
:caption: Configuring monitoring for the Litestar
91+
92+
import logging
93+
94+
from asgi_monitor.integrations.litestar import (
95+
MetricsConfig,
96+
TracingConfig,
97+
add_metrics_endpoint,
98+
build_metrics_middleware,
99+
build_tracing_middleware,
100+
)
101+
from asgi_monitor.logging import configure_logging
102+
from asgi_monitor.logging.uvicorn import build_uvicorn_log_config
103+
from litestar import Litestar
104+
from opentelemetry import trace
105+
from opentelemetry.sdk.resources import Resource
106+
from opentelemetry.sdk.trace import TracerProvider
107+
import uvicorn
108+
109+
logger = logging.getLogger(__name__)
110+
111+
112+
def create_app() -> Litestar:
113+
configure_logging(level=logging.INFO, json_format=True, include_trace=False)
114+
115+
resource = Resource.create(
116+
attributes={
117+
"service.name": "litestar",
118+
},
119+
)
120+
tracer = TracerProvider(resource=resource)
121+
trace.set_tracer_provider(tracer)
122+
123+
trace_config = TracingConfig(tracer_provider=tracer)
124+
metrics_config = MetricsConfig(app_name="litestar", include_trace_exemplar=True)
125+
126+
middlewares = [build_tracing_middleware(trace_config), build_metrics_middleware(metrics_config)]
127+
128+
app = Litestar([index], middleware=middlewares, logging_config=None)
129+
130+
add_metrics_endpoint(app, metrics_config.registry)
131+
132+
return app
133+
134+
135+
if __name__ == "__main__":
136+
log_config = build_uvicorn_log_config(level=logging.INFO, json_format=True, include_trace=True)
137+
uvicorn.run(create_app(), host="127.0.0.1", port=8000, log_config=log_config)
138+
139+
If you want to use **StructlogPlugin** from ``litestar.plugins.structlog`` together with tracing, you can embed a processor in the structlog processor chain to export the trace context to the log.
140+
141+
.. code-block:: python
142+
:caption: Import processor for extract trace meta
143+
144+
from asgi_monitor.logging.trace_processor import extract_opentelemetry_trace_meta

docs/monitoring/metrics.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ The ``BaseMetricsConfig`` class is used to configure metrics, and it accepts the
5050

5151
4. ``include_trace_exemplar`` (**bool**) - Whether to include trace exemplars in the metrics. Default is ``False``. This is only necessary if **tracing** is configured and metrics are collected in the ``OpenMetrics`` format.
5252

53-
5. ``include_metrics_endpoint`` (**bool**) Whether to include a **/metrics** endpoint. Default is ``True``. Collecting metrics in ``text/plain`` format.
54-
5553
You can also set up a **global** ``prometheus_client.REGISTRY`` in ``MetricsConfig`` to support your **global** metrics,
5654
but it is better to use your own **non-global** registry or leave the **default** registry.
5755

@@ -71,7 +69,7 @@ but it is better to use your own **non-global** registry or leave the **default*
7169
Exporting
7270
~~~~~~~~~~~~~~~~~~
7371

74-
If you are using integration with the web framework, you can add metric exports via the config by setting ``include_metrics_endpoint`` to ``True``. In this case, the metrics will be exported via the **/metrics** endpoint in ``text/plain`` format.
72+
If you are using integration with the web framework, you can add metric exports via the config by setting ``include_metrics_endpoint`` to ``True`` or by explicitly calling ``add_metrics_endpoint``. In this case, the metrics will be exported via the **/metrics** endpoint in ``text/plain`` format.
7573

7674
In case you need to **customize the endpoint**, add **protection**, use the **OpenMetrics** format, or just use **another method for delivering** metrics, then you should use the built-in ``get_latest_metrics`` function.
7775

0 commit comments

Comments
 (0)