Merged in fix/logging-warmup-background (pull request #3232)
Fix/logging warmup background * fix: logging in background functions * fix logging * fix logging * fix logging Approved-by: Linus Flood
This commit is contained in:
committed by
Linus Flood
parent
26f3b5bdd0
commit
4174d43e16
@@ -49,7 +49,7 @@ export async function POST() {
|
|||||||
revalidateManuallyLogger.info(`Tag: ${tag}`)
|
revalidateManuallyLogger.info(`Tag: ${tag}`)
|
||||||
|
|
||||||
revalidateTag(tag)
|
revalidateTag(tag)
|
||||||
cacheClient.deleteKey(tag, { fuzzy: true })
|
cacheClient.deleteKey(tag, { fuzzy: false })
|
||||||
|
|
||||||
return Response.json({ revalidated: true, now: Date.now() })
|
return Response.json({ revalidated: true, now: Date.now() })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -10,28 +10,11 @@ import { safeTry } from "../utils/safeTry"
|
|||||||
|
|
||||||
import type { Config, Context } from "@netlify/functions"
|
import type { Config, Context } from "@netlify/functions"
|
||||||
|
|
||||||
|
const sentryLogger = Sentry.logger
|
||||||
await configureSentry()
|
await configureSentry()
|
||||||
|
|
||||||
export const config: Config = {
|
export const config: Config = {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
}
|
}
|
||||||
const warmupLogger = {
|
|
||||||
info: (message: string, ...args: unknown[]) => {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.log(`[WARMUP] ${message}`, ...args)
|
|
||||||
Sentry.logger.info(`[WARMUP] ${message}`, { ...args })
|
|
||||||
},
|
|
||||||
warn: (message: string, ...args: unknown[]) => {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.warn(`[WARMUP] ${message}`, ...args)
|
|
||||||
Sentry.logger.warn(`[WARMUP] ${message}`, { ...args })
|
|
||||||
},
|
|
||||||
error: (message: string, ...args: unknown[]) => {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.error(`[WARMUP] ${message}`, ...args)
|
|
||||||
Sentry.logger.error(`[WARMUP] ${message}`, { ...args })
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
const langs = ["en", "sv", "no", "fi", "da", "de"] as const
|
const langs = ["en", "sv", "no", "fi", "da", "de"] as const
|
||||||
export const warmupKeys = [
|
export const warmupKeys = [
|
||||||
@@ -51,7 +34,7 @@ export default async function WarmupHandler(
|
|||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
switch (error.message as ErrorCode) {
|
switch (error.message as ErrorCode) {
|
||||||
case ErrorCodes.WARMUP_DISABLED:
|
case ErrorCodes.WARMUP_DISABLED:
|
||||||
warmupLogger.warn("Warmup is disabled")
|
sentryLogger.warn("[warmup-background] Warmup is disabled")
|
||||||
return
|
return
|
||||||
case ErrorCodes.REQUEST_NOT_FOR_CURRENT_CONTEXT:
|
case ErrorCodes.REQUEST_NOT_FOR_CURRENT_CONTEXT:
|
||||||
// This is expected, this webhook will be called for all deployments
|
// This is expected, this webhook will be called for all deployments
|
||||||
@@ -59,14 +42,16 @@ export default async function WarmupHandler(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sentryLogger.error(`[warmup-background] Warmup failed '${error}'`, {
|
||||||
|
error: error.toString(),
|
||||||
|
})
|
||||||
|
|
||||||
warmupLogger.error("Warmup failed", error)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
warmupLogger.info("Request is valid, starting warmup")
|
sentryLogger.info("[warmup-background] Request is valid, starting warmup")
|
||||||
await performWarmup(context)
|
await performWarmup(context)
|
||||||
warmupLogger.info("Warmup completed")
|
sentryLogger.info("[warmup-background] Warmup completed")
|
||||||
}
|
}
|
||||||
|
|
||||||
async function validateRequest(
|
async function validateRequest(
|
||||||
@@ -112,7 +97,10 @@ async function validateRequest(
|
|||||||
}
|
}
|
||||||
signature = headerValue
|
signature = headerValue
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
warmupLogger.warn("Failed to parse signature", e)
|
sentryLogger.warn(
|
||||||
|
sentryLogger.fmt`[warmup-background] Failed to parse signature ${e}`,
|
||||||
|
{ error: e instanceof Error ? e.toString() : String(e) }
|
||||||
|
)
|
||||||
throw new Error(ErrorCodes.FAILED_TO_PARSE_SIGNATURE)
|
throw new Error(ErrorCodes.FAILED_TO_PARSE_SIGNATURE)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +119,10 @@ async function validateRequest(
|
|||||||
|
|
||||||
export async function performWarmup(context: Context) {
|
export async function performWarmup(context: Context) {
|
||||||
for (const key of warmupKeys) {
|
for (const key of warmupKeys) {
|
||||||
warmupLogger.info("Warming up cache", key)
|
sentryLogger.info(
|
||||||
|
sentryLogger.fmt`[warmup-background] Warming up cache '${key}'`
|
||||||
|
)
|
||||||
|
|
||||||
await callWarmup(key, context)
|
await callWarmup(key, context)
|
||||||
// allow api to catch up
|
// allow api to catch up
|
||||||
await timeout(1000)
|
await timeout(1000)
|
||||||
@@ -153,13 +144,13 @@ async function callWarmup(key: (typeof warmupKeys)[number], context: Context) {
|
|||||||
signal: AbortSignal.timeout(30_000),
|
signal: AbortSignal.timeout(30_000),
|
||||||
})
|
})
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
warmupLogger.error(
|
sentryLogger.error(
|
||||||
`Warmup failed '${url.href}' with error: ${response.status}: ${response.statusText}`
|
sentryLogger.fmt`[warmup-background] Warmup failed '${url.href}' with error: ${response.status}: ${response.statusText}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
warmupLogger.error(
|
sentryLogger.error(
|
||||||
`Warmup failed '${url.href}' with error: ${error instanceof Error ? error.message : error}`
|
sentryLogger.fmt`[warmup-background] Warmup failed '${url.href}' with error: ${error instanceof Error ? error.message : error}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -182,15 +173,18 @@ async function validateSignature(token: string, buffer: string) {
|
|||||||
const hashedBody = crypto.createHash("sha256").update(buffer).digest("hex")
|
const hashedBody = crypto.createHash("sha256").update(buffer).digest("hex")
|
||||||
|
|
||||||
if (!hasSha256(decoded)) {
|
if (!hasSha256(decoded)) {
|
||||||
warmupLogger.error(
|
sentryLogger.error(
|
||||||
"Decoded jwt does not contain sha256, unable to verify signature"
|
"[warmup-background] Decoded jwt does not contain sha256, unable to verify signature"
|
||||||
)
|
)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return decoded.sha256 === hashedBody
|
return decoded.sha256 === hashedBody
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
warmupLogger.error("Failed to validate signature", error)
|
sentryLogger.error(
|
||||||
|
sentryLogger.fmt`[warmup-background] Failed to validate signature ${error}`,
|
||||||
|
{ error: error instanceof Error ? error.toString() : String(error) }
|
||||||
|
)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,37 @@
|
|||||||
import { captureRequestError, init as sentryInit } from "@sentry/nextjs"
|
/* eslint-disable no-console */
|
||||||
|
import Sentry from "@sentry/nextjs"
|
||||||
|
|
||||||
export const denyUrls: (string | RegExp)[] = [
|
export const denyUrls: (string | RegExp)[] = [
|
||||||
// Ignore preview urls
|
// Ignore preview urls
|
||||||
/\/.{2}\/preview\//,
|
/\/.{2}\/preview\//,
|
||||||
]
|
]
|
||||||
|
|
||||||
export const onRequestError = captureRequestError
|
export const onRequestError = Sentry.captureRequestError
|
||||||
|
|
||||||
export async function configureSentry() {
|
export async function configureSentry() {
|
||||||
const sentryEnvironment = Netlify.env.get("SENTRY_ENVIRONMENT")
|
const sentryEnvironment = Netlify.env.get("NEXT_PUBLIC_SENTRY_ENVIRONMENT")
|
||||||
const sampleRate = Number(Netlify.env.get("SENTRY_SERVER_SAMPLERATE") ?? 0.01)
|
const sampleRate = Number(Netlify.env.get("SENTRY_SERVER_SAMPLERATE") ?? 0.01)
|
||||||
sentryInit({
|
Sentry.init({
|
||||||
dsn: "https://fe39c070b4154e2f9cc35f0e5de0aedb@o4508102497206272.ingest.de.sentry.io/4508102500286544",
|
dsn: "https://fe39c070b4154e2f9cc35f0e5de0aedb@o4508102497206272.ingest.de.sentry.io/4508102500286544",
|
||||||
environment: sentryEnvironment,
|
environment: sentryEnvironment,
|
||||||
enabled: sentryEnvironment !== "development",
|
enabled: sentryEnvironment !== "development",
|
||||||
tracesSampleRate: sampleRate,
|
tracesSampleRate: sampleRate,
|
||||||
denyUrls: denyUrls,
|
denyUrls: denyUrls,
|
||||||
enableLogs: true,
|
enableLogs: true,
|
||||||
|
beforeSendLog(log) {
|
||||||
|
switch (log.level) {
|
||||||
|
case "info":
|
||||||
|
console.log(log.message, log)
|
||||||
|
break
|
||||||
|
case "warn":
|
||||||
|
console.warn(log.message, log)
|
||||||
|
break
|
||||||
|
case "error":
|
||||||
|
console.error(log.message, log)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
return log
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user