logs.ts
This file
Path
infra/logs.ts
30 lines · 1.5 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
Merge observability into mainoscabrielbefore adoption1985111
lines 1–30why, one step back1// The stage's Workers Logs, read through Cloudflare's SQL API: smoke counts2// a deploy's errors with it, and the alerts in `alerts.ts` watch the same3// rows. Every query here reads counts by Worker, never a log's text, so4// nothing a log holds reaches a terminal or an alert.5import { resources } from "./stages.ts";6import type { Stage } from "./stages.ts";78export const SQL_API = "https://api.cloudflare.com/client/v4/analytics/sql";910// What reads a dataset through the SQL API: the token infra/.env.schema names11// needs it on the account CLOUDFLARE_ACCOUNT_ID names.12export const ANALYTICS_READ = "Account › Account Analytics › Read";1314// A Workers Logs row that is an error: an uncaught exception, whose `error`15// is set, or a line logged at error level. A typed refusal is the product16// working and is never logged at error level, so none counts.17export const ERRORED = "(level = 'error' OR error != '')";1819export const quoted = (text: string) => `'${text.replaceAll("'", "''")}'`;2021export const workersOf = (stage: Stage): readonly string[] => {22 const { platform, sandbox, web } = resources(stage);2324 return [platform, sandbox, web];25};2627// One row per Worker that logged an error, `{ scriptName, errors }`; the28// time range goes beside the query, so no instant is written into its text.29export const errorsQuery = (workers: readonly string[]) =>30 `SELECT scriptName, count() AS errors FROM logs.workersLogs WHERE scriptName IN (${workers.map(quoted).join(", ")}) AND ${ERRORED} GROUP BY scriptName`;