@@ -81,6 +81,24 @@ async def hello():
8181
8282.. code-block:: python
8383
84+ from opentelemetry.trace import Span
85+ from typing import Any
86+ from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
87+
88+ async def application(scope: dict[str, Any], receive: Any, send: Any):
89+ await send({
90+ 'type': 'http.response.start',
91+ 'status': 200,
92+ 'headers': [
93+ [b'content-type', b'text/plain'],
94+ ],
95+ })
96+
97+ await send({
98+ 'type': 'http.response.body',
99+ 'body': b'Hello, world!',
100+ })
101+
84102 def server_request_hook(span: Span, scope: dict[str, Any]):
85103 if span and span.is_recording():
86104 span.set_attribute("custom_user_attribute_from_request_hook", "some-value")
@@ -93,7 +111,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
93111 if span and span.is_recording():
94112 span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
95113
96- OpenTelemetryMiddleware().( application, server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
114+ OpenTelemetryMiddleware(application, server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
97115
98116Capture HTTP request and response headers
99117*****************************************
0 commit comments