Telemetry
API reliability SQL recipe

Calculate API Error Rate by Route

Use SQL to rank API routes by 5xx error rate while protecting the result from low-volume noise.

Beginnerapi_requestsReviewed 2026-07-27

Question answered

Which API routes have the highest meaningful 5xx error rate?

An error count favors busy routes. Error rate normalizes failures by request volume, while a minimum-volume threshold keeps one failure on a rarely used endpoint from dominating the list.

Event contract

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampWhen the request completed, normalized to UTC.
route_templateUtf8Stable route shape such as /api/projects/:id.
status_codeInt64HTTP response status code.
latency_msFloat64Request duration in milliseconds.
DataFusion SQL

Copy the query

sql
SELECT
  route_template,
  COUNT(*) AS requests,
  SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END) AS errors,
  100.0 * SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END)
    / NULLIF(COUNT(*), 0) AS error_rate_pct
FROM api_requests
WHERE timestamp_utc >= now() - INTERVAL '24 hours'
GROUP BY route_template
HAVING COUNT(*) >= 20
ORDER BY error_rate_pct DESC
LIMIT 10;

This recipe is read-only and reviewed against Telemetry's DataFusion query patterns. Sample output is deterministic and synthetic; validate field types, thresholds, and business definitions against your own data.

Query result

5xx error rate by route

The checkout route is the clearest reliability risk even though search has more total traffic.

route_templaterequestserrorserror_rate_pct
/api/checkout1,240483.87
/api/projects/:id/sync860192.21
/api/search2,410170.71
/api/profile60220.33

Synthetic example output. Run the query against your own event schema and thresholds before using it for operational decisions.

How the SQL works

  1. 1The CASE expression counts only responses with a 5xx status while retaining the total request count in the same group.
  2. 2NULLIF prevents division by zero. The HAVING clause excludes routes with fewer than 20 requests, where a single error would create an unstable percentage.
  3. 3Use route templates rather than raw URLs so identifiers do not split one endpoint into thousands of groups.

Edge cases to decide

  • Choose a minimum request threshold that matches your traffic. Twenty requests is illustrative, not universal.
  • Separate expected 5xx responses from infrastructure failures if your API intentionally uses them for workflow control.
  • Segment by environment so staging traffic cannot change the production rate.

Recommended dashboard

  • Bar chart: error_rate_pct by route_template
  • Stat: total 5xx responses in the selected period
  • Table: newest failed requests with request_id and error_type

Alert guidance

Alert when error_rate_pct exceeds 2% for a route with at least 100 requests in the evaluation window.

Read alert setup

Put the recipe to work

Related instrumentation and guides

Continue the analysis

Run it on real events

Create a table, adapt the fields, and save the result

Start free, send structured events, and use the query result as a chart, shared dashboard widget, or alert input.

Get an API key