OpenTelemetry Boto Instrumentation

0.61b0 · active · verified Sat Apr 11

This library provides instrumentation for the `boto` (AWS SDK v2) Python library, allowing it to emit traces and metrics compatible with OpenTelemetry. It helps monitor interactions with AWS services made using `boto`. The current version is `0.61b0`, and it's released as part of the `opentelemetry-python-contrib` project, with frequent beta releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instrument the `boto` library. It initializes a simple `ConsoleSpanExporter` to print traces to the console, then calls `BotoInstrumentor().instrument()`. An example `boto` S3 operation is included, which will show traces even if the AWS call itself fails due to missing or invalid credentials. Ensure you have `boto` installed (`pip install boto`) for this to run.

import os
import boto
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.boto import BotoInstrumentor

# Configure OpenTelemetry TracerProvider
provider = TracerProvider()
processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)

# Instrument boto
BotoInstrumentor().instrument()

# Example boto usage (requires AWS credentials or configuration)
# For a real application, ensure AWS credentials are set up via env vars, ~/.aws/credentials, etc.
# Using dummy credentials for a non-functional example to demonstrate instrumentation.
# DO NOT use real credentials in quickstart.
os.environ['AWS_ACCESS_KEY_ID'] = os.environ.get('AWS_ACCESS_KEY_ID', 'DUMMY_KEY_ID')
os.environ['AWS_SECRET_ACCESS_KEY'] = os.environ.get('AWS_SECRET_ACCESS_KEY', 'DUMMY_SECRET_KEY')

print('Attempting to create S3 connection (may fail without real credentials, but traces will appear if instrumentation works):')
try:
    conn = boto.connect_s3(is_secure=False, port=8000, host='localhost') # Use a dummy host to avoid actual calls
    # Attempt to list buckets, this will likely fail but trigger instrumentation
    conn.get_all_buckets()
    print('S3 buckets retrieved (this might be an empty list or error out gracefully)')
except Exception as e:
    print(f'S3 operation failed: {e}')
finally:
    # Explicitly shutdown the provider to ensure all spans are exported
    provider.shutdown()

view raw JSON →