Monitoring server-side errors
medium 30 minCatch errors in one place, add the facts that explain them, group the same fault into one issue, count them as a share of all requests, and call a person only when users are hurt.
Video lesson · Chapter 1 of 6
Turn an exception into an incident signal
Theory
Read or listen first. About 1 min.
What it is
Server-side error monitoring means finding, recording, grouping and reporting the errors that happen inside your own services.
Two tools share the work. Metrics count errors: how many, which share, since when. An error tracker keeps single errors with their stack trace and groups equal ones. Metrics wake people. The tracker tells them what to repair.
Why it exists
Most users who meet an error never write to you. They leave. If you wait for letters you learn about a fraction of the faults, hours late.
Raw errors are too many to read. One broken line of code can raise five thousand errors in a minute. Grouping makes one issue of them, with a count.
And not every error deserves a call at night. A clear rule, based on what was promised to users, keeps the team calm and the calls rare.
A short history
May 1996
RFC 1945 describes HTTP/1.0 with the 500, 501, 502 and 503 status codes.
April 2012
RFC 6585 adds 429 Too Many Requests.
June 2022
RFC 9110 becomes the common definition of HTTP status codes for all versions.
July 2023
RFC 9457 defines problem details, a standard JSON answer for errors.
Words to know
- Exception
- A signal in a program that something unusual happened. It can be caught.
- Stack trace
- The list of calls that led to the exception.
- Fingerprint
- A short key. Errors with the same key belong to the same issue.
- Issue and event
- One fault, and one time it happened.
- SLI
- What you measure, such as the share of good requests.
- SLO
- The target for it, such as 99.9%.
- SLA
- A contract with consequences, usually weaker than the SLO.
- Error budget
- What the target allows to fail: 0.1% for 99.9%.
- Alert fatigue
- People stop reacting because most alerts mean nothing.
Understand it
Start here
Burnt pancakes
Nobody throws a burnt pancake away without a note.
| In the kitchen | In the system |
|---|---|
| Burnt pancake | Failed request |
| Card | Context of the error |
| Plates | Issues, grouped by fingerprint |
| Share on the board | Error rate |
| Tokens in the jar | Error budget |
The idea
Six goals
What good error monitoring gives
Detection
You know within minutes.
Grouping
Equal errors are one issue.
Priority
Many users or money first.
Alerting
The right person, at the right time.
Context
Enough facts to repair it.
Trends
More or fewer than last week, and since which release.
Codes
Whose fault is it?
| Code | Meaning | What to do |
|---|---|---|
| 400 | Bad input | Count. A jump after a release is ours |
| 401, 403 | Not signed in, not allowed | Count. A jump may be a broken login or an attack |
| 404 | Not found | Count. A jump may be a broken link of ours |
| 429 | Too many requests | Count. Check the limit |
| 500 | Our program failed | Issue |
| 502 | A server behind us answered badly | Look at that service |
| 503 | Overloaded or in maintenance | Capacity; tell clients when to come back |
| 504 | A server behind us was too slow | Look at timeouts |
What to do with a failed request
Codes made by the load balancer itself are a separate case. See load balancer monitoring.
Kinds
Six families of errors
| Family | Examples | First severity |
|---|---|---|
| Runtime | Null pointer, TypeError, IndexError | High |
| Database | Connection timeout, deadlock, failed query | Critical |
| Other services | HTTP timeout, DNS, certificate | High |
| Resources | Out of memory, disk full, no free thread | Critical |
| Validation | Bad JSON, missing field, wrong type | Low |
| Login and rights | Invalid token, expired session, denied | Medium |
Three words
| Word | Meaning |
|---|---|
| Error | A wrong state. In Java also a class for serious problems that a reasonable program should not catch, such as no memory |
| Exception | A signal that can be caught and handled |
| Failure | Users do not get what they expect |
The whole
From a throw to a call
Two paths
One exception
- 1
Capture
One middleware at the edge of the service catches what nobody else caught. Workers and scheduled jobs need the same.
- 2
Enrich
Add the trace id, the route, the release, the pseudonym of the user.
- 3
Send
In the background, with a limit. If the tracker is down, drop and count.
- 4
Group
Make a fingerprint, so that 5,234 events become one issue.
- 5
Alert
Rules on shares, on new issues and on the budget.
Context
What to write on the card
| About | Keep | Leave out |
|---|---|---|
| Request | Request id, trace id, method, route | Full body, cookies, authorization header |
| User | Internal id, session as a hash, browser | Name, e-mail, full address |
| Error | Type, message, stack, cause, own error code | Values inside the message |
| Place | Service, release, host, environment, time | Environment variables |
DatabaseError: Connection timeout after 30000ms at PaymentService.processPayment (app/services/payment.ts:45) at async OrderController.createOrder (app/controllers/order.ts:23) at async Router.handle (node_modules/express/lib/router.js:234){ "type": "https://example.com/errors/unavailable", "title": "Payment is not available right now", "status": 503, "request_id": "req-7f3a"}The trace id joins the error with the lines of other services. How it travels is told in inside a logging service.
Group
Fingerprints
A fingerprint is built from what stays the same each time: the type of the error and the first frame of your own code. What changes each time must be removed first.
| Good | Bad | Why bad |
|---|---|---|
| Type and first own frame | Request id | Always different: one issue per event |
| Service and message without values | Time | Always different |
| Own key: database, table | Full message with values | One issue per order |
| Other service and its status | Type alone | Different faults in one issue |
import redef normal(message): message = re.sub(r"[0-9a-f]{8}-[0-9a-f-]{27}", "<id>", message) return re.sub(r"\d+", "<n>", message)def fingerprint(kind, message, frame): return (kind, frame or normal(message))a = fingerprint("Timeout", "order 41 took 30012 ms", "payment.ts:pay")b = fingerprint("Timeout", "order 97 took 30555 ms", "payment.ts:pay")print(a == b, normal("order 41 took 30012 ms"))Leave the line number out of the key, or each release makes new issues. Use file and function. Sentry groups by default by fingerprint, which it derives from stack trace, exception and message, and lets you set your own.
Too many
Sampling an error storm
| Way | Rule | Good for |
|---|---|---|
| First ones | The first N of each issue | New faults: full detail at once |
| Rate limit | At most N per minute and issue | Storms |
| Random | 10% of all | Saving money on frequent ones |
Promise
Error budgets
| Word | Meaning | Example |
|---|---|---|
| SLI | What is measured | 99.2% of requests succeed |
| SLO | What we aim for | 99.9% |
| SLA | What the contract says | 99.5%, or money back |
The budget of a month
Where are we?
While there is budget, the team may take risks and release often. When it is empty, work on reliability comes first until it fills again. The rule is agreed before, with the product owner, or it will be discussed at the worst time.
Alerts use the burn rate, the speed at which the budget goes. The rules with two windows are in load balancer monitoring. What nines mean for design is in high availability.
Call
Alerts people trust
Alert on
- A rising share of errors.
- Budget burning fast.
- A new kind of error after a release.
- Many users hit by one issue.
Do not alert on
- Each single error.
- Expected errors such as rate limits.
- What nobody can act on.
- Numbers without an owner.
Severity
| Level | Example | Reaction |
|---|---|---|
| P1 | Service is down | Page at once |
| P2 | Main function broken, over 10% of users | Page, start within 15 minutes |
| P3 | Small function worse, under 1% of users | Next working day |
| P4 | Noise, warnings | Ticket |
# more than 1% of requests failsum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.01# less than a quarter of the budget left: a ticketslo:error_budget_remaining < 0.25# P99 over 2 secondshistogram_quantile(0.99, sum by (le) (rate(http_duration_seconds_bucket[5m]))) > 2Should it ring now?
A share needs traffic. At night one failure of three requests is 33%. Require a minimum number of errors too.
Life of an issue
Silent
Failures without an error
| Silent failure | How to see it |
|---|---|
| Answer 200 with an error inside | Check the content with a probe, count business results |
| An exception swallowed by an empty catch | Rule in code review, lint |
| A message that fails again and again | Length and age of the dead letter queue |
| A job that did not run | Alert when the last success is too old |
| Retries hide a sick service | Count each attempt, not only the last |
Errors users meet before they reach the server have a page of their own: client-side errors.
Summary
The whole topic in one minute.
Capture in one middleware, enrich with request, user, code and environment, send without blocking, group by a stable fingerprint, and alert on shares of requests.
5xx is ours, 4xx is usually the caller's, and silent failures have no status at all: check results, not only codes. Spend the error budget on purpose and slow down changes when it is gone.
- Capture, enrich, send, group, alert.
- Metrics count all; the tracker keeps examples.
- 5xx is ours; a jump of 4xx may be too.
- Fingerprint: type and own frame, no values.
- Share of requests, with a minimum, against a promise.
- Look for failures without an error.