| 1 | import { Extras, type Integration, type Event } from "@sentry/core"; |
| 2 | import * as Sentry from "@sentry/node"; |
| 3 | import os from "node:os"; |
| 4 | import { IdeInfo } from "../../index.js"; |
| 5 | import { isContinueTeamMember } from "../isContinueTeamMember.js"; |
| 6 | import { anonymizeSentryEvent } from "./anonymization.js"; |
| 7 | import { SENTRY_DSN } from "./constants.js"; |
| 8 | |
| 9 | export class SentryLogger { |
| 10 | static client: Sentry.NodeClient | undefined = undefined; |
| 11 | static scope: Sentry.Scope | undefined = undefined; |
| 12 | static uniqueId = "NOT_UNIQUE"; |
| 13 | static os: string | undefined = undefined; |
| 14 | static ideInfo: IdeInfo | undefined = undefined; |
| 15 | static allowTelemetry: boolean = false; |
| 16 | |
| 17 | private static initializeSentryClient(release: string): { |
| 18 | client: Sentry.NodeClient | undefined; |
| 19 | scope: Sentry.Scope | undefined; |
| 20 | } { |
| 21 | try { |
| 22 | // For shared environments like VSCode extensions, we need to avoid global state pollution |
| 23 | // Filter out integrations that use global state |
| 24 | // See https://docs.sentry.io/platforms/javascript/best-practices/shared-environments/ |
| 25 | |
| 26 | // Filter integrations that use the global variable |
| 27 | const integrations = Sentry.getDefaultIntegrations({}).filter( |
| 28 | (defaultIntegration: Integration) => { |
| 29 | // Remove integrations that might interfere with shared environments |
| 30 | return ![ |
| 31 | "OnUncaughtException", |
| 32 | "OnUnhandledRejection", |
| 33 | "ContextLines", |
| 34 | "LocalVariables", |
| 35 | ].includes(defaultIntegration.name); |
| 36 | }, |
| 37 | ); |
| 38 | |
| 39 | // Create client manually without polluting global state |
| 40 | const client = new Sentry.NodeClient({ |
| 41 | dsn: SENTRY_DSN, |
| 42 | release, |
| 43 | environment: process.env.NODE_ENV, |
| 44 | transport: Sentry.makeNodeTransport, |
| 45 | stackParser: Sentry.defaultStackParser, |
| 46 | |
| 47 | // For basic error tracking, a lower sample rate should be fine |
| 48 | sampleRate: 0.1, |
| 49 | tracesSampleRate: 0.1, |
| 50 | |
| 51 | // Privacy-conscious default |
| 52 | sendDefaultPii: false, |
| 53 | |
| 54 | // Strip sensitive data and add basic properties before sending events |
| 55 | beforeSend(event: Event) { |
| 56 | // First apply anonymization |
| 57 | const anonymizedEvent = anonymizeSentryEvent(event); |
| 58 | if (!anonymizedEvent) return null; |
| 59 | |
| 60 | // Add basic properties similar to PostHog telemetry |
| 61 | if (!anonymizedEvent.tags) anonymizedEvent.tags = {}; |
| 62 | if (!anonymizedEvent.extra) anonymizedEvent.extra = {}; |
| 63 | |
| 64 | // Add OS information |
| 65 | if (SentryLogger.os) { |
| 66 | anonymizedEvent.tags.os = SentryLogger.os; |
| 67 | } |
| 68 | |
| 69 | // Add ideInfo properties spread out as top-level properties |
| 70 | if (SentryLogger.ideInfo) { |
| 71 | anonymizedEvent.tags.extensionVersion = |
| 72 | SentryLogger.ideInfo.extensionVersion; |
| 73 | anonymizedEvent.tags.ideName = SentryLogger.ideInfo.name; |
| 74 | anonymizedEvent.tags.ideType = SentryLogger.ideInfo.ideType; |
| 75 | anonymizedEvent.tags.ideVersion = SentryLogger.ideInfo.version; |
| 76 | anonymizedEvent.tags.remoteName = SentryLogger.ideInfo.remoteName; |
| 77 | anonymizedEvent.tags.isPrerelease = |
| 78 | SentryLogger.ideInfo.isPrerelease; |
| 79 | } |
| 80 | |
| 81 | return anonymizedEvent; |
| 82 | }, |
| 83 | |
| 84 | // Use filtered integrations for Node.js/VSCode shared environment |
| 85 | integrations, |
| 86 | |
| 87 | // Enable structured logging |
| 88 | _experiments: { |
| 89 | enableLogs: true, |
| 90 | }, |
| 91 | }); |
| 92 | |
| 93 | // Create a new scope and set the client |
| 94 | const scope = new Sentry.Scope(); |
| 95 | scope.setClient(client); |
| 96 | |
| 97 | // Initialize the client after setting it on the scope |
| 98 | client.init(); |
| 99 | |
| 100 | return { client, scope }; |
| 101 | } catch (error) { |
| 102 | console.error("Failed to initialize Sentry client:", error); |
| 103 | return { client: undefined, scope: undefined }; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static async setup( |
| 108 | allowAnonymousTelemetry: boolean, |
| 109 | uniqueId: string, |
| 110 | ideInfo: IdeInfo, |
| 111 | userEmail?: string, |
| 112 | ) { |
| 113 | // TODO: Remove Continue team member check once Sentry is ready for all users |
| 114 | SentryLogger.allowTelemetry = |
| 115 | allowAnonymousTelemetry && isContinueTeamMember(userEmail); |
| 116 | SentryLogger.uniqueId = uniqueId; |
| 117 | SentryLogger.ideInfo = ideInfo; |
| 118 | SentryLogger.os = os.platform(); |
| 119 | |
| 120 | if (!SentryLogger.allowTelemetry) { |
| 121 | SentryLogger.client = undefined; |
| 122 | SentryLogger.scope = undefined; |
| 123 | } else if (!SentryLogger.client) { |
| 124 | const { client, scope } = SentryLogger.initializeSentryClient( |
| 125 | ideInfo.extensionVersion, |
| 126 | ); |
| 127 | SentryLogger.client = client; |
| 128 | SentryLogger.scope = scope; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private static ensureInitialized(): void { |
| 133 | if (!SentryLogger.allowTelemetry || SentryLogger.client) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | if (SentryLogger.ideInfo) { |
| 138 | const { client, scope } = SentryLogger.initializeSentryClient( |
| 139 | SentryLogger.ideInfo.extensionVersion, |
| 140 | ); |
| 141 | SentryLogger.client = client; |
| 142 | SentryLogger.scope = scope; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static get lazyClient(): Sentry.NodeClient | undefined { |
| 147 | SentryLogger.ensureInitialized(); |
| 148 | return SentryLogger.client; |
| 149 | } |
| 150 | |
| 151 | static get lazyScope(): Sentry.Scope | undefined { |
| 152 | SentryLogger.ensureInitialized(); |
| 153 | return SentryLogger.scope; |
| 154 | } |
| 155 | |
| 156 | static shutdownSentryClient() { |
| 157 | if (SentryLogger.client) { |
| 158 | void SentryLogger.client.close(); |
| 159 | SentryLogger.client = undefined; |
| 160 | SentryLogger.scope = undefined; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Initialize Sentry for error tracking, performance monitoring, and structured logging. |
| 167 | * Returns the Sentry client and scope, or undefined objects if telemetry is disabled. |
| 168 | */ |
| 169 | export function initializeSentry(): { |
| 170 | client: Sentry.NodeClient | undefined; |
| 171 | scope: Sentry.Scope | undefined; |
| 172 | } { |
| 173 | return { |
| 174 | client: SentryLogger.lazyClient, |
| 175 | scope: SentryLogger.lazyScope, |
| 176 | }; |
| 177 | } |
| 178 | |
| 179 | // Export utility functions for using Sentry throughout the application |
| 180 | |
| 181 | /** |
| 182 | * Create a custom span for performance monitoring |
| 183 | * |
| 184 | * @param operation The operation category (e.g., "http.client", "ui.click", "db.query") |
| 185 | * @param name A descriptive name for the span |
| 186 | * @param callback The function to execute within the span |
| 187 | * @returns The result of the callback function |
| 188 | */ |
| 189 | export function createSpan<T>( |
| 190 | operation: string, |
| 191 | name: string, |
| 192 | callback: () => T | Promise<T>, |
| 193 | ): T | Promise<T> { |
| 194 | const client = SentryLogger.lazyClient; |
| 195 | if (!client) { |
| 196 | return callback(); |
| 197 | } |
| 198 | |
| 199 | // Use withScope from Sentry to isolate the span context |
| 200 | return Sentry.withScope((isolatedScope: Sentry.Scope) => { |
| 201 | isolatedScope.setClient(client); |
| 202 | return Sentry.startSpan( |
| 203 | { |
| 204 | op: operation, |
| 205 | name, |
| 206 | }, |
| 207 | () => callback(), |
| 208 | ); |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Capture an exception and send it to Sentry |
| 214 | * |
| 215 | * @param error The error to capture |
| 216 | * @param context Additional context information |
| 217 | */ |
| 218 | export function captureException(error: Error, context?: Record<string, any>) { |
| 219 | const scope = SentryLogger.lazyScope; |
| 220 | if (!scope) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | try { |
| 225 | // Add context to scope if provided |
| 226 | if (context) { |
| 227 | scope.setExtras(context); |
| 228 | } |
| 229 | // Use scope's captureException to avoid global state |
| 230 | scope.captureException(error); |
| 231 | } catch (e) { |
| 232 | console.error(`Failed to capture exception to Sentry: ${e}`); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Capture a structured log message and send it to Sentry |
| 238 | * |
| 239 | * @param message The log message |
| 240 | * @param level The severity level (default: 'info') |
| 241 | * @param context Additional context information |
| 242 | */ |
| 243 | export function captureLog( |
| 244 | message: string, |
| 245 | level: Sentry.SeverityLevel = "info", |
| 246 | context?: Extras, |
| 247 | ) { |
| 248 | const scope = SentryLogger.lazyScope; |
| 249 | if (!scope) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | try { |
| 254 | // Add context to scope if provided |
| 255 | if (context) { |
| 256 | scope.setExtras(context); |
| 257 | } |
| 258 | // Use scope's captureMessage to avoid global state |
| 259 | scope.captureMessage(message, level); |
| 260 | } catch (e) { |
| 261 | console.error(`Failed to capture log to Sentry: ${e}`); |
| 262 | } |
| 263 | } |