Open History to see the last commit
1import * as Alchemy from "alchemy";
2import * as Axiom from "@alchemy.run/axiom";
3import * as Output from "alchemy/Output";
4import * as Config from "effect/Config";
5import * as Effect from "effect/Effect";
6import * as Option from "effect/Option";
7
8import { relayResourceNameForStage } from "./deploymentConfig.ts";
9
10const RUNBOOK_URL =
11 "https://github.com/pingdotgg/t3code/blob/main/docs/operations/05_relay-observability.md#alerts";
12
13const LINK_ROUTE = `"/v1/client/environment-links"`;
14const TUNNEL_ROUTES = `"/v1/environments/:environmentId/tunnel", "/v1/environments/:environmentId/tunnel/recovery"`;
15// The Cloudflare SDK client spans record the host but not the full URL.
16const CLOUDFLARE_API = `['attributes.server.address'] == "api.cloudflare.com"`;
17const STATUS = `toint(['attributes.http.response.status_code'])`;
18// The tunnel reaper records its counters on this span every five minutes,
19// including while cleanup is off.
20const SWEEP = `name == "relay.managed_endpoint_reaper.sweep"`;
21const sweepCounter = (field: string) =>
22 `toint(['attributes.custom']['relay.managed_endpoint_reaper.${field}'])`;
23
24// Status codes, not span errors: a request cut off by the relay deadline ends
25// as an interrupted span with no error flag, but still answers 504.
26// Below `minimum` requests the rate reads 0, so a quiet window cannot page.
27const serverFailurePercent = (dataset: string, routes: string, minimum: number) =>
28 [
29 `['${dataset}']`,
30 `| where kind == "server" and ['attributes.http.request.method'] == "POST" and ['attributes.http.route'] in (${routes})`,
31 `| summarize total = count(), failed = countif(${STATUS} >= 500)`,
32 `| project failure_pct = iff(total >= ${minimum}, 100.0 * failed / total, 0.0)`,
33 ].join("\n");
34
35type Severity = "page" | "warn";
36
37interface RelayMonitor {
38 readonly id: string;
39 readonly severity: Severity;
40 readonly name: string;
41 /** Why it fires and the healthy baseline. The runbook link is appended. */
42 readonly description: string;
43 readonly query: (dataset: string) => string;
44 readonly operator: "Above" | "AboveOrEqual" | "Below";
45 readonly threshold: number;
46 readonly rangeMinutes: number;
47 readonly intervalMinutes?: number;
48 readonly triggerFromNRuns?: number;
49 readonly alertOnNoData?: boolean;
50}
51
52// Thresholds come from 2026-10-16..19 prod data: the worst healthy window,
53// and the outage (Cloudflare API 429s stalling every provision).
54// Paging monitors need two bad runs in a row; warnings catch the slow
55// degradations an outage threshold has to ignore.
56const relayMonitors: ReadonlyArray<RelayMonitor> = [
57 {
58 id: "RelayLinkFailureRateMonitor",
59 severity: "page",
60 name: "T3 Connect relay: links failing",
61 description:
62 "Share of POST /v1/client/environment-links (turning on T3 Connect) answering 5xx over 15 min. Healthy: median 11%, worst 75%. #16810: 87-96%.",
63 query: (dataset) => serverFailurePercent(dataset, LINK_ROUTE, 20),
64 operator: "Above",
65 threshold: 80,
66 rangeMinutes: 15,
67 triggerFromNRuns: 2,
68 },
69 {
70 id: "RelayTunnelFailureRateMonitor",
71 severity: "page",
72 name: "T3 Connect relay: tunnel recovery failing",
73 description:
74 "Share of host tunnel registration and recovery answering 5xx over 15 min. Hosts whose tunnel drops cannot come back. Healthy: median 4%, worst 52%. #16810: 83-94%.",
75 query: (dataset) => serverFailurePercent(dataset, TUNNEL_ROUTES, 200),
76 operator: "Above",
77 threshold: 60,
78 rangeMinutes: 15,
79 triggerFromNRuns: 2,
80 },
81 {
82 id: "RelayConnectReachabilityMonitor",
83 severity: "page",
84 name: "T3 Connect relay: environments unreachable",
85 description:
86 "Share of environments clients tried to reach that connected at least once in 30 min. Offline hosts keep this below 100%, and retry storms against them do not move it. Healthy: 65-72%.",
87 query: (dataset) =>
88 [
89 `['${dataset}']`,
90 `| where name == "relay.environment_connector.connect"`,
91 `| extend environment = tostring(['attributes.custom']['relay.environment_id'])`,
92 `| summarize environments = dcount(environment), connected = dcountif(environment, error != true)`,
93 `| project reachable_pct = iff(environments >= 1000, 100.0 * connected / environments, 100.0)`,
94 ].join("\n"),
95 operator: "Below",
96 threshold: 55,
97 rangeMinutes: 30,
98 triggerFromNRuns: 2,
99 },
100 {
101 id: "RelayPushFailureRateMonitor",
102 severity: "page",
103 name: "T3 Connect relay: push delivery failing",
104 description:
105 "Share of APNs and FCM delivery jobs failing over 15 min, e.g. an expired APNs key or FCM credential. Healthy: APNs under 0.3%, FCM under 4%.",
106 query: (dataset) =>
107 [
108 `['${dataset}']`,
109 `| where name in ("relay.apns_deliveries.process_signed_job", "relay.fcm.process")`,
110 `| summarize total = count(), failed = countif(error == true)`,
111 `| project failure_pct = iff(total >= 100, 100.0 * failed / total, 0.0)`,
112 ].join("\n"),
113 operator: "Above",
114 threshold: 10,
115 rangeMinutes: 15,
116 triggerFromNRuns: 2,
117 },
118 {
119 id: "RelayCloudflareRateLimitedMonitor",
120 severity: "page",
121 name: "T3 Connect relay: Cloudflare API rate limited",
122 description:
123 "Cloudflare API 429s, plus calls the relay refused while paused after one, per 10 min. Sustained at this level, links and recoveries stall. Peak 429s before #16810: 1,286; during it: ~11k.",
124 query: (dataset) =>
125 `['${dataset}']\n| where (${CLOUDFLARE_API} and ${STATUS} == 429) or name == "relay.cloudflare_api_pause.rejected"\n| summarize rate_limited = count()`,
126 operator: "AboveOrEqual",
127 threshold: 500,
128 rangeMinutes: 10,
129 triggerFromNRuns: 2,
130 },
131 {
132 id: "RelayTrafficDroppedMonitor",
133 severity: "page",
134 name: "T3 Connect relay: traffic dropped",
135 description:
136 "Relay requests per 10 min collapsed: Worker down, broken deploy, or trace export stopped. Healthy minimum: ~69k.",
137 query: (dataset) => `['${dataset}']\n| where kind == "server"\n| summarize requests = count()`,
138 operator: "Below",
139 threshold: 10_000,
140 rangeMinutes: 10,
141 triggerFromNRuns: 2,
142 alertOnNoData: true,
143 },
144 {
145 id: "RelayCloudflareRateLimitWarningMonitor",
146 severity: "warn",
147 name: "T3 Connect relay: Cloudflare API near its limit",
148 description:
149 "Any sustained Cloudflare API 429s or paused calls. 429 bursts ran for days before #16810; raise the account limit or cut call volume before they grow.",
150 query: (dataset) =>
151 `['${dataset}']\n| where (${CLOUDFLARE_API} and ${STATUS} == 429) or name == "relay.cloudflare_api_pause.rejected"\n| summarize rate_limited = count()`,
152 operator: "AboveOrEqual",
153 threshold: 20,
154 rangeMinutes: 10,
155 triggerFromNRuns: 2,
156 },
157 {
158 id: "RelayLinkChronicFailureMonitor",
159 severity: "warn",
160 name: "T3 Connect relay: links degraded",
161 description:
162 "Share of links answering 5xx over 6 h, mostly 9 s deadline 504s from slow provisioning. Healthy days: 10-18%. Degraded days before #16810: 28-33%.",
163 query: (dataset) => serverFailurePercent(dataset, LINK_ROUTE, 100),
164 operator: "Above",
165 threshold: 20,
166 rangeMinutes: 360,
167 intervalMinutes: 30,
168 },
169 {
170 id: "RelayDatabaseLatencyMonitor",
171 severity: "warn",
172 name: "T3 Connect relay: database slow",
173 description:
174 "p90 Postgres query time over 15 min, in ms. Every relay request runs several queries in sequence, so this sets how close provisioning gets to the 9 s deadline. Healthy: ~200 ms.",
175 query: (dataset) =>
176 `['${dataset}']\n| where name == "postgres"\n| summarize p90 = percentile(duration, 90)\n| project p90_ms = p90 / 1000000`,
177 operator: "Above",
178 threshold: 500,
179 rangeMinutes: 15,
180 triggerFromNRuns: 2,
181 },
182 {
183 id: "RelayTunnelCleanupStoppedMonitor",
184 // Pages because nothing else surfaces it: unnoticed, it ran 2026-11-15..18 while 27k+ idle tunnels piled up.
185 severity: "page",
186 name: "T3 Connect relay: tunnel cleanup stopped",
187 description:
188 "Tunnel cleanup sweeps that finished in 2 h. Healthy: 24. The cron is not running, or every sweep fails, as during the 2026-10-19 Cloudflare 429s and from 2026-11-15 when listing tunnels timed out. Idle tunnels pile up and are billed meanwhile.",
189 query: (dataset) =>
190 `['${dataset}']\n| where ${SWEEP} and error != true\n| summarize sweeps = count()`,
191 operator: "Below",
192 threshold: 1,
193 rangeMinutes: 120,
194 intervalMinutes: 15,
195 triggerFromNRuns: 2,
196 alertOnNoData: true,
197 },
198 {
199 id: "RelayTunnelCleanupFailingMonitor",
200 severity: "warn",
201 name: "T3 Connect relay: tunnel deletions failing",
202 description:
203 "Cloudflare tunnel deletions that failed in 30 min. Healthy: 0; the worst 30 min so far had 3. The relay logs a warning with the Cloudflare error for each.",
204 query: (dataset) =>
205 `['${dataset}']\n| where ${SWEEP}\n| summarize failed = sum(${sweepCounter("failed")})`,
206 operator: "Above",
207 threshold: 10,
208 rangeMinutes: 30,
209 triggerFromNRuns: 2,
210 },
211 {
212 id: "RelayAccountDeletionFailingMonitor",
213 // Pages because a stuck deletion is a privacy request we have accepted and not carried out.
214 severity: "page",
215 name: "T3 Connect relay: account deletions failing",
216 description:
217 "Failed account deletion passes, plus sweeps that failed outright, in 2 h. The cron retries every 5 min, so one stuck deletion or a broken sweep fails 24 times in 2 h; a one-off Cloudflare blip fails once or twice. Healthy: 0. Pending rows and their attempts are in relay_account_deletions.",
218 query: (dataset) =>
219 `['${dataset}']\n| where name == "relay.account_deletion.process_pending"\n| summarize passes = sum(toint(['attributes.custom']['relay.account_deletion.failed'])), sweeps = countif(error == true)\n| project failed = coalesce(passes, 0) + sweeps`,
220 operator: "AboveOrEqual",
221 threshold: 12,
222 rangeMinutes: 120,
223 intervalMinutes: 15,
224 triggerFromNRuns: 2,
225 },
226];
227
228interface QueryChart {
229 readonly id: string;
230 readonly name: string;
231 readonly type: "TimeSeries" | "Table";
232 readonly query: { readonly apl: string };
233}
234
235const timeSeries = (id: string, name: string, apl: string): QueryChart => ({
236 id,
237 name,
238 type: "TimeSeries",
239 query: { apl },
240});
241
242const relayDashboardCharts = (dataset: string): ReadonlyArray<QueryChart> => {
243 const source = `['${dataset}']`;
244 const linkRequests = `${source}\n| where kind == "server" and ['attributes.http.request.method'] == "POST" and ['attributes.http.route'] == ${LINK_ROUTE}`;
245 const tunnelRequests = `${source}\n| where kind == "server" and ['attributes.http.request.method'] == "POST" and ['attributes.http.route'] in (${TUNNEL_ROUTES})`;
246 const clientConnections = `${source}\n| where name == "relay.connection.attempt" and error != true\n| extend surface = tostring(['resource.custom']['t3.client.surface'])\n| where isnotempty(surface)`;
247 return [
248 timeSeries(
249 "traffic",
250 "Requests and agent activity publishes",
251 `${source}\n| where kind == "server" or name == "relay.agent_activity_publisher.publish"\n| summarize requests = countif(kind == "server"), publishes = countif(name == "relay.agent_activity_publisher.publish") by bin_auto(_time)`,
252 ),
253 timeSeries(
254 "links",
255 "Links by outcome",
256 `${linkRequests}\n| summarize succeeded = countif(${STATUS} < 400), rejected = countif(${STATUS} >= 400 and ${STATUS} < 500), failed = countif(${STATUS} >= 500) by bin_auto(_time)`,
257 ),
258 timeSeries(
259 "link-latency",
260 "Link latency (9 s deadline)",
261 `${linkRequests}\n| summarize p50 = percentile(duration, 50), p90 = percentile(duration, 90) by bin_auto(_time)`,
262 ),
263 timeSeries(
264 "reachability",
265 "Environments reachable per 30 min (%)",
266 `${source}\n| where name == "relay.environment_connector.connect"\n| extend environment = tostring(['attributes.custom']['relay.environment_id'])\n| summarize environments = dcount(environment), connected = dcountif(environment, error != true) by bin(_time, 30m)\n| project _time, reachable_pct = 100.0 * connected / environments`,
267 ),
268 timeSeries(
269 "tunnel",
270 "Tunnel registration and recovery by outcome",
271 `${tunnelRequests}\n| summarize succeeded = countif(${STATUS} < 400), rejected = countif(${STATUS} >= 400 and ${STATUS} < 500), failed = countif(${STATUS} >= 500) by bin_auto(_time)`,
272 ),
273 timeSeries(
274 "cloudflare-api",
275 "Cloudflare API calls by status",
276 // Calls refused during a rate-limit pause never reach Cloudflare, so
277 // they are their own series or a storm would look like a quiet period.
278 `${source}\n| where ${CLOUDFLARE_API} or name == "relay.cloudflare_api_pause.rejected"\n| summarize count() by bin_auto(_time), status = case(name == "relay.cloudflare_api_pause.rejected", "paused", isnull(${STATUS}), "no response", tostring(${STATUS}))`,
279 ),
280 timeSeries(
281 "provision-paths",
282 "Tunnel provisions by path",
283 `${source}\n| where name == "relay.managed_endpoint_provider.provision"\n| summarize reused_ready_tunnel = countif(error != true and tobool(['attributes.custom']['relay.managed_endpoint.reused_ready_tunnel']) == true), full_provision = countif(error != true and isnull(['attributes.custom']['relay.managed_endpoint.reused_ready_tunnel'])), failed = countif(error == true) by bin_auto(_time)`,
284 ),
285 timeSeries(
286 "postgres",
287 "Postgres query time",
288 `${source}\n| where name == "postgres"\n| summarize p50 = percentile(duration, 50), p90 = percentile(duration, 90) by bin_auto(_time)`,
289 ),
290 timeSeries(
291 "push",
292 "Push delivery jobs",
293 `${source}\n| where name in ("relay.apns_deliveries.process_signed_job", "relay.fcm.process")\n| summarize delivered = countif(error != true), failed = countif(error == true) by bin_auto(_time), service = iff(name == "relay.fcm.process", "fcm", "apns")`,
294 ),
295 timeSeries(
296 "tunnel-cleanup",
297 "Tunnel cleanup per sweep",
298 `${source}\n| where ${SWEEP}\n| summarize deleted = sum(${sweepCounter("deleted")}), deleted_legacy = sum(${sweepCounter("deletedLegacy")}), would_delete = sum(${sweepCounter("wouldDelete")}), would_delete_legacy = sum(${sweepCounter("wouldDeleteLegacy")}), failed = sum(${sweepCounter("failed")}), sweeps_failed = countif(error == true) by bin_auto(_time)`,
299 ),
300 // Cloudflare's own counts for this stage, including tunnels the reaper
301 // skips, so this is the idle tunnels still billed.
302 timeSeries(
303 "tunnel-backlog",
304 "Idle tunnels in Cloudflare",
305 `${source}\n| where ${SWEEP}\n| summarize down = max(${sweepCounter("totalDown")}), never_connected = max(${sweepCounter("totalInactive")}) by bin_auto(_time)`,
306 ),
307 timeSeries(
308 "server-errors",
309 "5xx by route",
310 `${source}\n| where kind == "server" and ${STATUS} >= 500\n| summarize count() by bin_auto(_time), route = tostring(['attributes.http.route']), status = ${STATUS}`,
311 ),
312 {
313 id: "link-failures",
314 name: "Link failures by reason",
315 type: "Table",
316 query: {
317 apl: `${source}\n| where name == "relay.environment_linker.link" and error == true\n| extend message = tostring(['status.message'])\n| summarize failures = count() by reason = case(message contains "failed during '", strcat("provisioning: ", extract("failed during '([^']+)'", 1, message)), message startswith "Managed tunnel limit reached", "managed tunnel limit reached", substring(message, 0, 80))\n| top 10 by failures`,
318 },
319 },
320 // Connecting, as users see it: web, desktop and mobile export their
321 // connection spans into this dataset, in the same traces as the relay's
322 // and the host's spans. An attempt lasts until initial sync finishes.
323 timeSeries(
324 "connect-time",
325 "Connecting: time to connected by surface",
326 `${clientConnections}\n| summarize p50 = percentile(duration, 50), p90 = percentile(duration, 90) by bin_auto(_time), surface`,
327 ),
328 timeSeries(
329 "connect-outcomes",
330 "Connecting: attempts by outcome",
331 `${source}\n| where name == "relay.connection.attempt" and isnotempty(['resource.custom']['t3.client.surface'])\n| summarize connected = countif(error != true), failed = countif(error == true) by bin_auto(_time)`,
332 ),
333 {
334 id: "connect-phases",
335 name: "Connecting: where the time goes",
336 type: "Table",
337 query: {
338 // Failed direct route checks stay in: waiting out an unreachable LAN
339 // address before falling back to the relay is the slowest step.
340 apl: [
341 source,
342 `| where isnotempty(['resource.custom']['t3.client.surface'])`,
343 `| extend url = tostring(['attributes.url.full'])`,
344 `| extend phase = case(`,
345 ` name == "ConnectionDriver.checkRoute", "0. Direct route check, before any relay fallback",`,
346 ` name == "relay.connection.cloudSessionToken.resolve", "1. Clerk session token",`,
347 ` name == "clientRuntime.managedRelay.exchangeAccessToken", "2. Relay token exchange",`,
348 ` kind == "client" and url startswith "https://relay.t3.codes/v1/environments/" and url endswith "/connect", "3. Relay connect, minting a host credential",`,
349 ` name == "environment.authorization.descriptor", "4. Host descriptor",`,
350 ` name == "environment.authorization.accessToken.exchange", "5. Host token exchange",`,
351 ` name == "clientRuntime.connection.remote.createDpopSocketUrl", "6. Socket ticket",`,
352 ` name == "environment.initialSync", "7. Initial sync",`,
353 ` name == "relay.connection.attempt", "Total: time to connected",`,
354 ` "")`,
355 `| where phase != "" and (error != true or phase startswith "0.")`,
356 `| summarize calls = count(), p50 = percentile(duration, 50), p90 = percentile(duration, 90) by phase`,
357 `| order by phase asc`,
358 ].join("\n"),
359 },
360 },
361 timeSeries(
362 "relay-latency-by-continent",
363 "Relay token and connect latency by user continent (p50)",
364 `${source}\n| where kind == "server" and ['attributes.http.request.method'] == "POST" and (['attributes.http.route'] == "/v1/client/dpop-token" or ['attributes.http.route'] == "/v1/environments/:environmentId/connect")\n| extend continent = tostring(['attributes.custom']['relay.edge.continent'])\n| where isnotempty(continent)\n| summarize p50 = percentile(duration, 50) by bin_auto(_time), continent`,
365 ),
366 ];
367};
368
369export const notifierConfig = (name: string) =>
370 Config.String(name).pipe(
371 Config.option,
372 Config.map(Option.flatMap((value) => Option.liftPredicate(value.trim(), Boolean))),
373 );
374
375/**
376 * A deployment opts in by naming the notifier that pages (incident.io for
377 * prod). Personal stages and forks leave it unset and get no monitors or
378 * dashboard. Warnings go to their own notifier when one is set, and are only
379 * visible in Axiom otherwise.
380 */
381export const RelayMonitoring = Effect.fn("RelayMonitoring")(function* (traces: Axiom.Dataset) {
382 const { stage } = yield* Alchemy.Stack;
383 const page = yield* notifierConfig("RELAY_ALERT_NOTIFIER_ID");
384 if (Option.isNone(page)) return;
385 const notifiers: Record<Severity, Option.Option<string>> = {
386 page,
387 warn: yield* notifierConfig("RELAY_WARNING_NOTIFIER_ID"),
388 };
389
390 for (const monitor of relayMonitors) {
391 yield* Axiom.Monitor(monitor.id, {
392 name: stage === "prod" ? monitor.name : `[${stage}] ${monitor.name}`,
393 description: `${monitor.description} Runbook: ${RUNBOOK_URL}`,
394 type: "Threshold",
395 aplQuery: Output.map(traces.name, monitor.query),
396 operator: monitor.operator,
397 threshold: monitor.threshold,
398 intervalMinutes: monitor.intervalMinutes ?? 5,
399 rangeMinutes: monitor.rangeMinutes,
400 triggerFromNRuns: monitor.triggerFromNRuns ?? 1,
401 alertOnNoData: monitor.alertOnNoData ?? false,
402 notifierIds: Option.toArray(notifiers[monitor.severity]),
403 });
404 }
405
406 yield* Axiom.Dashboard("RelayHealthDashboard", {
407 dashboard: Output.map(traces.name, (dataset) => {
408 const charts = relayDashboardCharts(dataset);
409 return {
410 name: relayResourceNameForStage("t3-code-relay-health", stage),
411 // Shared with the whole org. An empty owner only means that for API
412 // tokens; deploys with a personal token would make it private.
413 owner: "X-AXIOM-EVERYONE",
414 description: `T3 Connect relay health. Alerts and runbook: ${RUNBOOK_URL}`,
415 refreshTime: 60,
416 schemaVersion: 2,
417 timeWindowStart: "qr-now-24h",
418 timeWindowEnd: "qr-now",
419 charts: [...charts],
420 // Two charts per row on Axiom's 12-column grid.
421 layout: charts.map((chart, index) => ({
422 i: chart.id,
423 x: (index % 2) * 6,
424 y: Math.floor(index / 2) * 6,
425 w: 6,
426 h: 6,
427 })),
428 };
429 }),
430 });
431});