Skip to content

Commit 6977da3

Browse files
andremmxrmx
andauthored
Improve asgi instrumentation example (#3576)
* Improve asgi instrumentation example * Fix indentation of asgi instrumentation example * Improve type hints of asgi instrumentation example * Ignore pylint too-many-lines --------- Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent ca079cb commit 6977da3

File tree

1 file changed

+24
-5
lines changed
  • instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi

1 file changed

+24
-5
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
# pylint: disable=too-many-locals
14+
# pylint: disable=too-many-locals,too-many-lines
1515

1616
"""
1717
The opentelemetry-instrumentation-asgi package provides an ASGI middleware that can be used
@@ -81,19 +81,38 @@ async def hello():
8181
8282
.. code-block:: python
8383
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):
85104
if span and span.is_recording():
86105
span.set_attribute("custom_user_attribute_from_request_hook", "some-value")
87106
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]):
89108
if span and span.is_recording():
90109
span.set_attribute("custom_user_attribute_from_client_request_hook", "some-value")
91110
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]):
93112
if span and span.is_recording():
94113
span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
95114
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)
97116
98117
Capture HTTP request and response headers
99118
*****************************************

0 commit comments

Comments
 (0)