Skip to content

Commit 4119c9c

Browse files
committed
docs: add docs for aiohttp
1 parent 3e66a13 commit 4119c9c

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
- [Prometheus](https://prometheus.io) metrics
2323
- [OpenTelemetry](https://opentelemetry.io) traces
2424
- [Structlog](https://www.structlog.org/) logging with native **logging** module support
25-
- Integrations with [Litestar](https://litestar.dev), [FastAPI](https://fastapi.tiangolo.com) and [Starlette](https://www.starlette.io)
25+
- Integrations with [Litestar](https://litestar.dev), [FastAPI](https://fastapi.tiangolo.com), [Starlette](https://www.starlette.io) and [Aiohttp](https://docs.aiohttp.org/en/stable/web.html)
2626
- Logging support for [Uvicorn](https://www.uvicorn.org) and [Gunicorn](https://gunicorn.org) with custom **UvicornWorker**
2727

2828
> [!IMPORTANT]

docs/integrations/index.rst

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.. _FastAPI: https://fastapi.tiangolo.com
22
.. _Starlette: https://www.starlette.io
33
.. _Litestar: https://litestar.dev
4+
.. _Aiohttp: https://docs.aiohttp.org/en/stable/web.html
45
.. _real_world: https://github.com/draincoder/asgi-monitor/tree/master/examples/real_world
56

67
.. _integrations:
@@ -13,6 +14,7 @@ Integration with the following frameworks is **now** implemented:
1314
* FastAPI_
1415
* Starlette_
1516
* Litestar_
17+
* Aiohttp_
1618

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

@@ -36,8 +38,7 @@ These frameworks have the **same** integration api, so here I will show you how
3638
import uvicorn
3739
3840
39-
40-
def create_app() -> None:
41+
def create_app() -> FastAPI:
4142
configure_logging(level=logging.INFO, json_format=True, include_trace=False)
4243
4344
resource = Resource.create(
@@ -141,3 +142,64 @@ If you want to use **StructlogPlugin** from ``litestar.plugins.structlog`` toget
141142
:caption: Import processor for extract trace meta
142143
143144
from asgi_monitor.logging.trace_processor import extract_opentelemetry_trace_meta
145+
146+
147+
Aiohttp
148+
====================
149+
150+
Despite the fact that **Aiohttp** does **not support** the **ASGI**-interface, but it is still a popular asynchronous framework and we are happy to support it.
151+
152+
.. code-block:: python
153+
:caption: Configuring monitoring for the Aiohttp
154+
155+
import logging
156+
157+
from aiohttp.web import Application, run_app
158+
from asgi_monitor.integrations.aiohttp import MetricsConfig, TracingConfig, setup_metrics, setup_tracing
159+
from asgi_monitor.logging import configure_logging
160+
from asgi_monitor.logging.aiohttp import TraceAccessLogger
161+
from opentelemetry import trace
162+
from opentelemetry.sdk.resources import Resource
163+
from opentelemetry.sdk.trace import TracerProvider
164+
165+
logger = logging.getLogger(__name__)
166+
167+
168+
def create_app() -> Application:
169+
configure_logging(level=logging.INFO, json_format=True, include_trace=True)
170+
171+
resource = Resource.create(
172+
attributes={
173+
"service.name": "aiohttp",
174+
},
175+
)
176+
tracer_provider = TracerProvider(resource=resource)
177+
trace.set_tracer_provider(tracer_provider)
178+
179+
trace_config = TracingConfig(tracer_provider=tracer_provider)
180+
metrics_config = MetricsConfig(app_name="aiohttp")
181+
182+
app = Application()
183+
184+
setup_tracing(app=app, config=trace_config)
185+
setup_metrics(app=app, config=metrics_config) # Must be configured last
186+
187+
return app
188+
189+
190+
if __name__ == "__main__":
191+
run_app(create_app(), host="127.0.0.1", port=8000, access_log_class=TraceAccessLogger, access_log=logger)
192+
193+
194+
.. important::
195+
196+
``TraceAccessLogger`` add trace meta info in aiohttp request log.
197+
198+
199+
**Aiohttp** tracing is not **based** on an ``opentelemetry-asgi``, so the ``TracingConfig`` looks like this:
200+
201+
1. ``scope_span_details_extractor`` (**Callable[[Request], tuple[str, dict[str, Any]]]**) - Callback which should return a string and a tuple, representing the desired default span name and a dictionary with any additional span attributes to set.
202+
203+
2. ``meter_provider`` (**MeterProvider | None**) - Optional meter provider to use.
204+
205+
3. ``tracer_provider`` (**TracerProvider | None**) - Optional tracer provider to use.

docs/monitoring/tracing.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Configuration
2929

3030
``BaseTracingConfig`` is a configuration class for the OpenTelemetry middleware, and it accepts the following arguments as input:
3131

32-
1. ``exclude_urls_env_key`` (**str**) - Key to use when checking whether a list of excluded urls is passed via ENV. Each integration module uses its own ``TracingConfig``, where the default value of the **metrics_prefix** corresponds to the name of the integration.
32+
1. ``exclude_urls_env_key`` (**str**) - Key to use when checking whether a list of excluded urls is passed via ENV. Each integration module uses its own ``TracingConfig``.
3333

34-
2. ``scope_span_details_extractor`` (**Callable[[Any], tuple[str, dict[str, Any]]]**) - Callback which should return a string and a tuple, representing the desired default span name and a dictionary with any additional span attributes to set. Each integration module uses its own ``TracingConfig``, where the default value of the **metrics_prefix** corresponds to the name of the integration.
34+
2. ``scope_span_details_extractor`` (**Callable[[Any], tuple[str, dict[str, Any]]]**) - Callback which should return a string and a tuple, representing the desired default span name and a dictionary with any additional span attributes to set. Each integration module uses its own ``TracingConfig``.
3535

3636
3. ``server_request_hook_handler`` (**Callable[[Span, dict], None] | None**) - Optional callback which is called with the server span and ASGI scope object for every incoming request.
3737

0 commit comments

Comments
 (0)