- Newest
- Most votes
- Most comments
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
- Add the ADOT Lambda Layer to your function and initialize the OpenTelemetry SDK in your Lambda code
- Create custom spans around your bedrock-agent-runtime.retrieve() and bedrock-runtime.converse() calls
- 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)
- Ensure trace context propagates by using the headers provided by ADOT
- Configure your Lambda's CloudWatch log group as the destination for both logs and trace segments to keep everything together
- 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
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,stopReasonand the model ID. TheConverseresponse 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
Relevant content
asked 2 years ago
