Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LoggerVercelTransport and use it in AvaTax app #1622

Merged
merged 6 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/empty-candles-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@saleor/apps-logger": patch
---

Added new transport `LoggerVercelTransport`. It is currently in experimental stage but it can be used to send logs directly to Vercel log drain. This transport has optional argument of `loggerContext` - if used you need to make sure that function is executed only on the server.

Usage:

```ts
import { logger } from "@saleor/apps-logger";
import { attachLoggerVercelTransport } from "@saleor/apps-logger/node";
import { loggerContext } from "./logger-context";

attachLoggerVercelTransport(logger, loggerContext);
```
5 changes: 5 additions & 0 deletions .changeset/forty-tools-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-avatax": patch
---

Added new `LoggerVercelTransport` support. It will help us send logs to our infrastructure without need of OTEL unstable logs API.
4 changes: 2 additions & 2 deletions apps/avatax/src/lib/app-configuration-logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ describe("AppConfigurationLogger", () => {
);

expect(mockInfo).toHaveBeenCalledWith("Received configuration", {
address: JSON.stringify({
address: {
city: "test",
country: "test",
zip: "10111",
state: "NY",
street: "test",
}),
Comment on lines -84 to -90
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting that it works but technically js doesn't preserve keys order (or guarantees it), so it may be flaky - one day stringified object can be different

but non blocking, if it works, we dont have to touch it

},
appConfigName: "config",
channelSlug: "default-channel",
companyCode: "test",
Expand Down
2 changes: 1 addition & 1 deletion apps/avatax/src/lib/app-configuration-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class AppConfigurationLogger {
appConfigName: resolvedAvataxConfig.config.name,
shippingTaxCode: resolvedAvataxConfig.config.shippingTaxCode,
companyCode: resolvedAvataxConfig.config.companyCode,
address: JSON.stringify(resolvedAvataxConfig.config.address),
address: resolvedAvataxConfig.config.address,
isSandbox: resolvedAvataxConfig.config.isSandbox,
isAutocommit: resolvedAvataxConfig.config.isAutocommit,
isDocumentRecordingEnabled: resolvedAvataxConfig.config.isDocumentRecordingEnabled,
Expand Down
19 changes: 10 additions & 9 deletions apps/avatax/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { attachLoggerConsoleTransport, createLogger, logger } from "@saleor/apps-logger";

import packageJson from "../package.json";

logger.settings.maskValuesOfKeys = ["metadata", "username", "password", "apiKey"];

if (process.env.NODE_ENV !== "production") {
attachLoggerConsoleTransport(logger);
}

if (typeof window === "undefined") {
import("@saleor/apps-logger/node").then(
async ({ attachLoggerOtelTransport, attachLoggerSentryTransport, LoggerContext }) => {
const loggerContext = await import("./logger-context").then((m) => m.loggerContext);
// Don't remove require - it's necessary for proper logger initialization
const {
attachLoggerSentryTransport,
attachLoggerVercelTransport,
} = require("@saleor/apps-logger/node");

attachLoggerSentryTransport(logger);

attachLoggerSentryTransport(logger);
attachLoggerOtelTransport(logger, packageJson.version, loggerContext);
},
);
if (process.env.NODE_ENV === "production") {
attachLoggerVercelTransport(logger, require("./logger-context").loggerContext);
}
}

export { createLogger, logger };
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class CalculateTaxesUseCase {
});
},
).map((results) => {
this.logger.info("Taxes calculated", { calculatedTaxes: JSON.stringify(results) });
this.logger.info("Taxes calculated", { calculatedTaxes: results });

ClientLogStoreRequest.create({
level: "info",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class CrudSettingsManager {

if (!validation.success) {
this.logger.error("Error while validating metadata", {
error: JSON.stringify(validation.error),
error: validation.error,
metadataKey: this.params.metadataKey,
});
throw new Error("Error while validating metadata");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const handler = checkoutCalculateTaxesSyncWebhook.createHandler(async (req, res,
subscriptionErrorChecker.checkPayload(payload);

logger.info("Tax base payload for checkout calculate taxes", {
payload: JSON.stringify(payload.taxBase),
payload: payload.taxBase,
});

loggerContext.set(ObservabilityAttributes.CHANNEL_SLUG, ctx.payload.taxBase.channel.slug);
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { logger, createLogger } from "./src/logger";
export { createLogger, logger } from "./src/logger";
export { attachLoggerConsoleTransport } from "./src/logger-console-transport";
1 change: 1 addition & 0 deletions packages/logger/node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { LoggerContext, wrapWithLoggerContext } from "./src/logger-context";
export * from "./src/logger-otel-transport";
export * from "./src/logger-sentry-transport";
export * from "./src/logger-vercel-transport";
3 changes: 2 additions & 1 deletion packages/logger/src/logger-context.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, it, expect } from "vitest";
import { describe, expect, it } from "vitest";

import { LoggerContext } from "./logger-context";

describe("LoggerContext", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/logger-sentry-transport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ILogObj, Logger } from "tslog";
import * as Sentry from "@sentry/nextjs";
import { SeverityLevel } from "@sentry/nextjs";
import { ILogObj, Logger } from "tslog";

const loggerLevelToSentryLevel = (level: string): SeverityLevel => {
switch (level) {
Expand Down
38 changes: 38 additions & 0 deletions packages/logger/src/logger-vercel-transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ILogObj, Logger } from "tslog";

import { LoggerContext } from "./logger-context";

export const attachLoggerVercelTransport = (
logger: Logger<ILogObj>,
loggerContext?: LoggerContext,
) => {
logger.attachTransport((log) => {
const { message, attributes, _meta } = log;

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

const stringifiedMessage = JSON.stringify({
message: bodyMessage,
...attributes,
...loggerContext?.getRawContext(),
_meta: {
..._meta,
// used to filter out log in log drain
source: "saleor-app",
},
});

// 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);
});
};
Loading