Skip to content

Commit 4f96d74

Browse files
committed
create sentry util for tracking traces, issues, and metrics
1 parent d826532 commit 4f96d74

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import {
2+
defaultStackParser,
3+
makeNodeTransport,
4+
NodeClient,
5+
Hub,
6+
metrics,
7+
} from "@sentry/node";
8+
import { type Options } from "./types";
9+
import { type NormalizedOptions } from "./utils/normalizeOptions";
10+
11+
export const createSentryInstance = (
12+
options: NormalizedOptions,
13+
bundler: string,
14+
) => {
15+
const telemetry = options.telemetry ?? true;
16+
17+
if (telemetry === false || !!options.dryRun) {
18+
return { sentryClient: undefined };
19+
}
20+
21+
const client = new NodeClient({
22+
dsn: "https://942e283ea612c29cc3371c6d27f57e58@o26192.ingest.sentry.io/4506739665207296",
23+
24+
tracesSampleRate: 1,
25+
sampleRate: 1,
26+
27+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
28+
// @ts-expect-error this value is being replaced by rollup
29+
release: __PACKAGE_VERSION__ as string,
30+
integrations: [metrics.metricsAggregatorIntegration()],
31+
tracePropagationTargets: ["api.codecov.io", "stage-api.codecov.dev"],
32+
33+
stackParser: defaultStackParser,
34+
35+
beforeSend: (event) => {
36+
event.exception?.values?.forEach((exception) => {
37+
delete exception.stacktrace;
38+
});
39+
40+
delete event.server_name;
41+
return event;
42+
},
43+
44+
beforeSendTransaction: (event) => {
45+
delete event.server_name;
46+
return event;
47+
},
48+
49+
transport: (nodeTransportOptions) => {
50+
const nodeTransport = makeNodeTransport(nodeTransportOptions);
51+
return {
52+
flush: (timeout) => nodeTransport.flush(timeout),
53+
send: async (request) => {
54+
if (telemetry) {
55+
return nodeTransport.send(request);
56+
}
57+
return undefined;
58+
},
59+
};
60+
},
61+
});
62+
63+
const hub = new Hub(client);
64+
65+
setTelemetryDataOnHub(options, hub, bundler);
66+
67+
// increment the counter for the bundler
68+
client.metricsAggregator?.add("c", `bundler-${bundler}`, 1);
69+
70+
return { sentryClient: client };
71+
};
72+
73+
export const setTelemetryDataOnHub = (
74+
options: Options,
75+
hub: Hub,
76+
bundler: string,
77+
) => {
78+
const telemetry = options.telemetry ?? true;
79+
80+
if (telemetry === false) {
81+
return false;
82+
}
83+
84+
if (
85+
options.apiUrl !== "https://api.codecov.io" ||
86+
// @ts-expect-error need to ensure that values belong to Codecov
87+
options.apiUrl !== "https://stage-api.codecov.dev"
88+
) {
89+
return false;
90+
}
91+
92+
hub.setTag("bundle-analysis", !!options.enableBundleAnalysis);
93+
hub.setTag("node", process.version);
94+
hub.setTag("platform", process.platform);
95+
hub.setTag("bundler", bundler);
96+
97+
return;
98+
};
99+
100+
export const safeFlushTelemetry = async (sentryClient: NodeClient) => {
101+
try {
102+
await sentryClient.flush(2000);
103+
} catch {
104+
// Noop when flushing fails.
105+
// We don't even need to log anything because there's likely nothing the user can do and they likely will not care.
106+
}
107+
};

0 commit comments

Comments
 (0)