Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
feat: real data (#87)
Thibault Le Ouay08ebd5c308ae
diff --git a/apps/web/src/app/api/checker/cron/_cron.ts b/apps/web/src/app/api/checker/cron/_cron.tsindex bf66303ae..0a780db95 100644--- a/apps/web/src/app/api/checker/cron/_cron.ts+++ b/apps/web/src/app/api/checker/cron/_cron.ts@@ -1,10 +1,16 @@import { Client } from "@upstash/qstash/cloudflare";import type { z } from "zod";-import { selectMonitorSchema } from "@openstatus/db/src/schema";+import { and, db, eq } from "@openstatus/db";+import {+ monitor,+ monitorsToPages,+ selectMonitorSchema,+} from "@openstatus/db/src/schema";import { availableRegions } from "@openstatus/tinybird";import { env } from "@/env.mjs";+import type { payloadSchema } from "../schema";const periodicityAvailable = selectMonitorSchema.pick({ periodicity: true });@@ -21,32 +27,51 @@ export const cron = async ({const timestamp = Date.now();// FIXME: Wait until db is ready- // const result = await db- // .select()- // .from(monitor)- // .where(eq(monitor.frequency, frequency));-- // for (const row of result) {- // for (const region of availableRegions) {- // await c.publishJSON({- // url: `${DEFAULT_URL}/api/checker/region/${region}`,- // body: {- // url: row.url,- // cronTimestamp: timestamp, // used to group all region requests- // },- // });- // }- // }-- // Right now we are just checking the ping endpoint- for (const region of availableRegions) {- await c.publishJSON({- url: `${DEFAULT_URL}/api/checker/regions/${region}`,- body: {+ const result = await db+ .select()+ .from(monitor)+ .where(and(eq(monitor.periodicity, periodicity), eq(monitor.active, true)))+ .all();++ for (const row of result) {+ // could be improved with a single query+ const allPages = await db+ .select()+ .from(monitorsToPages)+ .where(eq(monitorsToPages.monitorId, row.id))+ .all();++ for (const region of availableRegions) {+ const payload: z.infer<typeof payloadSchema> = {+ workspaceId: String(row.workspaceId),+ monitorId: String(row.id),+ url: row.url,+ cronTimestamp: timestamp,+ pageIds: allPages.map((p) => String(p.pageId)),+ };++ await c.publishJSON({+ url: `${DEFAULT_URL}/api/checker/region/${region}`,+ body: payload,+ });+ }+ }+ // our first legacy monitor+ if (periodicity === "10m") {+ // Right now we are just checking the ping endpoint+ for (const region of availableRegions) {+ const payload: z.infer<typeof payloadSchema> = {+ workspaceId: "openstatus",+ monitorId: "openstatusPing",url: `${DEFAULT_URL}/api/ping`,- // FIXME: what is the concret timezone format? In DE, is 02h later- cronTimestamp: timestamp, // used to group all region requests - can be also cronId- },- });+ cronTimestamp: timestamp,+ pageIds: ["openstatus"],+ };++ await c.publishJSON({+ url: `${DEFAULT_URL}/api/checker/regions/${region}`,+ body: payload,+ });+ }}};diff --git a/apps/web/src/app/api/checker/regions/_checker.ts b/apps/web/src/app/api/checker/regions/_checker.tsindex c7176f3ce..3edd387a3 100644--- a/apps/web/src/app/api/checker/regions/_checker.ts+++ b/apps/web/src/app/api/checker/regions/_checker.ts@@ -1,6 +1,8 @@import { Receiver } from "@upstash/qstash/cloudflare";import { nanoid } from "nanoid";+import type { z } from "zod";+import { page } from "@openstatus/db/src/schema";import {publishPingResponse,tbIngestPingResponse,@@ -8,6 +10,7 @@ import {} from "@openstatus/tinybird";import { env } from "@/env.mjs";+import { payloadSchema } from "../schema";export const monitorSchema = tbIngestPingResponse.pick({url: true,@@ -18,27 +21,36 @@ const tb = new Tinybird({ token: env.TINY_BIRD_API_KEY });const monitor = async (res: Response,- {- latency,- url,- region,- cronTimestamp,- }: { latency: number; url: string; region: string; cronTimestamp: number },+ monitorInfo: z.infer<typeof payloadSchema>,+ region: string,+ latency: number,) => {const json = res.bodyUsed ? await res.json() : {};- await publishPingResponse(tb)({- id: nanoid(), // TBD: we don't need it- workspaceId: "openstatus",- pageId: "openstatus",- monitorId: "openstatusPing",- timestamp: Date.now(),- statusCode: res.status,- latency,- url,- region,- cronTimestamp,- metadata: JSON.stringify({ body: json }),- });+ if (monitorInfo.pageIds.length > 0) {+ for (const pageId of monitorInfo.pageIds) {+ await publishPingResponse(tb)({+ ...monitorInfo,+ id: nanoid(), // TBD: we don't need it+ pageId: pageId,+ timestamp: Date.now(),+ statusCode: res.status,+ latency,+ region,+ metadata: JSON.stringify({ body: json }),+ });+ }+ } else {+ await publishPingResponse(tb)({+ ...monitorInfo,+ id: nanoid(), // TBD: we don't need it+ pageId: "",+ timestamp: Date.now(),+ statusCode: res.status,+ latency,+ region,+ metadata: JSON.stringify({ body: json }),+ });+ }};export const checker = async (request: Request, region: string) => {@@ -57,23 +69,16 @@ export const checker = async (request: Request, region: string) => {throw new Error("Could not parse request");}- const result = monitorSchema.safeParse(jsonData);+ const result = payloadSchema.safeParse(jsonData);if (!result.success) {throw new Error("Invalid response body");}- const { url, cronTimestamp } = result.data;-const startTime = Date.now();- const res = await fetch(url, { cache: "no-store" });+ const res = await fetch(result.data.url, { cache: "no-store" });const endTime = Date.now();const latency = endTime - startTime;- await monitor(res, {- latency,- url,- region,- cronTimestamp: cronTimestamp || Date.now(), // HOT FIXME: remove!- });+ await monitor(res, result.data, region, latency);};diff --git a/apps/web/src/app/api/checker/schema.ts b/apps/web/src/app/api/checker/schema.tsnew file mode 100644index 000000000..08ba18a7d--- /dev/null+++ b/apps/web/src/app/api/checker/schema.ts@@ -0,0 +1,9 @@+import { z } from "zod";++export const payloadSchema = z.object({+ workspaceId: z.string(),+ monitorId: z.string(),+ url: z.string(),+ cronTimestamp: z.number(),+ pageIds: z.array(z.string()),+});