A Python library for tracing Bedrock agent responses with OpenTelemetry.
Project description
bedrock-tracing
bedrock_tracing is a Python library that provides OpenTelemetry tracing capabilities for AWS Bedrock Agent interactions. It enables automatic tracing of API calls, helping users capture and analyze distributed traces for debugging and observability.
Installation
pip install bedrock_tracing
Features
- OpenTelemetry tracing for AWS Bedrock Agent interactions
- Integration with environment variables for credentials and configuration
@trace_decorator - Tracing Bedrock Agent Responses
The @trace_decorator simplifies tracing for Bedrock agent interactions using OpenTelemetry. It automatically captures relevant trace data, including:
- Session details: session.id, agent.id, and agent.alias_id
- Agent responses: Captures and logs agent-generated text
- Trace events: Processes structured trace data from Bedrock responses
When applied to a function that streams Bedrock agent responses, the decorator ensures each invocation is traced, enriching observability for debugging and performance analysis.
Usage
import os
import uuid
import boto3
from dotenv import load_dotenv
from bedrock_tracing import trace_decorator
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
# Load environment variables
load_dotenv()
def generate_session_id():
return str(uuid.uuid4())
@trace_decorator
def invoke_bedrock_agent(brt, question, session_id):
"""Invokes the Bedrock agent and processes trace data using the decorator."""
response = brt.invoke_agent(
agentId=os.getenv("BEDROCK_AGENT_ID"),
agentAliasId=os.getenv("BEDROCK_AGENT_ALIAS_ID"),
inputText=question,
enableTrace=True,
sessionId=session_id,
endSession=False,
)
return response.get("completion", [])
# Setup OpenTelemetry tracing
resource = Resource(attributes={
SERVICE_NAME: "bedrock-agent-trace-testing-application"
})
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
# AWS Bedrock Agent client setup
bs = boto3.Session(
aws_access_key_id=os.getenv("AWS_ACCESS_KEY"),
aws_secret_access_key=os.getenv("AWS_SECRET_KEY"),
region_name=os.getenv("AWS_REGION")
)
brt = bs.client("bedrock-agent-runtime")
# Execute agent interaction
session_id = generate_session_id()
question = "Tell me about Oracle integration not exceeding 100 words."
agent_response = invoke_bedrock_agent(brt, question, session_id)
License
bedrock_tracing is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bedrock_tracing-0.2.6.tar.gz.
File metadata
- Download URL: bedrock_tracing-0.2.6.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25b6c94d03e30899167a8e51f0dd6232ffe8d4d05f10c160889f3db1b35f1193
|
|
| MD5 |
ab12646cd64fb40634de5c7ab75a6ac2
|
|
| BLAKE2b-256 |
bc17bb4a88702b2f96b9cb5d7bbc45c5f181b65f8d017733b6d5b516277940b9
|
File details
Details for the file bedrock_tracing-0.2.6-py3-none-any.whl.
File metadata
- Download URL: bedrock_tracing-0.2.6-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77e76fcf1c825479e9aadcae3b5c085afa7f93e3bc366da3e240345536ea0fbf
|
|
| MD5 |
2a8540dd492118a6fd29de69d8dffb02
|
|
| BLAKE2b-256 |
7854c3aebf153ee64855ab65d953d214cfb1b6d8552d39e4dc227a459e325a0f
|