|
16 | 16 | The opentelemetry-instrumentation-asgi package provides an ASGI middleware that can be used
|
17 | 17 | on any ASGI framework (such as Django-channels / Quart) to track requests
|
18 | 18 | timing through OpenTelemetry.
|
| 19 | +
|
| 20 | +Usage (Quart) |
| 21 | +------------- |
| 22 | +
|
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + from quart import Quart |
| 26 | + from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware |
| 27 | +
|
| 28 | + app = Quart(__name__) |
| 29 | + app.asgi_app = OpenTelemetryMiddleware(app.asgi_app) |
| 30 | +
|
| 31 | + @app.route("/") |
| 32 | + async def hello(): |
| 33 | + return "Hello!" |
| 34 | +
|
| 35 | + if __name__ == "__main__": |
| 36 | + app.run(debug=True) |
| 37 | +
|
| 38 | +
|
| 39 | +Usage (Django 3.0) |
| 40 | +------------------ |
| 41 | +
|
| 42 | +Modify the application's ``asgi.py`` file as shown below. |
| 43 | +
|
| 44 | +.. code-block:: python |
| 45 | +
|
| 46 | + import os |
| 47 | + from django.core.asgi import get_asgi_application |
| 48 | + from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware |
| 49 | +
|
| 50 | + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'asgi_example.settings') |
| 51 | +
|
| 52 | + application = get_asgi_application() |
| 53 | + application = OpenTelemetryMiddleware(application) |
| 54 | +
|
| 55 | +
|
| 56 | +Usage (Raw ASGI) |
| 57 | +---------------- |
| 58 | +
|
| 59 | +.. code-block:: python |
| 60 | +
|
| 61 | + from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware |
| 62 | +
|
| 63 | + app = ... # An ASGI application. |
| 64 | + app = OpenTelemetryMiddleware(app) |
| 65 | +
|
| 66 | +
|
| 67 | +Configuration |
| 68 | +------------- |
| 69 | +
|
| 70 | +Request/Response hooks |
| 71 | +********************** |
| 72 | +
|
| 73 | +Utilize request/reponse hooks to execute custom logic to be performed before/after performing a request. The server request hook takes in a server span and ASGI |
| 74 | +scope object for every incoming request. The client request hook is called with the internal span and an ASGI scope which is sent as a dictionary for when the method recieve is called. |
| 75 | +The client response hook is called with the internal span and an ASGI event which is sent as a dictionary for when the method send is called. |
| 76 | +
|
| 77 | +.. code-block:: python |
| 78 | +
|
| 79 | + def server_request_hook(span: Span, scope: dict): |
| 80 | + if span and span.is_recording(): |
| 81 | + span.set_attribute("custom_user_attribute_from_request_hook", "some-value") |
| 82 | +
|
| 83 | + def client_request_hook(span: Span, scope: dict): |
| 84 | + if span and span.is_recording(): |
| 85 | + span.set_attribute("custom_user_attribute_from_client_request_hook", "some-value") |
| 86 | +
|
| 87 | + def client_response_hook(span: Span, message: dict): |
| 88 | + if span and span.is_recording(): |
| 89 | + span.set_attribute("custom_user_attribute_from_response_hook", "some-value") |
| 90 | +
|
| 91 | + OpenTelemetryMiddleware().(application, server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook) |
| 92 | +
|
| 93 | +API |
| 94 | +--- |
19 | 95 | """
|
20 | 96 |
|
21 | 97 | import typing
|
|
0 commit comments