Troubleshooting and Optimization · Topic 1 of 5
Logs, Metrics and Traces
Read CloudWatch metrics, logs and X-Ray traces, query logs with Logs Insights and metric filters, and design structured logging for DVA-C03 scenarios.
- 5 min
- Metrics say that, traces say where, logs say why.
- Use p99 or maximum rather than average when hunting latency problems.
- Logs Insights is for ad hoc queries. A metric filter is for continuous alerting on a pattern.
What it is
Three signals describe what a running application is doing. Metrics are numbers over time, logs are timestamped records of events, and traces follow one request across several services.
Each answers a different question. Metrics tell you that something is wrong, traces tell you where in the request path it is wrong, and logs tell you why.
On the exam you are given a symptom and must pick the signal or the query that finds the cause fastest.
How it works
Reading metrics
AWS services publish metrics to CloudWatch automatically, grouped by namespace such as AWS/Lambda. The average often hides problems, so look at maximums and percentiles such as p99 for latency.
| Symptom | Metric to check | What it usually means |
|---|---|---|
| Function failing | Errors | Unhandled exception or timeout |
| Requests rejected | Throttles | Concurrency limit reached |
| Slow responses | Duration | Slow dependency or too little memory |
| Stream consumer behind | IteratorAge | Consumer cannot keep up with the shard |
| Queue backing up | ApproximateAgeOfOldestMessage | Consumers too slow or too few |
| API returns 5xx | 5XXError, Latency, IntegrationLatency | Backend failing; the gap between Latency and IntegrationLatency is time spent in API Gateway itself |
Reading logs
Lambda writes everything your code prints to a log group named /aws/lambda/<function name>, with one log stream per execution environment. The execution role needs the logs:CreateLogGroup, logs:CreateLogStream and logs:PutLogEvents permissions, which the AWSLambdaBasicExecutionRole managed policy includes. A function with no logs at all usually lacks them.
Every invocation ends with a REPORT line showing Duration, Billed Duration, Memory Size, Max Memory Used and, on a cold start, Init Duration. When Max Memory Used equals Memory Size, the function is close to running out of memory.
Querying with CloudWatch Logs Insights
Logs Insights runs ad hoc queries over one or more log groups for a chosen time range. It discovers JSON fields automatically, and for Lambda it exposes fields such as @duration, @maxMemoryUsed and @requestId.
fields @timestamp, @requestId, @duration
| filter @type = "REPORT"
| sort @duration desc
| limit 10
The common commands are fields, filter, stats, sort, limit, parse and dedup. A query such as stats count() by bin(5m) after a filter on ERROR shows when errors started.
Two other tools work on log data continuously:
- Metric filter: matches a pattern in incoming log events and turns it into a CloudWatch metric you can alarm on. It only counts events that arrive after the filter exists.
- Subscription filter: streams matching events to a destination such as Lambda, Kinesis or Amazon Data Firehose for processing or long term storage.
Designing a logging strategy
Log in structured JSON with the same keys everywhere, for example level, message, requestId, userId, operation and durationMs. Logs Insights and metric filters can then select on those fields instead of parsing free text.
- Use levels. DEBUG for detail, INFO for state changes, WARN and ERROR for problems. Lambda advanced logging controls can emit JSON logs and set an application log level, so lower levels are dropped before they reach CloudWatch.
- Add a correlation ID. Pass a request identifier through every service call so one user action can be followed across components. The X-Amzn-Trace-Id header does this for X-Ray.
- Never log secrets or personal data. Mask tokens, passwords and personally identifiable information before writing the event.
- Control cost. CloudWatch Logs keeps data forever by default, so set a retention period on each log group. Avoid logging full payloads on hot paths.
Powertools for AWS Lambda provides a Logger that produces structured JSON with the request context added for you.
Reading traces
AWS X-Ray groups the work of one request into a trace made of segments, one per service, with subsegments for downstream calls. The service map shows latency and error rates per node, and a slow or faulting node points at the culprit. The next page covers how to turn tracing on.
How the exam asks about this
- "Find the ten slowest invocations of a function in the last hour" -> a Logs Insights query that filters REPORT lines, sorts on @duration and limits to ten.
- "Alert the team when the application logs more than five ERROR entries in five minutes" -> a metric filter on the log group plus a CloudWatch alarm.
- "Developers cannot filter log entries by customer across several services" -> structured JSON logs with consistent fields, queried with Logs Insights.
What to remember for the exam
- Metrics say that, traces say where, logs say why.
- Use p99 or maximum rather than average when hunting latency problems.
- Logs Insights is for ad hoc queries. A metric filter is for continuous alerting on a pattern.
- Metric filters do not backfill; they count only events after creation.
- Lambda log group names follow /aws/lambda/<function name>, and the execution role needs logs permissions.
- The REPORT line shows duration, billed duration, memory size and maximum memory used.
- Set retention on log groups, because the default is never to expire.
Do not log secrets, tokens or personal data, even at DEBUG level. Anyone with read access to the log group can read them.
Common mistakes
Judging latency by the average. A healthy average can hide a slow tail. Check percentiles and maximums before declaring a service fast.
Searching log streams by hand. Opening streams one by one does not scale. A Logs Insights query across the log group finds the same events in seconds.
Expecting a metric filter to count old events. The filter only publishes data points for log events ingested after it is created, so it cannot reconstruct history.
Check yourself
Which feature turns the text ERROR in application logs into an alarmable metric?
A CloudWatch Logs metric filter, with an alarm on the metric it publishes.
A function has no log group at all. What do you check first?
The execution role, which must allow logs:CreateLogGroup, logs:CreateLogStream and logs:PutLogEvents.
Why log in JSON with consistent keys?
Logs Insights and metric filters can then select and aggregate on fields such as requestId and level without parsing free text.
Topic 1 of 5 in Troubleshooting and Optimization
Test yourself on Troubleshooting and Optimization
The full AWS DVA-C03 question bank has 850 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.