Skip to main content

AWS Bedrock integration for telemetry.dev Python SDK

Project description

telemetry-dev-bedrock

AWS Bedrock instrumentation for the telemetry.dev Python SDK.

This package instruments boto3/botocore clients for:

  • bedrock-runtime
    • Converse
    • ConverseStream
    • InvokeModel
    • InvokeModelWithResponseStream
    • ApplyGuardrail
  • bedrock-agent-runtime
    • InvokeAgent
    • InvokeInlineAgent
    • Retrieve
    • RetrieveAndGenerate
    • RetrieveAndGenerateStream
    • InvokeFlow

Telemetry is emitted through telemetry_dev.start_span. No AWS request parameters are mutated.

Install

uv add telemetry-dev telemetry-dev-bedrock boto3

or with pip:

pip install telemetry-dev telemetry-dev-bedrock boto3

Quickstart

import os
import boto3
import telemetry_dev
from telemetry_dev_bedrock import wrap_bedrock

telemetry_dev.init(
    api_key=os.getenv("TELEMETRY_DEV_API_KEY"),
    service_name="bedrock-app",
    environment="production",
)

bedrock = wrap_bedrock(boto3.client("bedrock-runtime", region_name="us-east-1"))

bedrock.converse(
    modelId="anthropic.claude-3-5-haiku-20241022-v1:0",
    messages=[{"role": "user", "content": [{"text": "Hello"}]}],
)

telemetry_dev.shutdown()

wrap_bedrock accepts either a bedrock-runtime or bedrock-agent-runtime boto3 client. It patches the operation methods on that instance and is idempotent.

Global instrumentation

from telemetry_dev_bedrock import instrument_bedrock, uninstrument_bedrock

instrument_bedrock()
# bedrock-runtime and bedrock-agent-runtime clients created before or after this point are covered.
uninstrument_bedrock()

Global instrumentation wraps botocore.client.BaseClient._make_api_call and filters by service name, so unrelated boto3 clients pass through untouched. Per-client wrap_bedrock() clients are skipped by the global wrapper to avoid double spans.

Options

Option Default Applies to Notes
capture_agent_trace False Agent Runtime streams Aggregates trace usage and counts by default. When enabled, also attaches raw trace events as td.metadata.agent_trace after SDK masking/truncation.
agent = wrap_bedrock(
    boto3.client("bedrock-agent-runtime"),
    capture_agent_trace=True,
)

Signal coverage

All instrumented AWS responses/errors include gen_ai.response.id from the AWS request id, plus aws.http.status_code, aws.request.attempts when retries occurred, and aws.request.total_retry_delay_ms when the SDK exposes them.

Operation Span type Span name Captured fields
Converse generation chat {modelId} normalized input/output messages, system instructions, sampling params, usage/cache usage, finish reason, request id, retry attempts, prompt-router response model, server latency, guardrail metadata
ConverseStream generation chat {modelId} pull-through stream output, time-to-first-chunk, usage/latency from metadata, partial output on early break/error
InvokeModel generation or embedding chat {modelId} / embeddings {modelId} native JSON request/response bodies, provider-native sampling, provider-native usage, embedding output type, HTTP-header token fallback
InvokeModelWithResponseStream generation or embedding chat {modelId} / embeddings {modelId} provider-native chunk text where known, optional final amazon-bedrock-invocationMetrics usage
ApplyGuardrail span apply_guardrail {guardrailIdentifier} guardrail input/output, action, action reason, request id
InvokeAgent / InvokeInlineAgent agent invoke_agent {agentId} / invoke_agent {agentName} user input, streamed answer, session/memory ids, alias id, trace usage aggregation, trace event count, return-control output
Retrieve span retrieve {knowledgeBaseId} query, retrieval results, guardrail action, result count metadata
RetrieveAndGenerate / stream generation retrieve_and_generate {modelArn basename} input text, generated text, citations count, session id, guardrail action
InvokeFlow agent invoke_flow {flowIdentifier} inputs, flow output events, completion reason

StreamingBody behavior

InvokeModel returns a botocore StreamingBody. The integration reads it once to capture output and token usage, then replaces it with a new StreamingBody over the same bytes. Caller code can still call response["body"].read() normally.

Event streams are wrapped lazily. The wrapper does not pre-read events, preserves backpressure, forwards unknown attributes to the inner stream, and calls close() on the inner stream when closed.

Message normalization

Converse messages follow the OpenTelemetry GenAI non-normative LLM-call examples:

  • text blocks become { "type": "text", "content": ... }
  • toolUse becomes { "type": "tool_call", "id", "name", "arguments" }
  • toolResult becomes { "type": "tool_call_response", "id", "response" }
  • reasoning text becomes { "type": "reasoning", "content" }
  • image/document/video/audio bytes become blob parts without byte content
  • S3/URI sources become { "type": "uri", "uri", "modality" }

InvokeModel captures provider-native JSON bodies verbatim. Non-JSON bodies are not captured.

Semantics and guarantees

  • Provider is emitted as amazon-bedrock. This intentionally differs from the OpenTelemetry registry value aws.bedrock so telemetry.dev pricing keys match Bedrock model IDs exactly. The raw caller modelId, modelArn, or foundationModel is emitted as the model.
  • Instrumentation is fail-open. Normalization, span updates, and span endings are guarded so telemetry failures do not break caller code.
  • Requests are never modified.
  • Streams end spans exactly once. Exhaustion records full output and finish reason; early break, close(), GeneratorExit, or stream errors record partial output.
  • boto3 has no async client. aiobotocore is a separate package and is out of scope.

Coverage gaps

The following calls pass through unless a supported operation above is used:

  • CountTokens
  • InvokeGuardrailChecks
  • InvokeModelWithBidirectionalStream
  • StartAsyncInvoke, GetAsyncInvoke, ListAsyncInvokes
  • Agent Runtime session CRUD
  • Rerank
  • GenerateQuery
  • OptimizePrompt
  • AgenticRetrieveStream

Provider-native InvokeModel usage is best-effort. Anthropic Claude, Amazon Titan/Nova, Meta Llama, and Titan embeddings expose known token fields. Cohere and Mistral text models do not consistently include usage in the JSON body; spans still capture request/response bodies and finish reasons where present.

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

telemetry_dev_bedrock-0.1.0.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

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

telemetry_dev_bedrock-0.1.0-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file telemetry_dev_bedrock-0.1.0.tar.gz.

File metadata

  • Download URL: telemetry_dev_bedrock-0.1.0.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for telemetry_dev_bedrock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 979eddf12b79dda50266896caac837be1b52c4cfd5c6354bedc33f196caf80b5
MD5 84e1612744adcfd8c864472f1740b52a
BLAKE2b-256 c8d292cfe0853b6b0b85ad6486a31ecb66306c5bf99c29e44bd61450c4a29586

See more details on using hashes here.

File details

Details for the file telemetry_dev_bedrock-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: telemetry_dev_bedrock-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for telemetry_dev_bedrock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d7f019277147c52adc3b388206f17b7ff592550b9ace800686e5b444462f3d2
MD5 1f12a705cfa3a945cebf223f8cfe1372
BLAKE2b-256 38ad4d63a706f4442c3da379d7de1de5024fea202fed48057e5f4c9e1ccad7a8

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