Skip to content

Commit

Permalink
Add vercel log drain
Browse files Browse the repository at this point in the history
  • Loading branch information
poulch committed Dec 16, 2024
1 parent 5cc367a commit 46155dc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 75 deletions.
2 changes: 2 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ import ModernErrorsSerialize from "modern-errors-serialize";
export const BaseError = ModernError.subclass("BaseError", {
plugins: [ModernErrorsSerialize],
});

export const UnknownError = BaseError.subclass("UnknownError");
10 changes: 8 additions & 2 deletions src/lib/logger/create-logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import packageJson from "../../../package.json";
import { attachLoggerConsoleTransport } from "./logger-console-transport";
import { createLogger, logger } from "./logger";
import { attachLoggerOtelTransport } from "./logger-otel-transport";
import { attachLoggerSentryTransport } from "./logger-sentry-transport";
import {attachLoggerVercelRuntimeTransport} from "@/lib/logger/logger-vercel-transport";
import { loggerContext } from "@/logger-context";

logger.settings.maskValuesOfKeys = ["metadata", "username", "password", "apiKey"];
Expand All @@ -14,7 +14,13 @@ if (process.env.NODE_ENV !== "production") {
if (typeof window === "undefined") {
attachLoggerSentryTransport(logger);

attachLoggerOtelTransport(logger, packageJson.version, loggerContext);
if (process.env.NODE_ENV === "production") {
attachLoggerVercelRuntimeTransport(
logger,
packageJson.version,
loggerContext
);
}
}

export { createLogger, logger };
73 changes: 0 additions & 73 deletions src/lib/logger/logger-otel-transport.ts

This file was deleted.

82 changes: 82 additions & 0 deletions src/lib/logger/logger-vercel-transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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,
loggerContext?: LoggerContext,
) => {
logger.attachTransport((log) => {
try {
const { message, attributes, _meta } = log;

const bodyMessage = log._meta.name ? `[${log._meta.name}] ${message}` : message;

const stringifiedMessage = JSON.stringify({
message: bodyMessage,
...(loggerContext?.getRawContext() ?? {}),
...attributes,
deployment: {
environment: process.env.ENV,
},
otel: {
span_id: trace.getActiveSpan()?.spanContext().spanId,
trace_id: trace.getActiveSpan()?.spanContext().traceId,
timestamp: _meta.date.getTime(),
},
"commit-sha": process.env.VERCEL_GIT_COMMIT_SHA,
service: {
name: process.env.OTEL_SERVICE_NAME,
version: appVersion,
},
_meta: {
..._meta,
// used to filter out log in log drain
source: "saleor-app",
},
});

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);
return;
}

if (_meta.logLevelName === "WARN") {
console.warn(stringifiedMessage);
return;
}

console.log(stringifiedMessage);
} catch (error) {
Sentry.captureException(
new UnknownError("Error during attaching Vercel transport", {
cause: error,
}),
);
}
});
};

0 comments on commit 46155dc

Please sign in to comment.