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-27Tested with Apache DataFusion 45.2.0

Reviewed by the Telemetry product team on . SQL compatibility, event contract, synthetic output, and operational caveats. Review standards and ownership

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 read-only query is planned and executed against an empty typed table with Apache DataFusion 45.2.0. The deterministic sample output is synthetic and reviewed separately; validate field types, thresholds, and business definitions against your own data. Read the testing methodology.

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/checkout25520
/api/search2514
/api/profile2000

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

5xx error rate by route: static chart of synthetic error_rate_pct values from the Calculate API Error Rate by Route example result
Indexable SVG of the deterministic example output. Download it for an article, runbook, or design review with attribution.

Reproduce the example

Download the public fixture

The JSON bundle includes the typed event contract, reproducible input rows, exact SQL, expected output, review notes, and engine version. The CSV contains the displayed result.

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