🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
perf(tracking): cache tracker preflights and quiet hot-path logs (#1311)
Goldflag8244ee8fc5af
diff --git a/docker-compose.cloud.yml b/docker-compose.cloud.ymlindex abfa2a4e7..ce55fdba4 100644--- a/docker-compose.cloud.yml+++ b/docker-compose.cloud.yml@@ -157,6 +157,11 @@ services:redis:condition: service_healthyrestart: unless-stopped+ logging:+ driver: "json-file"+ options:+ max-size: "100m"+ max-file: "5"ports:- "3001:3001"@@ -183,6 +188,11 @@ services:depends_on:- backendrestart: unless-stopped+ logging:+ driver: "json-file"+ options:+ max-size: "100m"+ max-file: "5"ports:- "3002:3002"diff --git a/docker-compose.yml b/docker-compose.ymlindex 92ce80c2d..6aa5ae9ab 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -150,6 +150,11 @@ services:retries: 5start_period: 10srestart: unless-stopped+ logging:+ driver: "json-file"+ options:+ max-size: "100m"+ max-file: "5"client:image: ghcr.io/rybbit-io/rybbit-client:${IMAGE_TAG:-latest}@@ -173,6 +178,11 @@ services:depends_on:- backendrestart: unless-stopped+ logging:+ driver: "json-file"+ options:+ max-size: "100m"+ max-file: "5"volumes:clickhouse-data:diff --git a/server/src/api/sessionReplay/recordSessionReplay.test.ts b/server/src/api/sessionReplay/recordSessionReplay.test.tsindex 928920403..fa5effbb6 100644--- a/server/src/api/sessionReplay/recordSessionReplay.test.ts+++ b/server/src/api/sessionReplay/recordSessionReplay.test.ts@@ -9,6 +9,7 @@ const mocks = vi.hoisted(() => ({isSiteOverLimit: vi.fn(),isSiteWithoutReplay: vi.fn(),recordEvents: vi.fn(),+ loggerDebug: vi.fn(),loggerInfo: vi.fn(),loggerError: vi.fn(),}));@@ -88,6 +89,7 @@ function createRequest(overrides: RequestOverrides = {}): ReplayRequest {ip: overrides.ip ?? "203.0.113.10",log: {error: mocks.loggerError,+ debug: mocks.loggerDebug,info: mocks.loggerInfo,},} as unknown as ReplayRequest;diff --git a/server/src/api/sessionReplay/recordSessionReplay.ts b/server/src/api/sessionReplay/recordSessionReplay.tsindex cec1319eb..ab0c5de9d 100644--- a/server/src/api/sessionReplay/recordSessionReplay.ts+++ b/server/src/api/sessionReplay/recordSessionReplay.ts@@ -67,7 +67,7 @@ export async function recordSessionReplay(const { siteId, sessionReplay } = siteConfiguration ?? {};if (!sessionReplay) {- request.log.info({ siteId }, "Skipping session replay event because replay is not enabled");+ request.log.debug({ siteId }, "Skipping session replay event because replay is not enabled");return reply.status(200).send({ success: true, message: "Session replay not enabled" });}@@ -81,14 +81,14 @@ export async function recordSessionReplay(// Check if the site has exceeded its monthly limitif (usageService.isSiteOverLimit(Number(siteId))) {- request.log.info({ siteId }, "Skipping session replay event because the Site is over its monthly limit");+ request.log.debug({ siteId }, "Skipping session replay event because the Site is over its monthly limit");return reply.status(200).send("Site over monthly limit, event not tracked");}// Check if the site can record replays: the plan may not include them (e.g. enabled// before a downgrade from Pro) or the monthly replay quota may be exhaustedif (usageService.isSiteWithoutReplay(Number(siteId))) {- request.log.info({ siteId }, "Skipping session replay event because replay is unavailable for plan or quota");+ request.log.debug({ siteId }, "Skipping session replay event because replay is unavailable for plan or quota");return reply.status(200).send({ success: true, message: "Session replay not available for plan or quota" });}@@ -108,7 +108,7 @@ export async function recordSessionReplay(});if (exclusionDecision.excluded) {- request.log.info(+ request.log.debug({ siteId, exclusionReason: exclusionDecision.reason },"Skipping session replay event because a Site Exclusion Decision matched");diff --git a/server/src/lib/cors.test.ts b/server/src/lib/cors.test.tsindex a88a55246..dbb4e5bf3 100644--- a/server/src/lib/cors.test.ts+++ b/server/src/lib/cors.test.ts@@ -5,7 +5,9 @@ import {createCorsOptionsDelegate,createRejectUntrustedOriginHook,getTrustedCorsOrigins,+ isTrackerCorsPath,normalizeCorsOrigin,+ TRACKER_PREFLIGHT_MAX_AGE_SECONDS,} from "./cors.js";async function buildCorsTestApp(env: NodeJS.ProcessEnv) {@@ -18,6 +20,8 @@ async function buildCorsTestApp(env: NodeJS.ProcessEnv) {app.delete("/api/sites/:siteId", async () => ({ success: true }));app.get("/api/sites/:siteId/sessions", async () => ({ data: [] }));app.post("/api/track", async () => ({ success: true }));+ app.post("/api/session-replay/record/:siteId", async () => ({ success: true }));+ app.post("/api/site/:siteId/feature-flags/evaluate", async () => ({ flags: {} }));app.post("/api/mcp", async () => ({ success: true }));app.get("/api/version", async () => ({ version: "0.0.0" }));@@ -234,6 +238,71 @@ describe("CORS policy", () => {});});+describe("tracker preflight caching", () => {+ const env = { NODE_ENV: "production", BASE_URL: "https://app.rybbit.example" };++ function preflight(app: Awaited<ReturnType<typeof buildCorsTestApp>>, url: string, origin: string, method = "POST") {+ return app.inject({+ method: "OPTIONS",+ url,+ headers: {+ origin,+ "access-control-request-method": method,+ "access-control-request-headers": "content-type",+ },+ });+ }++ it("lets browsers cache tracker preflights from any origin", async () => {+ const app = await buildCorsTestApp(env);++ try {+ for (const url of [+ "/api/track",+ "/api/session-replay/record/abc123",+ "/api/site/abc123/feature-flags/evaluate",+ ]) {+ const res = await preflight(app, url, "https://customer.example");++ expect(res.statusCode).toBe(204);+ expect(res.headers["access-control-allow-origin"]).toBe("https://customer.example");+ expect(res.headers["access-control-max-age"]).toBe(String(TRACKER_PREFLIGHT_MAX_AGE_SECONDS));+ }+ } finally {+ await app.close();+ }+ });++ it("keeps the browser default for dashboard and other public routes", async () => {+ const app = await buildCorsTestApp(env);++ try {+ const management = await preflight(app, "/api/sites/1", "https://app.rybbit.example", "DELETE");+ expect(management.statusCode).toBe(204);+ expect(management.headers["access-control-max-age"]).toBeUndefined();++ const sessions = await preflight(app, "/api/sites/81/sessions", "https://docs.rybbit.com", "GET");+ expect(sessions.statusCode).toBe(204);+ expect(sessions.headers["access-control-max-age"]).toBeUndefined();+ } finally {+ await app.close();+ }+ });++ it("matches only the endpoints the tracking script calls", () => {+ expect(isTrackerCorsPath("/api/track")).toBe(true);+ expect(isTrackerCorsPath("/api/identify")).toBe(true);+ expect(isTrackerCorsPath("/api/session-replay/record/abc")).toBe(true);+ expect(isTrackerCorsPath("/api/site/tracking-config/abc")).toBe(true);+ expect(isTrackerCorsPath("/api/site/abc/feature-flags/evaluate")).toBe(true);++ expect(isTrackerCorsPath("/api/version")).toBe(false);+ expect(isTrackerCorsPath("/api/sites/abc/sessions")).toBe(false);+ expect(isTrackerCorsPath("/api/sites/abc/feature-flags/evaluate")).toBe(false);+ expect(isTrackerCorsPath("/api/site/abc/feature-flags/evaluate/extra")).toBe(false);+ });+});+describe("trusted CORS origins", () => {it("normalizes quoted URLs to origins", () => {expect(normalizeCorsOrigin('"https://rybbit.example.com/path"')).toBe("https://rybbit.example.com");@@ -255,5 +324,4 @@ describe("trusted CORS origins", () => {})).toEqual(["https://rybbit.example.com", "http://localhost:3002", "http://127.0.0.1:3002"]);});-});diff --git a/server/src/lib/cors.ts b/server/src/lib/cors.tsindex 949eb27cb..5049d47bf 100644--- a/server/src/lib/cors.ts+++ b/server/src/lib/cors.ts@@ -17,6 +17,7 @@ type CorsOptions = {methods: string[];allowedHeaders: string[];credentials: boolean;+ maxAge?: number;};type CorsDelegateCallback = (error: Error | null, options?: CorsOptions) => void;@@ -26,6 +27,10 @@ const commonCorsOptions = {allowedHeaders: corsAllowedHeaders,};+// Chromium caps Access-Control-Max-Age at 2 hours; without the header it caches a+// preflight for only 5 seconds, so nearly every tracker POST paid for an OPTIONS.+export const TRACKER_PREFLIGHT_MAX_AGE_SECONDS = 7200;+const devTrustedOrigins = ["http://localhost:3002", "http://127.0.0.1:3002"];const unsafeMethods = new Set(["POST", "PUT", "PATCH", "DELETE"]);@@ -105,6 +110,19 @@ export function isPublicCorsPath(path: string): boolean {);}+// Endpoints the tracking script calls from customer pages. Their CORS policy+// depends only on the origin, so browsers may cache the preflight. Dashboard+// routes keep the default so a policy change reaches them immediately.+export function isTrackerCorsPath(path: string): boolean {+ return (+ path === "/api/track" ||+ path === "/api/identify" ||+ path.startsWith("/api/session-replay/record/") ||+ path.startsWith("/api/site/tracking-config/") ||+ /^\/api\/site\/[^/]+\/feature-flags\/evaluate$/.test(path)+ );+}+export function getCorsOptionsForRequest(request: FastifyRequest, env: NodeJS.ProcessEnv = process.env): CorsOptions {const requestOrigin = normalizeCorsOrigin(request.headers.origin);if (!requestOrigin) {@@ -115,7 +133,8 @@ export function getCorsOptionsForRequest(request: FastifyRequest, env: NodeJS.Pr};}- if (isPublicCorsPath(getRequestPath(request))) {+ const path = getRequestPath(request);+ if (isPublicCorsPath(path)) {// These paths are reachable from any origin (e.g. embedded widgets), but they// are also hit by the authenticated dashboard, which sends cookies. Reflect any// origin, but only allow credentials for trusted origins so the dashboard's@@ -125,6 +144,7 @@ export function getCorsOptionsForRequest(request: FastifyRequest, env: NodeJS.Pr...commonCorsOptions,origin: true,credentials: getTrustedCorsOrigins(env).includes(requestOrigin),+ ...(isTrackerCorsPath(path) ? { maxAge: TRACKER_PREFLIGHT_MAX_AGE_SECONDS } : {}),};}diff --git a/server/src/lib/logger/LOGGING.md b/server/src/lib/logger/LOGGING.mdindex 71c104db2..e71fa1773 100644--- a/server/src/lib/logger/LOGGING.md+++ b/server/src/lib/logger/LOGGING.md@@ -38,7 +38,7 @@ Fastify's default request logging is disabled and replaced by `requestLogging.ts| handled response with status 500 or greater | `error` | `Request completed with server error` || handled response with status 400–499 | `warn` | `Request completed with client error` || successful response | `info` | `Request completed` |-| successful ingestion or static-asset request | `debug` | `Request completed` |+| successful tracker request or CORS preflight | `debug` | `Request completed` |Routes configured with `logLevel: "silent"`, such as health checks, emit no lifecycle event. Thrown failures are marked internally so the later response hook does not log the same lifecycle failure twice.diff --git a/server/src/lib/logger/requestLogging.test.ts b/server/src/lib/logger/requestLogging.test.tsindex aa20a420e..8d249fea0 100644--- a/server/src/lib/logger/requestLogging.test.ts+++ b/server/src/lib/logger/requestLogging.test.ts@@ -1,3 +1,4 @@+import cors from "@fastify/cors";import Fastify, { type FastifyInstance } from "fastify";import { pino } from "pino";import { afterEach, describe, expect, it } from "vitest";@@ -64,6 +65,56 @@ describe("request lifecycle logging", () => {]);});+ it("logs successful tracker config and flag requests at debug", async () => {+ const { app, events } = createApp();+ app.get("/api/site/tracking-config/:siteId", () => ({ sessionReplay: false }));+ app.post("/api/site/:siteId/feature-flags/evaluate", () => ({ flags: {} }));++ await app.inject({ method: "GET", url: "/api/site/tracking-config/abc" });+ await app.inject({ method: "POST", url: "/api/site/abc/feature-flags/evaluate" });++ expect(requestEvents(events)).toEqual([+ expect.objectContaining({ level: 20, route: "/api/site/tracking-config/:siteId", statusCode: 200 }),+ expect.objectContaining({ level: 20, route: "/api/site/:siteId/feature-flags/evaluate", statusCode: 200 }),+ ]);+ });++ it("keeps tracker config failures at their normal level", async () => {+ const { app, events } = createApp();+ app.get("/api/site/tracking-config/:siteId", (_request, reply) => reply.status(404).send({ error: "not found" }));++ await app.inject({ method: "GET", url: "/api/site/tracking-config/missing" });++ expect(requestEvents(events)).toEqual([+ expect.objectContaining({ level: 40, statusCode: 404, msg: "Request completed with client error" }),+ ]);+ });++ it("logs answered CORS preflights at debug and rejected ones at warn", async () => {+ const { app, events } = createApp();+ await app.register(cors, {+ delegator: (request, callback) =>+ callback(null, { origin: request.headers.origin === "https://allowed.example" }),+ });+ app.post("/api/track", () => ({ success: true }));++ await app.inject({+ method: "OPTIONS",+ url: "/api/track",+ headers: { origin: "https://allowed.example", "access-control-request-method": "POST" },+ });+ await app.inject({+ method: "OPTIONS",+ url: "/api/track",+ headers: { origin: "https://blocked.example", "access-control-request-method": "POST" },+ });++ expect(requestEvents(events)).toEqual([+ expect.objectContaining({ level: 20, method: "OPTIONS", statusCode: 204, msg: "Request completed" }),+ expect.objectContaining({ level: 40, method: "OPTIONS", statusCode: 404 }),+ ]);+ });+it("logs handled response failures from their status code", async () => {const { app, events } = createApp();app.get("/unavailable", (_request, reply) => reply.status(503).send({ error: "unavailable" }));diff --git a/server/src/lib/logger/requestLogging.ts b/server/src/lib/logger/requestLogging.tsindex 62d0402b4..aa330efa2 100644--- a/server/src/lib/logger/requestLogging.ts+++ b/server/src/lib/logger/requestLogging.ts@@ -1,10 +1,14 @@import type { FastifyError, FastifyInstance, FastifyReply, FastifyRequest } from "fastify";+// Routes the tracking script hits on customer pages. Their successes are logged at+// debug; failures keep their normal level.const HIGH_VOLUME_SUCCESS_ROUTES = new Set(["/api/identify","/api/metrics.js","/api/replay.js","/api/script.js",+ "/api/site/:siteId/feature-flags/evaluate",+ "/api/site/tracking-config/:siteId","/api/track",]);@@ -16,7 +20,10 @@ function requestPath(request: FastifyRequest): string {return request.url.split("?", 1)[0];}-function isHighVolumeSuccessRoute(route: string): boolean {+function isHighVolumeSuccess(request: FastifyRequest, route: string): boolean {+ // CORS preflights are answered by @fastify/cors before routing; a rejected one+ // is a 404 and still logs as a client error.+ if (request.method === "OPTIONS") return true;return HIGH_VOLUME_SUCCESS_ROUTES.has(route) || route.endsWith("/session-replay/record/:siteId");}@@ -69,7 +76,7 @@ export function registerRequestLogging(fastify: FastifyInstance): void {request.log.error(context, "Request completed with server error");} else if (reply.statusCode >= 400) {request.log.warn(context, "Request completed with client error");- } else if (isHighVolumeSuccessRoute(context.route)) {+ } else if (isHighVolumeSuccess(request, context.route)) {request.log.debug(context, "Request completed");} else {request.log.info(context, "Request completed");diff --git a/server/src/services/tracker/ingestEvent.ts b/server/src/services/tracker/ingestEvent.tsindex 9e8d2cbef..0ed556144 100644--- a/server/src/services/tracker/ingestEvent.ts+++ b/server/src/services/tracker/ingestEvent.ts@@ -58,12 +58,12 @@ export async function ingestEvent(trackingRequest: TrackingRequest): Promise<Ing});if (exclusionDecision.excluded) {- logger.info({ siteId: payload.site_id, exclusionReason: exclusionDecision.reason }, "Site exclusion matched");+ logger.debug({ siteId: payload.site_id, exclusionReason: exclusionDecision.reason }, "Site exclusion matched");return { status: "excluded", exclusion: exclusionDecision };}if (usageService.isSiteOverLimit(site.siteId)) {- logger.info({ siteId: payload.site_id }, "Skipping event - site over monthly limit");+ logger.debug({ siteId: payload.site_id }, "Skipping event - site over monthly limit");return { status: "over_limit" };}diff --git a/server/src/services/tracker/trackEvent.ts b/server/src/services/tracker/trackEvent.tsindex d692786cf..e5f1f510c 100644--- a/server/src/services/tracker/trackEvent.ts+++ b/server/src/services/tracker/trackEvent.ts@@ -29,7 +29,9 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {const trackingRequest = await resolveTrackingRequest(request, validationResult.data);if (!trackingRequest) {- logger.warn({ siteId: validationResult.data.site_id }, "Site not found");+ // The 404 lifecycle line already logs at warn; deleted sites that still embed+ // the script would otherwise log a second warn on every event.+ logger.debug({ siteId: validationResult.data.site_id }, "Site not found");return reply.status(404).send({success: false,error: "Site not found",