Skip to content

Commit

Permalink
Add support for Vercel log limit
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzuraw committed Nov 12, 2024
1 parent 6d7134b commit 42c537d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-crews-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@saleor/apps-logger": minor
---

If Vercel runtime transport log is exceeding Vercel log limit (4kb) error to Sentry will be logged as it won't be visible in log drain.
4 changes: 3 additions & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"@sentry/nextjs": "../../node_modules/@sentry/nextjs",
"eslint-config-saleor": "workspace:*",
"vite": "5.3.3",
"vitest": "1.6.0"
"vitest": "1.6.0",
"modern-errors": "7.0.1",
"modern-errors-serialize": "6.0.0"
},
"exports": {
".": {
Expand Down
8 changes: 8 additions & 0 deletions packages/logger/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ModernError from "modern-errors";
import modernErrorsSerialize from "modern-errors-serialize";

export const BaseError = ModernError.subclass("BaseError", {
plugins: [modernErrorsSerialize],
});

export const UnknownError = BaseError.subclass("UnknownError");
22 changes: 21 additions & 1 deletion packages/logger/src/logger-vercel-runtime-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ import { trace } from "@opentelemetry/api";
import * as Sentry from "@sentry/nextjs";
import { ILogObj, Logger } from "tslog";

import { BaseError, UnknownError } from "./errors";
import { LoggerContext } from "./logger-context";

const VercelMaximumLogSizeExceededError = BaseError.subclass("VercelMaximumLogSizeExceededError");

function isLogExceedingVercelLimit(inputString: string): boolean {
const byteLength = new TextEncoder().encode(inputString).length;

return byteLength > 4096; // Vercel serverless function log limit - 4KB
}

export const attachLoggerVercelRuntimeTransport = (
logger: Logger<ILogObj>,
appVersion: string,
Expand Down Expand Up @@ -39,6 +48,17 @@ export const attachLoggerVercelRuntimeTransport = (
},
});

if (isLogExceedingVercelLimit(stringifiedMessage)) {
Sentry.captureException(
new VercelMaximumLogSizeExceededError("Log message is exceeding Vercel limit", {
props: {
logName: log._meta.name,
logMessage: bodyMessage,
},
}),
);
}

// Prints Vercel log in proper level https://vercel.com/docs/observability/runtime-logs#level
if (_meta.logLevelName === "ERROR") {
console.error(stringifiedMessage);
Expand All @@ -53,7 +73,7 @@ export const attachLoggerVercelRuntimeTransport = (
console.log(stringifiedMessage);
} catch (error) {
Sentry.captureException(
new Error("Error during attaching Vercel transport", {
new UnknownError("Error during attaching Vercel transport", {
cause: error,
}),
);
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 42c537d

Please sign in to comment.