Skip to main content

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
  • Automatic session ID generation
  • Integration with environment variables for credentials and configuration

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)

Environment Variables

Ensure the following environment variables are set before using the library:

  • AWS_ACCESS_KEY: Your AWS access key
  • AWS_SECRET_KEY: Your AWS secret key
  • AWS_REGION: The AWS region
  • BEDROCK_AGENT_ALIAS_ID: The Bedrock Agent Alias ID

License

bedrock_tracing is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bedrock_tracing-0.2.3.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bedrock_tracing-0.2.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file bedrock_tracing-0.2.3.tar.gz.

File metadata

  • Download URL: bedrock_tracing-0.2.3.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for bedrock_tracing-0.2.3.tar.gz
Algorithm Hash digest
SHA256 0c301fa9f9df85d5fe22fc21cc9abc466c7bc56a5bffdbdfbe6826300897ab3f
MD5 19a0aa01cb11ae8b9ef67c92d7953e23
BLAKE2b-256 70593844bba1fd8611e7850bdd64044d9ef66dd6ddb1f9b359a69ef7ebc6475d

See more details on using hashes here.

File details

Details for the file bedrock_tracing-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for bedrock_tracing-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 36263603d2ce8c5e07637bab9bc6d877618ffb4cc73e2b4ce302d95633faa784
MD5 0c2b9ca078185ff16bd460340d554b3b
BLAKE2b-256 178bb95e7372dff51fc533dffba46ac2bab9986e475c2c6533d28f290752c2b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page