Track AWS API calls from Boto3
See which Amazon Web Services (AWS) calls your application makes with Boto3, how long they take, and whether they fail. Each API call becomes a span (one operation with a start and a duration), nested in the trace (the journey of a request) that triggered it.
- The AWS service and operation, such as
S3.ListBuckets - The region, duration, and errors for each API call
- Model and token-usage attributes for supported Amazon Bedrock model calls, plus input/output
token measurements in the
gen_ai.client.token.usagemetric
You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the
setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token
to copy) and, for production or other languages, creates a write token (the credential your app
uses to send data). New to Logfire? Start with Getting Started.
Install logfire with the botocore extra:
pip install 'logfire[botocore]'
uv add 'logfire[botocore]'
Call logfire.instrument_botocore() once, after
logfire.configure() and before making AWS API calls. Botocore is the library Boto3 uses to make
those calls, so the same integration covers both libraries.
import boto3
import logfire
logfire.configure()
logfire.instrument_botocore()
s3 = boto3.client('s3', region_name='us-east-1')
response = s3.list_buckets()
print([bucket['Name'] for bucket in response['Buckets']])
Open the live view and look for the S3.ListBuckets span. Expand it to see
the AWS service, operation, and region. A failed AWS API call records an error on its span and still
raises the original exception in your application.
Choose the integration for the library your application uses:
- Boto3 or Botocore: use
logfire.instrument_botocore()for calls through abedrock-runtimeclient, as well as other AWS services such as S3. This instruments all synchronous Botocore clients in the process; it cannot be limited to a single client. - Anthropic SDK: use
logfire.instrument_anthropic(client)withanthropic.AnthropicBedrockoranthropic.AsyncAnthropicBedrockfor Claude model calls. These clients send their own HTTP requests; Botocore instrumentation does not trace them. - Pydantic AI: use the Pydantic AI integration to record agent runs, model calls, and tool calls, including agents using Bedrock models.
For supported Bedrock models and instrumentor versions, OpenTelemetry adds gen_ai.* attributes
to Converse and InvokeModel spans, including the model and input/output token counts. Enable
instrument_botocore() before using your bedrock-runtime client.
Bedrock message content is off by default. Set the environment variable
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true to record prompts and responses as log
events through this Logfire instance. They can contain sensitive data. Review your
scrubbing configuration before sending them to a Logfire project.
-
No AWS spans: check that the
botocoreextra is installed in the environment running your app, and enable instrumentation before the first API call. Check that other Logfire spans reach Live view too, so you can distinguish an instrumentation problem from an export problem. -
Upload or download spans lose their parent: Boto3’s
upload_file()anddownload_file()use background threads. Installopentelemetry-instrumentation-threadingwithuv add opentelemetry-instrumentation-threading, then enable the threading instrumentor before Botocore instrumentation:from opentelemetry.instrumentation.threading import ThreadingInstrumentor import logfire logfire.configure() ThreadingInstrumentor().instrument() logfire.instrument_botocore() -
Async clients aren’t traced: this integration covers synchronous Botocore clients. Aiobotocore has a separate instrumentor in the same OpenTelemetry package.
instrument_botocore() accepts the options below and additional
keyword arguments supported by the OpenTelemetry instrumentor’s API.
Omitted options use upstream defaults, which may change between versions.
Use request_hook or response_hook to add attributes to an AWS span. Both default to no hook.
request_hook(span, service_name, operation_name, api_params)runs before the request.spanis the OpenTelemetry span,service_nameis a string such ass3,operation_nameis a string such asListBuckets, andapi_paramsis the request parameter dictionary.response_hook(span, service_name, operation_name, result)runs after the request, including when it fails.resultis the response dictionary, orNonewhen no response is available.
Logfire logs exceptions raised by these hooks and allows the AWS call to continue. Hook failures do not replace exceptions raised by the AWS call.
Trace-context headers let another service connect its work to the caller’s trace. See
distributed tracing for how Logfire connects work across services.
The propagator option controls how this integration writes those headers on outgoing AWS requests. The default is
opentelemetry.propagators.aws.aws_xray_propagator.AwsXRayPropagator, which writes the
X-Amzn-Trace-Id header understood by AWS services.
Advanced users can pass another OpenTelemetry TextMapPropagator. AWS services require the
X-Ray header to continue the trace, so replacing the default can prevent that connection.
The distributed_tracing option of logfire.configure() controls incoming context extraction.
It does not disable outgoing AWS trace headers, even when set to False. See
unintentional distributed tracing
for when to accept incoming context.