Skip to content

Commit e339e27

Browse files
committed
Improve asgi instrumentation example
1 parent 59cc34e commit e339e27

File tree

1 file changed

+19
-1
lines changed
  • instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi

1 file changed

+19
-1
lines changed

instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
98116
Capture HTTP request and response headers
99117
*****************************************

0 commit comments

Comments
 (0)