Skip to content

Commit 207cf05

Browse files
committed
extract formatTags and its dependencies to a separate module
1 parent a99be25 commit 207cf05

3 files changed

Lines changed: 45 additions & 42 deletions

File tree

packages/shared/src/formatTags.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import StackUtils from "stack-utils";
2+
3+
const stackUtils = new StackUtils({ cwd: process.cwd(), internals: StackUtils.nodeInternals() });
4+
5+
function anonymizeStackTrace(stack: string): string {
6+
return stack
7+
.split("\n")
8+
.map(line => {
9+
const frame = stackUtils.parseLine(line);
10+
if (frame && frame.file) {
11+
const relativePath = frame.file.includes("node_modules")
12+
? frame.file.substring(frame.file.indexOf("node_modules"))
13+
: frame.file;
14+
return line.replace(frame.file, relativePath);
15+
}
16+
return line;
17+
})
18+
.join("\n");
19+
}
20+
21+
export type Tags = Record<string, unknown>;
22+
23+
export function formatTags(tags?: Tags) {
24+
if (!tags) {
25+
return;
26+
}
27+
28+
return Object.entries(tags).reduce((result, [key, value]) => {
29+
if (value instanceof Error) {
30+
result[key] = {
31+
// Intentionally keeping this for any extra properties attached in `Error`
32+
...(value as any),
33+
errorName: value.name,
34+
errorMessage: value.message,
35+
errorStack: anonymizeStackTrace(value.stack ?? ""),
36+
};
37+
} else {
38+
result[key] = value;
39+
}
40+
return result;
41+
}, {} as Record<string, unknown>);
42+
}

packages/shared/src/logger.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,15 @@ import LokiTransport from "winston-loki";
44
import { AuthInfo } from "./graphql/fetchAuthInfoFromGraphQL";
55
import { getDeviceId } from "./getDeviceId";
66
import { randomUUID } from "crypto";
7-
import StackUtils from "stack-utils";
7+
import { formatTags, Tags } from "./formatTags";
88

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

15-
const stackUtils = new StackUtils({ cwd: process.cwd(), internals: StackUtils.nodeInternals() });
16-
17-
function anonymizeStackTrace(stack: string): string {
18-
return stack
19-
.split("\n")
20-
.map(line => {
21-
const frame = stackUtils.parseLine(line);
22-
if (frame && frame.file) {
23-
const relativePath = frame.file.includes("node_modules")
24-
? frame.file.substring(frame.file.indexOf("node_modules"))
25-
: frame.file;
26-
return line.replace(frame.file, relativePath);
27-
}
28-
return line;
29-
})
30-
.join("\n");
31-
}
32-
33-
export function formatTags(tags?: Tags) {
34-
if (!tags) {
35-
return;
36-
}
37-
38-
return Object.entries(tags).reduce((result, [key, value]) => {
39-
if (value instanceof Error) {
40-
result[key] = {
41-
// Intentionally keeping this for any extra properties attached in `Error`
42-
...(value as any),
43-
errorName: value.name,
44-
errorMessage: value.message,
45-
errorStack: anonymizeStackTrace(value.stack ?? ""),
46-
};
47-
} else {
48-
result[key] = value;
49-
}
50-
return result;
51-
}, {} as Record<string, unknown>);
52-
}
53-
5415
type LogLevel = "error" | "warn" | "info" | "debug";
55-
export type Tags = Record<string, unknown>;
5616

5717
class Logger {
5818
private deviceId: string;

packages/shared/src/sentry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const { nodeProfilingIntegration } = require("@sentry/profiling-node");
33
import { AuthInfo } from "./graphql/fetchAuthInfoFromGraphQL";
44
import { getDeviceId } from "./getDeviceId";
55
import { randomUUID } from "crypto";
6-
import { formatTags, logger, Tags } from "./logger";
6+
import { logger } from "./logger";
7+
import { formatTags, Tags } from "./formatTags";
78

89
const SENTRY_DSN =
910
"https://5c145b72bb502832982243d6584f163d@o437061.ingest.us.sentry.io/4507534763819008"; // write-only permissions

0 commit comments

Comments
 (0)