|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 |
| -# pylint: disable=too-many-locals |
| 14 | +# pylint: disable=too-many-locals,too-many-lines |
15 | 15 |
|
16 | 16 | """
|
17 | 17 | The opentelemetry-instrumentation-asgi package provides an ASGI middleware that can be used
|
@@ -81,19 +81,38 @@ async def hello():
|
81 | 81 |
|
82 | 82 | .. code-block:: python
|
83 | 83 |
|
84 |
| - def server_request_hook(span: Span, scope: dict[str, Any]): |
| 84 | + from opentelemetry.trace import Span |
| 85 | + from typing import Any |
| 86 | + from asgiref.typing import Scope, ASGIReceiveEvent, ASGISendEvent |
| 87 | + from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware |
| 88 | +
|
| 89 | + async def application(scope: Scope, receive: ASGIReceiveEvent, send: ASGISendEvent): |
| 90 | + await send({ |
| 91 | + 'type': 'http.response.start', |
| 92 | + 'status': 200, |
| 93 | + 'headers': [ |
| 94 | + [b'content-type', b'text/plain'], |
| 95 | + ], |
| 96 | + }) |
| 97 | +
|
| 98 | + await send({ |
| 99 | + 'type': 'http.response.body', |
| 100 | + 'body': b'Hello, world!', |
| 101 | + }) |
| 102 | +
|
| 103 | + def server_request_hook(span: Span, scope: Scope): |
85 | 104 | if span and span.is_recording():
|
86 | 105 | span.set_attribute("custom_user_attribute_from_request_hook", "some-value")
|
87 | 106 |
|
88 |
| - def client_request_hook(span: Span, scope: dict[str, Any], message: dict[str, Any]): |
| 107 | + def client_request_hook(span: Span, scope: Scope, message: dict[str, Any]): |
89 | 108 | if span and span.is_recording():
|
90 | 109 | span.set_attribute("custom_user_attribute_from_client_request_hook", "some-value")
|
91 | 110 |
|
92 |
| - def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, Any]): |
| 111 | + def client_response_hook(span: Span, scope: Scope, message: dict[str, Any]): |
93 | 112 | if span and span.is_recording():
|
94 | 113 | span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
|
95 | 114 |
|
96 |
| - OpenTelemetryMiddleware().(application, server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook) |
| 115 | + OpenTelemetryMiddleware(application, server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook) |
97 | 116 |
|
98 | 117 | Capture HTTP request and response headers
|
99 | 118 | *****************************************
|
|
0 commit comments