1// Alchemy modifications are licensed under Apache-2.0.
2// This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
3import { rewriteFramesIntegration, Toucan } from "toucan-js";
4import type { ColoMetadata } from "./types.ts";
5
6export function setupSentry(
7 request: Request,
8 context: ExecutionContext | undefined,
9 dsn: string,
10 clientId: string,
11 clientSecret: string,
12 coloMetadata?: ColoMetadata,
13 versionMetadata?: WorkerVersionMetadata,
14 accountId?: number,
15 scriptId?: number,
16): Toucan | undefined {
17 // Are we running locally without access to Sentry secrets? If so, don't initialise Sentry
18 if (!(dsn && clientId && clientSecret)) {
19 return undefined;
20 }
21 const sentry = new Toucan({
22 dsn,
23 request,
24 context,
25 sampleRate: 1.0,
26 release: versionMetadata?.tag,
27 integrations: [
28 rewriteFramesIntegration({
29 iteratee(frame) {
30 frame.filename = "/index.js";
31 return frame;
32 },
33 }),
34 ],
35 requestDataOptions: {
36 allowedHeaders: [
37 "user-agent",
38 "cf-challenge",
39 "accept-encoding",
40 "accept-language",
41 "cf-ray",
42 "content-length",
43 "content-type",
44 "host",
45 ],
46 allowedSearchParams: /(.*)/,
47 },
48
49 transportOptions: {
50 headers: {
51 "CF-Access-Client-ID": clientId,
52 "CF-Access-Client-Secret": clientSecret,
53 },
54 },
55 });
56
57 if (coloMetadata) {
58 sentry.setTag("colo", coloMetadata.coloId);
59 sentry.setTag("metal", coloMetadata.metalId);
60 }
61
62 if (accountId && scriptId) {
63 sentry.setTag("accountId", accountId);
64 sentry.setTag("scriptId", scriptId);
65 }
66
67 sentry.setUser({ id: accountId?.toString() });
68
69 return sentry;
70}