OpenTelemetry instrumentation for AWS Bedrock runtime services providing automatic tracing, metrics, and event emission for AI model invocations
—
Core instrumentation functionality for automatically tracing AWS Bedrock AI model invocations. The BedrockInstrumentor class provides the primary interface for enabling comprehensive OpenTelemetry instrumentation of boto3 Bedrock clients.
Main instrumentor class that enables automatic tracing of Bedrock API calls by wrapping boto3 client creation methods. Inherits from OpenTelemetry's BaseInstrumentor and follows the standard instrumentation lifecycle.
class BedrockInstrumentor(BaseInstrumentor):
"""
An instrumentor for Bedrock's client library.
This class automatically instruments boto3 clients created for the
bedrock-runtime service, adding comprehensive tracing, metrics, and
event emission capabilities.
"""
def __init__(
self,
enrich_token_usage: bool = False,
exception_logger = None,
use_legacy_attributes: bool = True
):
"""
Initialize the Bedrock instrumentor.
Parameters:
- enrich_token_usage: Enable detailed token counting using model-specific APIs
- exception_logger: Custom exception handling callback function
- use_legacy_attributes: Use legacy span attributes instead of semantic conventions
"""
def instrumentation_dependencies(self) -> Collection[str]:
"""
Return the list of packages required for instrumentation.
Returns:
Collection of package specifications that must be installed
"""
def instrument(self, **kwargs) -> None:
"""
Enable instrumentation for Bedrock clients.
Parameters:
- tracer_provider: OpenTelemetry tracer provider
- meter_provider: OpenTelemetry meter provider
- event_logger_provider: OpenTelemetry event logger provider
"""
def uninstrument(self, **kwargs) -> None:
"""
Disable instrumentation and unwrap all instrumented methods.
Removes all instrumentation wrappers and restores original
boto3 client behavior.
"""Global configuration management for controlling instrumentation behavior across all instrumented clients. Configuration is set at instrumentation time and affects all subsequent Bedrock client operations.
class Config:
"""
Global configuration for Bedrock instrumentation.
These settings control the behavior of all instrumented Bedrock clients
and are set when the instrumentor is initialized.
"""
enrich_token_usage: bool
"""Enable detailed token counting using model-specific tokenizer APIs"""
exception_logger: Any
"""Custom exception handling callback for instrumentation errors"""
use_legacy_attributes: bool
"""Control span attribute format - legacy attributes vs semantic conventions"""Functions for checking and controlling instrumentation behavior at runtime.
def is_metrics_enabled() -> bool:
"""
Check if metrics collection is enabled.
Returns:
Boolean indicating if metrics should be collected based on
TRACELOOP_METRICS_ENABLED environment variable (default: true)
"""from opentelemetry.instrumentation.bedrock import BedrockInstrumentor
import boto3
# Enable instrumentation with default settings
BedrockInstrumentor().instrument()
# All bedrock-runtime clients created after this point will be instrumented
client = boto3.client('bedrock-runtime', region_name='us-east-1')from opentelemetry.instrumentation.bedrock import BedrockInstrumentor
from opentelemetry import trace, metrics
import logging
def custom_exception_handler(exception, context):
"""Custom exception handling for instrumentation errors"""
logging.error(f"Bedrock instrumentation error: {exception}")
# Configure custom tracer and meter providers
tracer_provider = trace.get_tracer_provider()
meter_provider = metrics.get_meter_provider()
# Initialize with advanced options
instrumentor = BedrockInstrumentor(
enrich_token_usage=True, # Enable detailed token counting
exception_logger=custom_exception_handler,
use_legacy_attributes=False # Use semantic conventions
)
# Instrument with custom providers
instrumentor.instrument(
tracer_provider=tracer_provider,
meter_provider=meter_provider
)from opentelemetry.instrumentation.bedrock import BedrockInstrumentor, is_metrics_enabled
import os
# Check environment before instrumenting
if os.getenv('ENABLE_BEDROCK_TRACING', 'false').lower() == 'true':
instrumentor = BedrockInstrumentor(
enrich_token_usage=is_metrics_enabled(),
use_legacy_attributes=False
)
instrumentor.instrument()
print("Bedrock instrumentation enabled")
else:
print("Bedrock instrumentation disabled")from opentelemetry.instrumentation.bedrock import BedrockInstrumentor
# Store instrumentor reference for lifecycle management
instrumentor = BedrockInstrumentor()
try:
# Enable instrumentation
instrumentor.instrument()
# Application code using Bedrock clients
# ... (client usage here)
finally:
# Clean up instrumentation on shutdown
instrumentor.uninstrument()The instrumentor automatically wraps the following boto3 methods to add tracing:
When a bedrock-runtime client is created, the following methods are automatically instrumented:
The instrumentation creates spans with the following naming convention:
Spans include comprehensive attributes covering:
The instrumentation requires the following packages:
_instruments = ("boto3 >= 1.28.57",)Additional dependencies are automatically managed:
Install with Tessl CLI
npx tessl i tessl/pypi-opentelemetry-instrumentation-bedrock