Monitoring and Logging · Topic 4 of 5
Log Analysis, Tracing and Anomaly Detection
Analyze logs and metrics with CloudWatch Logs Insights, Contributor Insights, anomaly detection, X-Ray tracing, Synthetics canaries and DevOps Guru.
- 5 min
- Logs Insights queries log groups with
fields,filter,stats,sortandbin(). - Lambda REPORT lines expose duration and memory fields for analysis.
- Contributor Insights shows top talkers; Metrics Insights queries metrics with SQL like syntax.
What it is
Collecting data is only useful if you can find the cause of a problem quickly. This page covers the tools that turn raw logs, metrics and requests into answers: queries, traces, synthetic tests and machine learning detection.
A good approach has three layers. Metrics tell you that something is wrong, logs tell you what happened, and traces tell you where in a chain of services the time or the error occurred.
How it works
CloudWatch Logs Insights
Logs Insights queries one or more log groups with a pipe style language. It discovers fields in JSON logs automatically and exposes system fields such as @timestamp and @message.
fields @timestamp, @message
| filter @message like /ERROR/
| stats count() as errors by bin(5m)
| sort errors desc
Lambda writes a REPORT line for each invocation, and Logs Insights exposes fields such as @duration, @maxMemoryUsed and @initDuration.
filter @type = "REPORT"
| stats max(@duration) as maxMs, avg(@maxMemoryUsed) as avgMem by bin(10m)
Queries can cover several log groups and, with cross account observability, groups in other accounts. You can save queries and add their results to dashboards.
Contributor Insights and Metrics Insights
Contributor Insights reads log data and shows the top contributors, such as the client IPs sending the most requests or the users causing most errors. Metrics Insights queries metrics with an SQL like language, for example the ten instances with the highest CPU.
SELECT AVG(CPUUtilization) FROM SCHEMA("AWS/EC2", InstanceId)
GROUP BY InstanceId ORDER BY AVG() DESC LIMIT 10
Anomaly detection
CloudWatch can learn the normal pattern of a metric, including daily and weekly cycles, and draw an expected band. An alarm can then fire when the metric leaves the band, so you do not have to guess a fixed threshold. Logs anomaly detection finds unusual patterns in log events in a similar way.
| Method | Good for |
|---|---|
| Static threshold alarm | Clear limits, such as disk above 90 percent |
| Anomaly detection alarm | Metrics with seasonal patterns where a fixed threshold is wrong |
| Composite alarm | Combining several signals to cut noise |
AWS X-Ray
X-Ray traces a request as it passes through services. A trace is made of segments (one per service), which contain subsegments (calls to downstream services or databases). The service map draws the graph and highlights latency and error rates per node.
| Concept | Meaning |
|---|---|
| Annotation | Key value pairs that are indexed, so you can filter traces by them |
| Metadata | Extra data that is stored but not indexed |
| Sampling rule | Controls what share of requests are traced, to limit cost |
| Group | A filter expression that saves a set of traces and can drive metrics |
Instrument applications with the X-Ray SDK or with AWS Distro for OpenTelemetry. On EC2 and on premises servers the X-Ray daemon relays data, and Lambda supports active tracing as a function setting. ECS tasks usually run the daemon or collector as a sidecar container.
aws lambda update-function-configuration --function-name orders --tracing-config Mode=Active
X-Ray finds the slow downstream call in a chain such as API Gateway to Lambda to DynamoDB, which a single log group cannot show.
Synthetic monitoring
CloudWatch Synthetics runs canaries, which are scripts (Node.js or Python) that visit endpoints or workflows on a schedule from AWS. They find outages before real users do and produce metrics, screenshots and logs that alarms can watch.
Machine learning operations insight
Amazon DevOps Guru analyses metrics, logs, events and traces of the resources you choose (by CloudFormation stack, tag or account) and reports insights about anomalous behavior with related events and recommended actions. Choose it for broad anomaly detection with little configuration.
Analysis outside CloudWatch
| Data in | Tool |
|---|---|
| S3 (CloudTrail, flow logs, ALB logs) | Amazon Athena with SQL |
| Large volume streaming logs | Amazon OpenSearch Service |
| Time series for containers | Managed Prometheus and Managed Grafana |
| Alerts | CloudWatch alarms, EventBridge |
For Athena, convert logs to a columnar format such as Parquet and partition them by date to reduce scanned data and cost.
A troubleshooting workflow
- Start from an alarm or user report and note the time window.
- Check metrics for the symptom: error rate, latency, saturation.
- Query logs for errors in that window and group them by cause.
- Follow a failing request through the X-Ray trace to the slow or failing component.
- Compare with a recent deployment or configuration change from CloudTrail and Config.
What to remember for the exam
- Logs Insights queries log groups with
fields,filter,stats,sortandbin(). - Lambda REPORT lines expose duration and memory fields for analysis.
- Contributor Insights shows top talkers; Metrics Insights queries metrics with SQL like syntax.
- Anomaly detection alarms use a learned band and suit seasonal metrics.
- X-Ray annotations are indexed and searchable; metadata is not.
- X-Ray sampling rules control the cost and volume of traces.
- Lambda needs active tracing enabled and permission to write segments.
- Synthetics canaries test endpoints continuously from outside the application.
- DevOps Guru provides machine learning insights with recommendations.
- Athena plus partitioned Parquet is the cost efficient way to query archived logs.
To find which downstream service adds latency in a microservice chain, choose X-Ray tracing over searching logs in each service.
Common mistakes
Using logs alone for latency. A single service log cannot show where time went across several services.
Fixed thresholds on seasonal metrics. They alarm every busy hour or miss real problems at night.
Putting searchable data in X-Ray metadata. Only annotations are indexed.
Querying huge raw log sets in Athena. Unpartitioned text files make every query scan everything.
Tracing every request on a high traffic service raises cost and overhead, so tune the sampling rules.
Check yourself
Which X-Ray field lets you filter traces by a custom key?
An annotation, because annotations are indexed and metadata is not.
Which CloudWatch feature shows the top clients producing errors in a log group?
Contributor Insights.
When is an anomaly detection alarm better than a static threshold?
When the metric follows daily or weekly patterns, so the normal level changes over time.
Topic 4 of 5 in Monitoring and Logging
Test yourself on Monitoring and Logging
The full AWS DOP-C02 question bank has 844 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.