diff --git a/package.json b/package.json index bf920db..ebec18a 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "build:openapi": "ts-node scripts/build-openapi-schema.ts", "postinstall": "pnpm db:generate", "check:type": "tsc --noEmit --skipLibCheck --module esnext", - "db:push": "prisma db push", "db:generate": "prisma generate", "db:migrate:dev": "prisma migrate dev", "db:migrate:apply": "prisma migrate deploy", diff --git a/prisma/migrations/20231031143357_add_telemetry_event_payload/migration.sql b/prisma/migrations/20231031143357_add_telemetry_event_payload/migration.sql new file mode 100644 index 0000000..2213f3e --- /dev/null +++ b/prisma/migrations/20231031143357_add_telemetry_event_payload/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "TelemetryEvent" ADD COLUMN "payload" JSON; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 93ae2e0..b1a5810 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -205,6 +205,7 @@ model TelemetryEvent { eventName String? @db.VarChar(100) urlOrigin String @db.VarChar(500) urlPath String @db.VarChar(500) + payload Json? @db.Json // Other payload info get from query params, should be a object createdAt DateTime @default(now()) @db.Timestamptz(6) session TelemetrySession @relation(fields: [sessionId], references: [id], onUpdate: Cascade, onDelete: Cascade) diff --git a/src/server/model/telemetry.ts b/src/server/model/telemetry.ts index 72e4a97..b59560d 100644 --- a/src/server/model/telemetry.ts +++ b/src/server/model/telemetry.ts @@ -5,11 +5,12 @@ import { getRequestInfo } from '../utils/detect'; import { prisma } from './_client'; export async function recordTelemetryEvent(req: Request) { - const url = req.query.url ?? req.headers.referer; + const { url = req.headers.referer, name, ...others } = req.query; + if (!(url && typeof url === 'string')) { return; } - const eventName = req.query.name ? String(req.query.name) : undefined; + const eventName = name ? String(name) : undefined; const session = await findSession(req, url); if (!session) { @@ -22,6 +23,7 @@ export async function recordTelemetryEvent(req: Request) { } const { origin, pathname } = new URL(url); + const payload = Object.keys(others).length > 0 ? others : undefined; await prisma.telemetryEvent.create({ data: { @@ -30,6 +32,7 @@ export async function recordTelemetryEvent(req: Request) { eventName, urlOrigin: origin, urlPath: pathname, + payload, }, }); }