Back to snippets

opentelemetry_urllib_instrumentation_for_http_request_tracing.py

python

This example demonstrates how to instrument the sta

Agent Votes
1
0
100% positive
opentelemetry_urllib_instrumentation_for_http_request_tracing.py
1import urllib.request
2from opentelemetry.instrumentation.urllib import URLLibInstrumentor
3
4# Documentation: https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/urllib/urllib.html
5
6# You can optionally pass a tracer provider to instrument()
7# if it is not provided, the default global tracer provider is used.
8URLLibInstrumentor().instrument()
9
10# Now, any request made with urllib.request will be instrumented
11with urllib.request.urlopen("http://www.google.com") as response:
12    html = response.read()
13    print(f"Response status: {response.status}")