Skip to content

Open-source status page, incident response and uptime monitoring

wip: metadata (#83)

Maximilian Kaske51d561919613
diff --git a/apps/web/src/app/api/checker/regions/_checker.ts b/apps/web/src/app/api/checker/regions/_checker.ts
index cfc961b1c..c7176f3ce 100644
--- a/apps/web/src/app/api/checker/regions/_checker.ts
+++ b/apps/web/src/app/api/checker/regions/_checker.ts
@@ -1,6 +1,5 @@
import { Receiver } from "@upstash/qstash/cloudflare";
import { nanoid } from "nanoid";
-import { z } from "zod";
import {
publishPingResponse,
@@ -26,8 +25,9 @@ const monitor = async (
cronTimestamp,
}: { latency: number; url: string; region: string; cronTimestamp: number },
) => {
+ const json = res.bodyUsed ? await res.json() : {};
await publishPingResponse(tb)({
- id: nanoid(),
+ id: nanoid(), // TBD: we don't need it
workspaceId: "openstatus",
pageId: "openstatus",
monitorId: "openstatusPing",
@@ -37,17 +37,7 @@ const monitor = async (
url,
region,
cronTimestamp,
- // TODO: discuss how to use the metadata properly
- // metadata: {
- // status: res.status,
- // statusText: res.statusText,
- // ok: res.ok,
- // headers: res.headers,
- // // body: res.body ? JSON.parse(res.body),
- // bodyUsed: res.bodyUsed,
- // redirected: res.redirected,
- // type: res.type,
- // },
+ metadata: JSON.stringify({ body: json }),
});
};
diff --git a/apps/web/src/components/data-table/data-table-row-action.tsx b/apps/web/src/components/data-table/data-table-row-action.tsx
index fcc704092..ce69309e3 100644
--- a/apps/web/src/components/data-table/data-table-row-action.tsx
+++ b/apps/web/src/components/data-table/data-table-row-action.tsx
@@ -2,19 +2,32 @@
import type { Row } from "@tanstack/react-table";
import { MoreHorizontal } from "lucide-react";
+import * as z from "zod";
-import { tbIngestPingResponse } from "@openstatus/tinybird";
+import { tbBuildResponseList } from "@openstatus/tinybird";
import { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
- DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
+// REMINDER: needed because `ResponseList` returns metadata as string, not as Record
+const schema = tbBuildResponseList.extend({
+ metadata: z.record(z.string(), z.unknown()).nullable(),
+});
+
interface DataTableRowActionsProps<TData> {
row: Row<TData>;
}
@@ -22,25 +35,53 @@ interface DataTableRowActionsProps<TData> {
export function DataTableRowActions<TData>({
row,
}: DataTableRowActionsProps<TData>) {
- const ping = tbIngestPingResponse.parse(row.original); // shouldn't it be tbBuild?
+ const ping = schema.parse(row.original);
return (
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <Button variant="ghost" className="h-8 w-8 p-0">
- <span className="sr-only">Open menu</span>
- <MoreHorizontal className="h-4 w-4" />
- </Button>
- </DropdownMenuTrigger>
- <DropdownMenuContent align="end">
- <DropdownMenuLabel>Actions</DropdownMenuLabel>
- <DropdownMenuItem
- onClick={() => navigator.clipboard.writeText(ping.id)}
- >
- Copy ping ID
- </DropdownMenuItem>
- <DropdownMenuSeparator />
- <DropdownMenuItem>View Response</DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
+ <Dialog>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ variant="ghost"
+ className="data-[state=open]:bg-accent h-8 w-8 p-0"
+ >
+ <span className="sr-only">Open menu</span>
+ <MoreHorizontal className="h-4 w-4" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuLabel>Actions</DropdownMenuLabel>
+ <DialogTrigger asChild>
+ <DropdownMenuItem>View Metadata</DropdownMenuItem>
+ </DialogTrigger>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>Metadata</DialogTitle>
+ <DialogDescription>
+ Additional informations to your Response like...
+ </DialogDescription>
+ </DialogHeader>
+ <div className="border-border rounded-lg border border-dashed p-4">
+ {!Boolean(Object.keys(ping.metadata || {}).length) ? (
+ <ul className="grid gap-1">
+ {Object.keys(ping.metadata || {}).map((key) => {
+ return (
+ <li key={key} className="text-sm">
+ <p>
+ <code>
+ {`"${key}"`}: {`${ping.metadata?.[key] || null}`}
+ </code>
+ </p>
+ </li>
+ );
+ })}
+ </ul>
+ ) : (
+ <p className="text-sm">No data collected</p>
+ )}
+ </div>
+ </DialogContent>
+ </Dialog>
);
}
diff --git a/packages/tinybird/datasources/ping_response.datasource b/packages/tinybird/datasources/ping_response.datasource
index f24682e06..fff8cdac5 100644
--- a/packages/tinybird/datasources/ping_response.datasource
+++ b/packages/tinybird/datasources/ping_response.datasource
@@ -10,7 +10,8 @@ SCHEMA >
`timestamp` Int64 `json:$.timestamp`,
`url` String `json:$.url`,
`workspaceId` String `json:$.workspaceId`,
- `cronTimestamp` Nullable(Int64) `json:$.cronTimestamp`
+ `cronTimestamp` Nullable(Int64) `json:$.cronTimestamp`,
+ `metadata` Nullable(String) `json:$.metadata`
ENGINE "MergeTree"
ENGINE_SORTING_KEY "id, timestamp, url, workspaceId"
diff --git a/packages/tinybird/src/client.ts b/packages/tinybird/src/client.ts
index 13bd5bebd..1d094436d 100644
--- a/packages/tinybird/src/client.ts
+++ b/packages/tinybird/src/client.ts
@@ -14,8 +14,6 @@ import {
export function publishPingResponse(tb: Tinybird) {
return tb.buildIngestEndpoint({
datasource: "ping_response__v2",
- // @ts-ignore TODO: this is caused by the metadata even though the
- // error says something regarding _input.cronTimestamp
event: tbIngestPingResponse,
});
}
diff --git a/packages/tinybird/src/validation.ts b/packages/tinybird/src/validation.ts
index ed460fe02..b7b790ac2 100644
--- a/packages/tinybird/src/validation.ts
+++ b/packages/tinybird/src/validation.ts
@@ -36,11 +36,7 @@ export const tbIngestPingResponse = z.object({
latency: z.number().int(), // in ms
cronTimestamp: z.number().int().optional().nullable().default(Date.now()),
url: z.string().url(),
- metadata: z
- .record(z.string(), z.unknown())
- .default({})
- .transform((t) => JSON.stringify(t))
- .optional(),
+ metadata: z.string().optional().default("{}").nullable(),
region: z.string().min(4).max(4),
});
@@ -60,7 +56,8 @@ export const tbBuildResponseList = z.object({
metadata: z
.string()
.default("{}")
- .transform((t) => JSON.parse(t)),
+ .transform((t) => JSON.parse(t))
+ .nullable(),
region: z.enum(availableRegions),
});
@@ -105,7 +102,7 @@ export const tbBuildMonitorList = z.object({
cronTimestamp: z.number().int(),
});
-export type Ping = z.infer<typeof tbIngestPingResponse>;
+export type Ping = z.infer<typeof tbBuildResponseList>;
export type Region = (typeof availableRegions)[number]; // TODO: rename type AvailabeRegion
export type Monitor = z.infer<typeof tbBuildMonitorList>;
export type ResponseListParams = z.infer<typeof tbParameterResponseList>;