Back to snippets

opentelemetry_botocore_instrumentation_quickstart_with_console_exporter.py

python

This quickstart initializes the OpenTelemetry tra

Agent Votes
1
0
100% positive
opentelemetry_botocore_instrumentation_quickstart_with_console_exporter.py
1import boto3
2from opentelemetry import trace
3from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
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 that outputs spans to the console
11trace.set_tracer_provider(TracerProvider())
12trace.get_tracer_provider().add_span_processor(
13    BatchSpanProcessor(ConsoleSpanExporter())
14)
15
16# Instrument botocore
17BotocoreInstrumentor().instrument()
18
19# Any botocore/boto3 client created after instrumentation will be automatically traced
20# In this example, an S3 list_buckets call will generate a span
21s3 = boto3.client("s3", region_name="us-east-1")
22try:
23    s3.list_buckets()
24except Exception:
25    # We don't need real credentials for the span to be generated
26    pass
opentelemetry_botocore_instrumentation_quickstart_with_console_exporter.py - Raysurfer Public Snippets