DEV Community

Cover image for Day 29: From Logs to Insights – implementing Structured Logging & X-Ray in AWS Lambda
Eric Rodríguez
Eric Rodríguez

Posted on

Day 29: From Logs to Insights – implementing Structured Logging & X-Ray in AWS Lambda

When you are running a monolithic app on your laptop, debugging is easy. You just look at the console. But when your code is running in ephemeral containers in the cloud, debugging can be a nightmare.

Today, I upgraded my Finance Agent with professional Observability tools.

  1. Structured Logging (JSON)

I refactored my Python Lambda to log in JSON format.

Before: print(f"Error: {e}") (Hard to parse)

After: print(json.dumps({"level": "ERROR", "component": "Bedrock", "details": str(e)}))

This small change allows me to use CloudWatch Logs Insights to run SQL-like queries on my logs, such as filtering only errors related to the AI model.

  1. Visualizing Latency with AWS X-Ray

I enabled "Active Tracing" in the Lambda configuration. Now, AWS automatically generates a Service Map (see cover image). I can visually see that my Plaid API call takes 200ms, while my Bedrock AI generation takes 1.5s. This visual "report" is invaluable for optimization.

Top comments (0)