Skip to content

Commit

Permalink
migrate log points to the new logger in `packages/test-utils/src/lega…
Browse files Browse the repository at this point in the history
…cy-cli/main.ts` (#583)

* migrate log points to the new logger

* add getErrorTags util function

* remove unused function

* use getErrorTags to keep stacktrace

* format errors from the Logger class

* assign error tags to object key

* pr feedback

* truncate stack traces for maximum anonymity
  • Loading branch information
miriambudayr authored Jul 2, 2024
1 parent 4f1dd73 commit 951bc7d
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 118 deletions.
2 changes: 2 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"mixpanel": "^0.18.0",
"open": "^8.4.2",
"pretty-ms": "^7.0.1",
"stack-utils": "^2.0.6",
"strip-ansi": "^6.0.1",
"superstruct": "^1.0.4",
"table": "^6.8.2",
Expand All @@ -33,6 +34,7 @@
"devDependencies": {
"@replay-cli/pkg-build": "workspace:^",
"@types/jest": "^28.1.5",
"@types/stack-utils": "^2.0.3",
"jest": "^28.1.3",
"typescript": "^5.5.2"
},
Expand Down
48 changes: 46 additions & 2 deletions packages/shared/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,33 @@ import { AuthInfo } from "./graphql/fetchAuthInfoFromGraphQL";
import { getDeviceId } from "./getDeviceId";
import { randomUUID } from "crypto";

// Does not work with import. Only works with require().
const StackUtils = require("stack-utils");

const GRAFANA_USER = "909360";
const GRAFANA_PUBLIC_TOKEN =
"glc_eyJvIjoiOTEyOTQzIiwibiI6IndyaXRlLW90ZWwtcmVwbGF5LWNsaSIsImsiOiJ0UnFsOXV1a2QyQUI2NzIybDEzSkRuNDkiLCJtIjp7InIiOiJwcm9kLXVzLWVhc3QtMCJ9fQ=="; // write-only permissions.
const GRAFANA_BASIC_AUTH = `${GRAFANA_USER}:${GRAFANA_PUBLIC_TOKEN}`;
const HOST = "https://logs-prod-006.grafana.net";

const stackUtils = new StackUtils({ cwd: process.cwd(), internals: StackUtils.nodeInternals() });

function anonymizeStackTrace(stack: string): string {
return stack
.split("\n")
.map(line => {
const frame = stackUtils.parseLine(line);
if (frame && frame.file) {
const relativePath = frame.file.includes("node_modules")
? frame.file.substring(frame.file.indexOf("node_modules"))
: frame.file;
return line.replace(frame.file, relativePath);
}
return line;
})
.join("\n");
}

type LogLevel = "error" | "warn" | "info" | "debug";

type Tags = Record<string, unknown>;
Expand Down Expand Up @@ -74,7 +95,9 @@ class Logger {
}

private log(message: string, level: LogLevel, tags?: Tags) {
this.localDebugger(message, JSON.stringify(tags));
const formattedTags = this.formatTags(tags);

this.localDebugger(message, formattedTags);

if (process.env.REPLAY_TELEMETRY_DISABLED) {
return;
Expand All @@ -83,7 +106,7 @@ class Logger {
const entry: LogEntry = {
level,
message,
tags,
...formattedTags,
deviceId: this.deviceId,
sessionId: this.sessionId,
};
Expand All @@ -106,6 +129,27 @@ class Logger {
}
}

private formatTags(tags?: Record<string, unknown>) {
if (!tags) {
return;
}

return Object.entries(tags).reduce((result, [key, value]) => {
if (value instanceof Error) {
result[key] = {
// Intentionally keeping this for any extra properties attached in `Error`
...(value as any),
errorName: value.name,
errorMessage: value.message,
errorStack: anonymizeStackTrace(value.stack ?? ""),
};
} else {
result[key] = value;
}
return result;
}, {} as Record<string, unknown>);
}

async close() {
if (process.env.REPLAY_TELEMETRY_DISABLED) {
return;
Expand Down
3 changes: 3 additions & 0 deletions packages/test-utils/src/legacy-cli/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getErrorMessage(e: unknown) {
return e && typeof e === "object" && "message" in e ? (e.message as string) : "Unknown Error";
}
Loading

0 comments on commit 951bc7d

Please sign in to comment.