| 1 | import * as Sentry from "@sentry/node"; |
| 2 | |
| 3 | import { |
| 4 | getProfilingIntegration, |
| 5 | isProfilingAvailable, |
| 6 | } from "./sentry-profiling.js"; |
| 7 | import { logger } from "./util/logger.js"; |
| 8 | import { getVersion } from "./version.js"; |
| 9 | |
| 10 | interface SentryConfig { |
| 11 | enabled: boolean; |
| 12 | dsn?: string; |
| 13 | environment: string; |
| 14 | release: string; |
| 15 | sampleRate: number; |
| 16 | profilesSampleRate: number; |
| 17 | tracesSampleRate: number; |
| 18 | } |
| 19 | |
| 20 | class SentryService { |
| 21 | private config: SentryConfig; |
| 22 | private initialized = false; |
| 23 | |
| 24 | constructor() { |
| 25 | this.config = this.loadConfig(); |
| 26 | if (this.config.enabled) { |
| 27 | this.initialize(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | private loadConfig(): SentryConfig { |
| 32 | const enabled = |
| 33 | process.env.SENTRY_ENABLED !== "false" && !!process.env.SENTRY_DSN; |
| 34 | |
| 35 | return { |
| 36 | enabled, |
| 37 | dsn: process.env.SENTRY_DSN, |
| 38 | environment: |
| 39 | process.env.SENTRY_ENVIRONMENT || process.env.NODE_ENV || "development", |
| 40 | release: getVersion(), |
| 41 | sampleRate: parseFloat(process.env.SENTRY_SAMPLE_RATE || "1.0"), |
| 42 | profilesSampleRate: parseFloat( |
| 43 | process.env.SENTRY_PROFILES_SAMPLE_RATE || "1.0", |
| 44 | ), |
| 45 | tracesSampleRate: parseFloat( |
| 46 | process.env.SENTRY_TRACES_SAMPLE_RATE || "1.0", |
| 47 | ), |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | private async initialize() { |
| 52 | if (this.initialized || !this.config.dsn) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | try { |
| 57 | const integrations = []; |
| 58 | const profilingIntegration = await getProfilingIntegration(); |
| 59 | if ( |
| 60 | profilingIntegration && |
| 61 | Object.keys(profilingIntegration).length > 0 |
| 62 | ) { |
| 63 | integrations.push(profilingIntegration); |
| 64 | } |
| 65 | |
| 66 | Sentry.init({ |
| 67 | dsn: this.config.dsn, |
| 68 | environment: this.config.environment, |
| 69 | release: this.config.release, |
| 70 | sampleRate: this.config.sampleRate, |
| 71 | profilesSampleRate: this.config.profilesSampleRate, |
| 72 | tracesSampleRate: this.config.tracesSampleRate, |
| 73 | integrations, |
| 74 | beforeSend(event) { |
| 75 | // Filter out certain error types if needed |
| 76 | if (event.exception?.values?.[0]?.type === "AbortError") { |
| 77 | return null; |
| 78 | } |
| 79 | return event; |
| 80 | }, |
| 81 | }); |
| 82 | |
| 83 | this.initialized = true; |
| 84 | logger.debug("Sentry initialized", { |
| 85 | environment: this.config.environment, |
| 86 | release: this.config.release, |
| 87 | profilingAvailable: await isProfilingAvailable(), |
| 88 | }); |
| 89 | } catch (error) { |
| 90 | logger.error("Failed to initialize Sentry", error); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public isEnabled(): boolean { |
| 95 | return this.config.enabled && this.initialized; |
| 96 | } |
| 97 | |
| 98 | public captureException(error: Error, context?: Record<string, any>) { |
| 99 | if (!this.isEnabled()) return; |
| 100 | |
| 101 | Sentry.withScope((scope) => { |
| 102 | if (context) { |
| 103 | Object.entries(context).forEach(([key, value]) => { |
| 104 | scope.setContext(key, value); |
| 105 | }); |
| 106 | } |
| 107 | Sentry.captureException(error); |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | public captureMessage( |
| 112 | message: string, |
| 113 | level: "info" | "warning" | "error" | "debug" | "fatal" = "info", |
| 114 | context?: Record<string, any>, |
| 115 | ) { |
| 116 | if (!this.isEnabled()) return; |
| 117 | |
| 118 | Sentry.withScope((scope) => { |
| 119 | if (context) { |
| 120 | Object.entries(context).forEach(([key, value]) => { |
| 121 | scope.setContext(key, value); |
| 122 | }); |
| 123 | } |
| 124 | Sentry.captureMessage(message, level); |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | public setUser(user: { id?: string; email?: string; username?: string }) { |
| 129 | if (!this.isEnabled()) return; |
| 130 | |
| 131 | Sentry.setUser(user); |
| 132 | } |
| 133 | |
| 134 | public setTag(key: string, value: string) { |
| 135 | if (!this.isEnabled()) return; |
| 136 | |
| 137 | Sentry.setTag(key, value); |
| 138 | } |
| 139 | |
| 140 | public setContext(key: string, context: Record<string, any>) { |
| 141 | if (!this.isEnabled()) return; |
| 142 | |
| 143 | Sentry.setContext(key, context); |
| 144 | } |
| 145 | |
| 146 | public addBreadcrumb(breadcrumb: { |
| 147 | message: string; |
| 148 | category?: string; |
| 149 | level?: "fatal" | "error" | "warning" | "log" | "info" | "debug"; |
| 150 | data?: Record<string, any>; |
| 151 | }) { |
| 152 | if (!this.isEnabled()) return; |
| 153 | |
| 154 | Sentry.addBreadcrumb(breadcrumb); |
| 155 | } |
| 156 | |
| 157 | public startSpan(name: string, op?: string) { |
| 158 | if (!this.isEnabled()) return null; |
| 159 | |
| 160 | return Sentry.startSpan({ name, op }, (span) => span); |
| 161 | } |
| 162 | |
| 163 | public async flush(timeout = 2000): Promise<boolean> { |
| 164 | if (!this.isEnabled()) return true; |
| 165 | |
| 166 | return await Sentry.flush(timeout); |
| 167 | } |
| 168 | |
| 169 | public async close(timeout = 2000): Promise<boolean> { |
| 170 | if (!this.isEnabled()) return true; |
| 171 | |
| 172 | return await Sentry.close(timeout); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | export const sentryService = new SentryService(); |