Skip to main content

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.

Metadata

Release files for bedrock-tracing 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bedrock-tracing 0.6.0
File Size Uploaded Provenance
bedrock_tracing-0.6.0.tar.gz 6.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for bedrock-tracing 0.6.0
File Interpreter ABI Platform
bedrock_tracing-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.0 kB

Release files / bedrock_tracing-0.6.0.tar.gz

Download URL bedrock_tracing-0.6.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5890e680fe02844b53ff79da45d08e7e63c583c4a0f59280432458387dfa44f0
BLAKE2b-256 checksum
How to use checksums
af42d18fce1b497408205d48768b453b977659ea43a1061f8609b83ce897932d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.2

Release files / bedrock_tracing-0.6.0-py3-none-any.whl

Download URL bedrock_tracing-0.6.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31b2cf448391b183da48afc9001ddbf489fbc50eca4b6bf1ac3d3e24b2df4bb8
BLAKE2b-256 checksum
How to use checksums
39d7b974b9a6c21672ac9f0e1666614f52d0e30a05259ffb886777be8ea22189
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.2

Security for bedrock-tracing 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page