Logs
Structured records of what the system did.
Structured logs with request ids and useful context make debugging production possible. Too little logging leaves you blind, and too much buries the signal and costs money. Logs must never contain secrets or unnecessary personal data.
Why it matters for a product engineer
When a user reports a problem, logs are often the only record of what the system did for that user. A log line in free text is easy to write and hard to search. A structured line, with fields such as the request id, the user, the route and the duration, can be filtered, counted and joined. Product engineers write most of the log statements, so they decide whether the logs can answer questions about real users.
How to do it
- Log in a structured format, such as key=value pairs or JSON, with the same field names everywhere.
- Add a request id to every line, and pass it to other services, so one request can be followed across them.
- Write one summary line at the end of each request, with its key facts: route, status, user, duration, number of queries.
- Keep secrets, tokens and unneeded personal data out of logs, and test that they stay out.
- Set retention and log levels on purpose, because storage costs grow with traffic.
Common mistakes
- Messages such as “error occurred” with no context.
- Passwords, tokens or full card numbers in logs.
- Different field names for the same thing in different services.
- Logging everything at debug level in production, until the bill arrives.
Example
Stripe adds a “canonical log line” to its normal logs: one line per request per service, written at the end of the request. The line collects the request’s key facts, from the HTTP method and status to the user, the rate limit and the number of database queries. Engineers can then ask questions such as “which users are rate limited the most?” in one query. Stripe also loads these lines into its data warehouse, and uses them to power its Developer Dashboard.
A Stripe canonical log line, part by part
canonical-log-line1 http_method=POST http_path=/v1/charges http_status=2002 auth_type=api_key user_id=usr_1233 rate_allowed=true rate_remaining=994 database_queries=34 duration=0.0095 request_id=req_1236
- 1Line typeOne per request, written at the end
- 2The requestWhat was asked, and the result
- 3Who askedLets you group by user
- 4Rate limitingAnswers who is limited most
- 5Cost and speedFor slow-request analysis
- 6Request idJoins this line to the full trace
One line holds the facts that usually sit in many lines, so one query can answer a question about all requests.
ReportedStripe blog, Canonical log lines, 2019
Sources: Fast and flexible observability with canonical log lines (Stripe) ↗ · Observability primer (OpenTelemetry) ↗
Resources
1- docsStart here
Observability primer ↗
OpenTelemetry · 10 minLogs, metrics and traces explained, and how they connect so you can answer questions you did not plan for.
Tools
4- ToolOpen source
OpenTelemetry
Open standard and SDKs for emitting traces, metrics and logs to any backend.
- ToolOpen source
Grafana
Open-source dashboards for metrics, logs and traces from almost any data source.
- ToolOpen source
Prometheus
Open-source metrics database and alerting, the common default for service metrics.
- ToolFreemium
Sentry
Error tracking and performance monitoring that groups exceptions and links them to releases.