From 094fae38fcabc12b5d9870cc87d697a98aa470a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efra=C3=ADn=20Roch=C3=ADn?= Date: Fri, 21 Apr 2023 08:08:59 -0700 Subject: [PATCH] Add log messages on cold/hot requests (#8420) Co-authored-by: Keith Williams --- apps/web/middleware.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index ca5eb7e6a4..2d4cb61622 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -7,9 +7,16 @@ import { CONSOLE_URL, WEBAPP_URL, WEBSITE_URL } from "@calcom/lib/constants"; import { isIpInBanlist } from "@calcom/lib/getIP"; import { extendEventData, nextCollectBasicSettings } from "@calcom/lib/telemetry"; +let cold = true; + const middleware: NextMiddleware = async (req) => { const url = req.nextUrl; + const requestHeaders = new Headers(req.headers); + // This console.log is required to create a report in axios for hot and cold requests + console.log(cold ? "Cold Start" : "Hot Start"); + requestHeaders.set("x-cal-cold-start", cold ? "true" : "false"); + cold = false; if (!url.pathname.startsWith("/api")) { // // NOTE: When tRPC hits an error a 500 is returned, when this is received @@ -61,27 +68,19 @@ const middleware: NextMiddleware = async (req) => { } if (url.pathname.startsWith("/api/trpc/")) { - const requestHeaders = new Headers(req.headers); requestHeaders.set("x-cal-timezone", req.headers.get("x-vercel-ip-timezone") ?? ""); - return NextResponse.next({ - request: { - headers: requestHeaders, - }, - }); } if (url.pathname.startsWith("/auth/login")) { - const moreHeaders = new Headers(req.headers); // Use this header to actually enforce CSP, otherwise it is running in Report Only mode on all pages. - moreHeaders.set("x-csp-enforce", "true"); - return NextResponse.next({ - request: { - headers: moreHeaders, - }, - }); + requestHeaders.set("x-csp-enforce", "true"); } - return NextResponse.next(); + return NextResponse.next({ + request: { + headers: requestHeaders, + }, + }); }; export const config = {