Elffuss BraidHub

kikocis/continue public

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