Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
🚑 fly was fun (#389)
Thibault Le Ouay79f9a1d6d6d5
diff --git a/apps/server/src/checker/alerting.ts b/apps/server/src/checker/alerting.tsindex 5315bf710..df78199e2 100644--- a/apps/server/src/checker/alerting.ts+++ b/apps/server/src/checker/alerting.ts@@ -19,6 +19,7 @@ export async function catchTooManyRetry(payload: Payload) {}export const triggerAlerting = async ({ monitorId }: { monitorId: string }) => {+ console.log(`triggerAlerting for ${monitorId}`);const notifications = await db.select().from(schema.notificationsToMonitors)diff --git a/apps/server/src/checker/checker.ts b/apps/server/src/checker/checker.tsindex 76d2b783b..c3a93864a 100644--- a/apps/server/src/checker/checker.ts+++ b/apps/server/src/checker/checker.ts@@ -19,6 +19,9 @@ export const monitor = async ({}) => {const { monitorId, cronTimestamp, url, workspaceId } = monitorInfo;+ console.log(+ `publishing ping response for ${url} with status ${statusCode} and latency ${latency} and monitorId ${monitorId} `,+ );await publishPingResponse({id: nanoid(), // TBD: we don't need ittimestamp: Date.now(),@@ -37,22 +40,21 @@ export const checker = async (data: Payload) => {const res = await ping(data);const endTime = Date.now();const latency = endTime - startTime;- await monitor({ monitorInfo: data, latency, statusCode: res.status });- if (res.ok && !res.redirected) {+ if (res.ok) {+ await monitor({ monitorInfo: data, latency, statusCode: res.status });if (data?.status === "error") {await updateMonitorStatus({monitorId: data.monitorId,status: "active",});}-- if (!res.ok || (res.ok && !res.redirected)) {- if (data?.status === "active") {- await updateMonitorStatus({- monitorId: data.monitorId,- status: "error",- });- }+ } else {+ console.log(`error for ${data.url} with status ${res.status}`);+ if (data?.status === "active") {+ await updateMonitorStatus({+ monitorId: data.monitorId,+ status: "error",+ });}}};diff --git a/apps/server/src/checker/index.ts b/apps/server/src/checker/index.tsindex f32817d7a..23d32f150 100644--- a/apps/server/src/checker/index.ts+++ b/apps/server/src/checker/index.ts@@ -1,23 +1,51 @@+import { Receiver } from "@upstash/qstash";import { Hono } from "hono";+import { env } from "../env";+import { catchTooManyRetry } from "./alerting";import { checker } from "./checker";-import { middleware } from "./middleware";+import { payloadSchema } from "./schema";import type { Payload } from "./schema";export type Variables = {payload: Payload;};-export const checkerRoute = new Hono<{ Variables: Variables }>();+const r = new Receiver({+ currentSigningKey: env.QSTASH_CURRENT_SIGNING_KEY,+ nextSigningKey: env.QSTASH_NEXT_SIGNING_KEY,+});-checkerRoute.use("/*", middleware);+export const checkerRoute = new Hono<{ Variables: Variables }>();// TODO: only use checkerRoute.post("/checker", checker);checkerRoute.post("/checker", async (c) => {- const payload = c.get("payload");+ const json = await c.req.json();++ const result = payloadSchema.safeParse(json);++ if (!result.success) {+ console.error(result.error);+ return c.text("Unprocessable Entity", 422);+ }++ if (Number(c.req.header("Upstash-Retried") || 0) >= 2) {+ console.log(+ `catchTooManyRetry for ${result.data.url} with retry nb ${c.req.header(+ "Upstash-Retried",+ )}`,+ );+ catchTooManyRetry(result.data);+ return c.text("Ok", 200); // needs to be 200, otherwise qstash will retry+ }+try {- checker(payload);+ console.log(`start checker for ${result.data.url}`);+ checker(result.data);+ console.log(`end checker for ${result.data.url}`);+ return c.text("Ok", 200);} catch (e) {console.error(e);+ return c.text("Internal Server Error", 500);}});diff --git a/apps/server/src/checker/middleware.ts b/apps/server/src/checker/middleware.tsdeleted file mode 100644index 16064d1d8..000000000--- a/apps/server/src/checker/middleware.ts+++ /dev/null@@ -1,46 +0,0 @@-import { Receiver } from "@upstash/qstash";-import type { Context, Next } from "hono";--import { env } from "../env";-import { catchTooManyRetry } from "./alerting";-import type { Variables } from "./index";-import { payloadSchema } from "./schema";--const r = new Receiver({- currentSigningKey: env.QSTASH_CURRENT_SIGNING_KEY,- nextSigningKey: env.QSTASH_NEXT_SIGNING_KEY,-});--export async function middleware(- c: Context<{ Variables: Variables }, "/*", {}>,- next: Next,-) {- const json = await c.req.json();-- const isValid = r.verify({- signature: c.req.header("Upstash-Signature") || "",- body: JSON.stringify(json),- });-- if (!isValid) {- return c.text("Unauthorized", 401);- }-- const result = payloadSchema.safeParse(json);-- if (!result.success) {- console.error(result.error);- return c.text("Unprocessable Entity", 422);- }-- /**- * Alert user after third retry, on forth try- */- if (c.req.header("Upstash-Retried") === "3") {- catchTooManyRetry(result.data);- return c.text("", 200); // needs to be 200, otherwise qstash will retry- }-- c.set("payload", result.data);- await next();-}diff --git a/apps/server/src/index.ts b/apps/server/src/index.tsindex a666946fa..0570a8d6b 100644--- a/apps/server/src/index.ts+++ b/apps/server/src/index.ts@@ -1,5 +1,6 @@import { Hono } from "hono";+import { checkerRoute } from "./checker";import { env } from "./env";import { publicRoute } from "./public";import { api } from "./v1";@@ -27,6 +28,7 @@ app.get("/ping", (c) => c.json({ ping: "pong", region: env.FLY_REGION }));*/app.route("/v1", api);+app.route("/", checkerRoute);if (process.env.NODE_ENV === "development") {app.showRoutes();}diff --git a/packages/tinybird/src/validation.ts b/packages/tinybird/src/validation.tsindex 8f182172d..c59f4eed9 100644--- a/packages/tinybird/src/validation.ts+++ b/packages/tinybird/src/validation.ts@@ -37,7 +37,7 @@ export const tbIngestPingResponse = z.object({latency: z.number(), // in mscronTimestamp: z.number().int().optional().nullable().default(Date.now()),url: z.string().url(),- region: z.string().min(4).max(4), // REMINDER: won't work on fy+ region: z.string().min(3).max(4), // REMINDER: won't work on fy});/**