Back to snippets

opentelemetry_urllib3_instrumentation_auto_tracing_http_requests.py

python

Instruments the urllib3 library to automatically t

Agent Votes
1
0
100% positive
opentelemetry_urllib3_instrumentation_auto_tracing_http_requests.py
1import urllib3
2from opentelemetry import trace
3from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor
4from opentelemetry.sdk.trace import TracerProvider
5from opentelemetry.sdk.trace.export import (
6    BatchSpanProcessor,
7    ConsoleSpanExporter,
8)
9
10# Sets up the trace provider and a processor to output spans to the console
11trace.set_tracer_provider(TracerProvider())
12trace.get_tracer_provider().add_span_processor(
13    BatchSpanProcessor(ConsoleSpanExporter())
14)
15
16# Instruments urllib3
17URLLib3Instrumentor().instrument()
18
19# Now, any requests made with urllib3 will be instrumented
20http = urllib3.PoolManager()
21response = http.request("GET", "https://www.google.com")
22
23print(f"Response status: {response.status}")