CloudWatch logs, metrics and queries
Choose the right signal
A checkout service needs three complementary signals: metrics summarize counts and durations, logs describe discrete events, and traces follow a request across components. A metric such as Requests in the Demo/Checkout namespace with dimensions Environment=dev and Route=checkout identifies a time series. A different dimension combination is another custom metric. Use a bounded set of route templates, not raw request IDs, as dimensions. Put correlation IDs in structured logs instead. The CloudWatch agent can collect host metrics and logs; configure only required sources, a scoped role and retention.
Investigate with logs
Assume one structured completion event per eligible request, containing route, status, durationMs and traceId. The illustrative Logs Insights QL query below groups checkout completions by status; restrict the selected log group and time range before running it. A retry or duplicated completion event breaks the one-record assumption, so count at a defined boundary. Metric filters produce metrics from matching incoming log entries; they are not historical ad-hoc queries. CloudWatch also supports direct log alarms on query results. Set log retention and redact secrets, authorization headers and personal payloads before emission.
fields @timestamp, route, status, event| filter route = "checkout" and event = "complete"| stats count(*) as requests by statusRead rates and distributions
In a synthetic five-minute window, 1,000 requests include 20 failures: throughput is 200 requests/minute and error rate is 2%. For unequal windows, sum failures and totals before dividing; an average of percentages is generally wrong. Saturation means a constrained resource is near capacity: track queue age, utilization or connection pools. A mean can hide slow requests. For durations 10, 10, 10, 10, 200 ms, mean is 48 ms and nearest-rank p95 is 200 ms. Do not average instance p95 values to get a fleet p95; use compatible distributions or raw observations. CloudWatch percentile calculation need not use our offline nearest-rank method.
Bound monitoring cost
A dashboard combines request rate, error rate, tail latency and saturation using the same time range. A hypothetical metric plan with 3 routes, 2 environments and 4 metric names can produce 24 series if all combinations emit. Estimate log ingest GB, stored GB-month, query bytes scanned, metric series or ingestion volume (depending on ingestion model), alarms and dashboards separately. For instance, 2 GB/day for 30 days is 60 GB ingested, not automatically 60 GB-month stored. Shorter retention does not reverse ingest charges. Check region, log class, pricing and quotas before a real lab; no cloud query is run here.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CloudWatch logs, metrics and queries — knowledge check
Reason about telemetry, distributed tracing and reliability objectives.
Which dimension is usually a poor choice for a bounded custom metric?
What is the error rate for 20 failures out of 1,000 eligible requests?
What is nearest-rank p95 for 10, 10, 10, 10, 200 ms?
How does a metric filter differ from an ad-hoc Logs Insights query?
Which quantity is 2 GB/day for 30 days?