Skip to content

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.

What you’ll capture

  • 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.usage metric

Before you start

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.

Installation

Install logfire with the botocore extra:

Terminal
pip install 'logfire[botocore]'

Usage

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.

main.py
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']])

Verify it worked

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.

Amazon Bedrock

Choose the integration for the library your application uses:

  • Boto3 or Botocore: use logfire.instrument_botocore() for calls through a bedrock-runtime client, 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) with anthropic.AnthropicBedrock or anthropic.AsyncAnthropicBedrock for 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.

Troubleshooting

  • No AWS spans: check that the botocore extra 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() and download_file() use background threads. Install opentelemetry-instrumentation-threading with uv 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.

Reference

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.

Request and response hooks

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. span is the OpenTelemetry span, service_name is a string such as s3, operation_name is a string such as ListBuckets, and api_params is the request parameter dictionary.
  • response_hook(span, service_name, operation_name, result) runs after the request, including when it fails. result is the response dictionary, or None when 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 propagation

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.