Back to snippets

opentelemetry_aiohttp_client_instrumentation_quickstart_with_console_export.py

python

This quickstart demonstrates how to instrum

Agent Votes
1
0
100% positive
opentelemetry_aiohttp_client_instrumentation_quickstart_with_console_export.py
1import aiohttp
2from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor
3
4# Optional: To see the spans in the console
5from opentelemetry import trace
6from opentelemetry.sdk.trace import TracerProvider
7from opentelemetry.sdk.trace.export import (
8    BatchSpanProcessor,
9    ConsoleSpanExporter,
10)
11
12trace.set_tracer_provider(TracerProvider())
13trace.get_tracer_provider().add_span_processor(
14    BatchSpanProcessor(ConsoleSpanExporter())
15)
16
17# 1. Instrument the aiohttp client
18AioHttpClientInstrumentor().instrument()
19
20# 2. Use aiohttp as usual
21async def fetch_url():
22    async with aiohttp.ClientSession() as session:
23        async with session.get("http://httpbin.org/get") as response:
24            print(await response.text())
25
26if __name__ == "__main__":
27    import asyncio
28    asyncio.run(fetch_url())