Skip to content
TypeScript

feat(relay): watch tunnel cleanup on the relay dashboard and warnings (#22105)

Julius Marminge2d88519c2f48
diff --git a/docs/operations/relay-observability.md b/docs/operations/relay-observability.md
index 3f174e6882d..f2cd94e333b 100644
--- a/docs/operations/relay-observability.md
+++ b/docs/operations/relay-observability.md
@@ -147,6 +147,15 @@ account limit.
**Traffic dropped.** The Worker is down, a deploy broke it, or spans stopped reaching Axiom. Check
`https://relay.t3.codes/health` first.
+**Tunnel cleanup stopped / tunnel deletions failing.** Warnings for the five-minute reaper that deletes
+idle Cloudflare tunnels. "Stopped" counts sweeps that finished, so it also fires when every sweep
+fails; a sweep fails when listing tunnels does, usually behind Cloudflare 429s. Idle tunnels are
+billed until the reaper catches up, which the "Idle tunnels in Cloudflare" chart shows. A failed
+sweep records no counts, so while sweeps fail the chart stops at the last good one; check the tunnel
+list in the Cloudflare dashboard for current numbers. For failed
+deletions, read the relay's "Failed to delete an inactive managed tunnel" warnings. To pause cleanup,
+see [Managed tunnel cleanup rollout](release.md#managed-tunnel-cleanup-rollout).
+
**Links degraded / database slow.** Warnings for slow degradation. Link failures above 20% over six
hours have so far meant provisioning running into the 9 s deadline, with Postgres latency as the
usual cause.
diff --git a/infra/relay/src/monitoring.ts b/infra/relay/src/monitoring.ts
index f791dfc4502..fb9769cfde3 100644
--- a/infra/relay/src/monitoring.ts
+++ b/infra/relay/src/monitoring.ts
@@ -14,6 +14,11 @@ const LINK_ROUTE = `"/v1/client/environment-links"`;
const TUNNEL_ROUTES = `"/v1/environments/:environmentId/tunnel", "/v1/environments/:environmentId/tunnel/recovery"`;
const CLOUDFLARE_API = `['attributes.url.full'] startswith "https://api.cloudflare.com"`;
const STATUS = `toint(['attributes.http.response.status_code'])`;
+// The tunnel reaper records its counters on this span every five minutes,
+// including while cleanup is off.
+const SWEEP = `name == "relay.managed_endpoint_reaper.sweep"`;
+const sweepCounter = (field: string) =>
+ `toint(['attributes.custom']['relay.managed_endpoint_reaper.${field}'])`;
// Status codes, not span errors: a request cut off by the relay deadline ends
// as an interrupted span with no error flag, but still answers 504.
@@ -173,6 +178,33 @@ const relayMonitors: ReadonlyArray<RelayMonitor> = [
rangeMinutes: 15,
triggerFromNRuns: 2,
},
+ {
+ id: "RelayTunnelCleanupStoppedMonitor",
+ severity: "warn",
+ name: "T3 Connect relay: tunnel cleanup stopped",
+ description:
+ "Tunnel cleanup sweeps that finished in 30 min. Healthy: 6. The cron is not running, or every sweep fails, as during the 2026-10-19 Cloudflare 429s when listing tunnels timed out. Idle tunnels pile up and are billed meanwhile.",
+ query: (dataset) =>
+ `['${dataset}']\n| where ${SWEEP} and error != true\n| summarize sweeps = count()`,
+ operator: "Below",
+ threshold: 1,
+ rangeMinutes: 30,
+ triggerFromNRuns: 2,
+ alertOnNoData: true,
+ },
+ {
+ id: "RelayTunnelCleanupFailingMonitor",
+ severity: "warn",
+ name: "T3 Connect relay: tunnel deletions failing",
+ description:
+ "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.",
+ query: (dataset) =>
+ `['${dataset}']\n| where ${SWEEP}\n| summarize failed = sum(${sweepCounter("failed")})`,
+ operator: "Above",
+ threshold: 10,
+ rangeMinutes: 30,
+ triggerFromNRuns: 2,
+ },
];
interface QueryChart {
@@ -230,6 +262,18 @@ const relayDashboardCharts = (dataset: string): ReadonlyArray<QueryChart> => {
"Push delivery jobs",
`${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")`,
),
+ timeSeries(
+ "tunnel-cleanup",
+ "Tunnel cleanup per sweep",
+ `${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)`,
+ ),
+ // Cloudflare's own counts for this stage, including tunnels the reaper
+ // skips, so this is the idle tunnels still billed.
+ timeSeries(
+ "tunnel-backlog",
+ "Idle tunnels in Cloudflare",
+ `${source}\n| where ${SWEEP}\n| summarize down = max(${sweepCounter("totalDown")}), never_connected = max(${sweepCounter("totalInactive")}) by bin_auto(_time)`,
+ ),
timeSeries(
"server-errors",
"5xx by route",