Lesson 8 of 9 · 25m
Observability for Distributed Functions
Logs, metrics, traces, correlation IDs, and the operational habits needed when one user action fans out across five managed services.
Debugging serverless is not debugging "the server." One click can cross API Gateway, Lambda, EventBridge, SQS, another Lambda, DynamoDB, and a vendor API. There is no host to SSH into and no single process containing the whole story. Observability is the only way the system is legible.
Correlation beats volume
The most important field in every log line is a correlation ID that survives service boundaries. Accept one at the edge or generate it, then pass it through events, queue attributes, workflow input, and downstream HTTP headers:
import uuid
def handler(event, context):
correlation_id = extract_correlation_id(event) or str(uuid.uuid4())
log.info("order accepted", extra={
"correlation_id": correlation_id,
"order_id": event.get("orderId"),
})
publish_event(event, correlation_id)Use structured logs. JSON with correlation_id, order_id, latency_ms, and error_kind can be queried; a prose sentence cannot.
Metrics should describe pain and pressure
Useful metrics depend on the component:
API: 4xx/5xx count, p95 latency, throttles
Lambda: errors, duration p95/p99, concurrency, iterator age
SQS: visible messages, oldest message age, DLQ depth
EventBridge: failed invocations, throttled rules
DynamoDB: throttled reads/writes, latencyThe first alarms should track user-visible damage: API 5xx rate, workflow failures, queue oldest-message age, and DLQ growth. A dashboard of green invocation counts is decoration.
Tracing answers where time went
X-Ray or OpenTelemetry tracing separates cold starts, queue waits, retries, downstream calls, and handler time. Without spans, all of them look like one mysterious three-second blur.
from aws_xray_sdk.core import xray_recorder
@xray_recorder.capture("charge_payment")
def charge_payment(order):
...Tracing has cost and noise. Sample success paths aggressively, but do not sample errors away.
Errors need shape
"Something failed" is not actionable:
{
"level": "error",
"correlation_id": "8a0a...",
"order_id": "ord_123",
"error_kind": "vendor_timeout",
"retryable": true,
"dependency": "shipping-api",
"duration_ms": 4210
}Now bad input, bugs, throttling, vendor failure, and timeouts can drive different responses.
Use the evidence native to each service
Step Functions execution history explains workflow state. EventBridge failure destinations explain undelivered events. SQS DLQs preserve poison messages. API Gateway access logs explain edge behavior. CloudWatch is important, but it is not the whole system.
Field notes
- Never log secrets, authorization headers, payment data, or full user documents because the payload was convenient.
- Standardize field names across services;
request_idin one service andtraceIdin another silently breaks correlation. - Alarms should page only for actionable failure, not every transient retry.
- Inject failure in staging: vendor outage, poison message, permissions regression, hot partition. Practice before production does it for you.
Was this lesson helpful?