Skip to content

How do I add observability to an existing Amazon Bedrock RAG application?

0

I have a simple, functioning RAG application on AWS and want to implement observability using AWS services and recommended practices.

Current architecture

  • An AWS Lambda function receives a user question.
  • It calls Amazon Bedrock Knowledge Bases using bedrock-agent-runtime.retrieve() to retrieve relevant document chunks.
  • It passes the question and retrieved context to an Amazon Bedrock model using bedrock-runtime.converse() to generate an answer.
  • The Lambda returns the answer and source information

My question

What is the recommended AWS approach for implementing end-to-end observability for this type of RAG application? Specifically, how should I instrument the Lambda, connect retrieval and generation into a trace, and capture useful information such as retrieval results, model latency, token usage, and errors? Should I use AWS Distro for OpenTelemetry, the Bedrock observability features, CloudWatch, or another AWS-supported approach?

I am looking for an architecture and practical implementation guidance for an existing application, rather than instructions for building a new RAG system from scratch.

What I have tried

I enabled CloudWatch Transaction Search and configured trace delivery for my managed Bedrock Knowledge Base to X-Ray using CloudWatch delivery APIs. I can see spans in the aws/spans log group and have found AgentCore.BuiltinTools.KnowledgeBaseRetrieve spans corresponding to retrieval requests.

However, these spans mainly contain metadata such as operation name, duration, HTTP status, and resource identifiers. They do not contain the user's question, retrieved chunks, generated answer, or model token usage. The traces also appear inconsistently in the observability dashboard.

I have not yet instrumented the Lambda itself or configured end-to-end tracing across retrieval and generation. My attempts so far may not represent the recommended approach.

2 Answers
0

For your existing RAG application, the recommended AWS approach combines multiple observability layers to achieve end-to-end visibility. Here's how to implement comprehensive observability:

Core Instrumentation Strategy

Instrument your Lambda function using the AWS Distro for OpenTelemetry (ADOT) SDK. If you're using Lambda, leverage the AWS Lambda Layer for OpenTelemetry to simplify integration. This creates the foundation for distributed tracing across your entire request flow.

Connecting Retrieval and Generation into Unified Traces

The key is implementing distributed tracing that spans from your Lambda through Bedrock Knowledge Bases to the model invocation. When you instrument with ADOT, you can propagate trace context across service boundaries. Use consistent session IDs when possible to maintain context across related interactions. The ADOT SDK allows you to create custom spans for your Lambda logic, retrieval calls, and generation calls, linking them into a single trace.

Capturing Rich Telemetry Data

While Bedrock's built-in observability provides operational metrics like invocations, errors, throttles, and latency, it intentionally focuses on service-level telemetry rather than request content. To capture the detailed information you need—user questions, retrieved chunks, generated answers, and token usage—add custom attributes to your traces using the ADOT SDK. This allows you to enhance spans with application-specific context that's valuable for troubleshooting and analysis.

For your Lambda function, emit custom runtime metrics for aspects like retrieval result quality, context size, and generation parameters. These complement the standard metrics Bedrock publishes to CloudWatch under the AWS/Bedrock/KnowledgeBases namespace.

CloudWatch Integration

Configure your Lambda to deliver logs to CloudWatch Logs and enable CloudWatch Transaction Search (which you've already done). This provides a consolidated view where you can correlate logs, metrics, and traces. Set up CloudWatch alarms to notify you of potential issues like elevated error rates or latency spikes before they impact users.

Practical Implementation Steps

  1. Add the ADOT Lambda Layer to your function and initialize the OpenTelemetry SDK in your Lambda code
  2. Create custom spans around your bedrock-agent-runtime.retrieve() and bedrock-runtime.converse() calls
  3. Add custom attributes to these spans containing sanitized versions of questions, retrieval metadata (like number of chunks returned), and generation metadata (like token counts and model parameters)
  4. Ensure trace context propagates by using the headers provided by ADOT
  5. Configure your Lambda's CloudWatch log group as the destination for both logs and trace segments to keep everything together
  6. Monitor resource usage metrics, particularly Lambda memory and execution duration, to optimize performance

Best Practices

  • Use consistent session identifiers across related requests to track multi-turn conversations
  • Implement custom attributes strategically—focus on data that helps with debugging and optimization
  • Pay attention to memory usage metrics to optimize your Lambda's performance
  • Set up CloudWatch alarms for key metrics like error rates, latency percentiles, and throttles
  • Consider the privacy implications of logging user questions and generated content; implement appropriate data handling policies

Why Built-in Spans Are Limited

The spans you're seeing from Bedrock Knowledge Bases are designed for service-level observability—tracking that the service is functioning correctly, measuring latency, and identifying errors. They don't contain request content by design for privacy and security reasons. This is why custom instrumentation at the application layer is essential for capturing the semantic details of your RAG workflow.

This multi-layered approach—ADOT for application-level tracing, Bedrock's built-in observability for service metrics, and CloudWatch for centralized monitoring—provides comprehensive visibility into your RAG application's behavior, performance, and reliability.
Sources
Add observability to your Amazon Bedrock AgentCore resources - Amazon Bedrock AgentCore
Observability for managed knowledge bases - Amazon Bedrock
OPS 4. How do you implement observability in your workload? - AWS Well-Architected Framework

answered a month ago

EXPERT

reviewed a month ago

0

The Knowledge Base spans you found are service-level only, so the content (question, chunks, answer, tokens) has to come from two other places: Bedrock's invocation logs and your own Lambda instrumentation. Here's a setup that ties them together.

1. Turn on model invocation logging (it's off by default). Bedrock console → Settings → Model invocation logging, with CloudWatch Logs and/or S3 as the destination. It covers Converse, ConverseStream, InvokeModel and InvokeModelWithResponseStream. Each log entry includes the request and response bodies (inline up to 100 KB; larger bodies go to S3), inputTokenCount, outputTokenCount, the modelId and the caller's identity.arn. That covers the "what was asked and answered" gap without writing prompts into your traces.

2. Use requestMetadata to join the logs to your traces. Converse accepts requestMetadata, up to 16 key-value pairs that are written into the invocation log so you can filter on them. Put your trace ID, a request or session ID, and the Knowledge Base ID there. In CloudWatch Logs Insights you can then go from a slow trace straight to the exact prompt and response.

3. Instrument the Lambda as one trace with two child spans. Turn on tracing for the function (X-Ray active tracing, or the ADOT Lambda layer if you want OpenTelemetry), then create one span around retrieve() and one around converse(), both under the invocation span. On each span, record:

  • retrieve: the number of results, the top scores from retrievalResults[].score, the source locations (S3 URIs), and the latency. Store IDs and locations, not chunk text.
  • converse: usage.inputTokens, usage.outputTokens, usage.totalTokens, metrics.latencyMs, stopReason and the model ID. The Converse response returns all of these, so you don't need to estimate tokens.

4. Publish a few custom metrics. Emit total tokens, retrieval count and end-to-end latency per request as CloudWatch metrics (Embedded Metric Format from Lambda is the simplest), next to the standard AWS/Bedrock metrics. Alarm on p95 latency, throttles and error counts.

5. Decide on data handling up front. Invocation logs contain user prompts and answers. Restrict access to that log group or bucket, set a retention period, and encrypt it with KMS if your data needs it.

With this split, the traces show where the time goes (retrieval vs generation), the invocation logs show what was said, and requestMetadata links the two.

answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.