-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodejs_example.js
More file actions
602 lines (510 loc) · 15.4 KB
/
nodejs_example.js
File metadata and controls
602 lines (510 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/**
* Bugsink/Sentry SDK Integration Example for Node.js
* ===================================================
*
* This example demonstrates comprehensive error tracking integration
* using the Sentry SDK with a self-hosted Bugsink server.
*
* Requirements:
* npm install @sentry/node @sentry/profiling-node express
*
* DSN Format:
* https://<project-key>@<your-bugsink-host>/<project-id>
*/
const Sentry = require("@sentry/node");
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
// =============================================================================
// CONFIGURATION
// =============================================================================
const SENTRY_DSN =
process.env.SENTRY_DSN ||
"https://your-project-key@errors.observability.app.bauer-group.com/1";
const ENVIRONMENT = process.env.NODE_ENV || "development";
const RELEASE = process.env.APP_VERSION || "1.0.0";
const SERVER_NAME = process.env.HOSTNAME || require("os").hostname();
// =============================================================================
// SENTRY INITIALIZATION
// =============================================================================
/**
* Initialize Sentry SDK with comprehensive configuration.
* Call this once at application startup, BEFORE any other code.
*/
function initSentry() {
Sentry.init({
dsn: SENTRY_DSN,
// Environment & Release
environment: ENVIRONMENT,
release: `my-app@${RELEASE}`,
serverName: SERVER_NAME,
// Integrations
integrations: [
// Performance Profiling
nodeProfilingIntegration(),
],
// Performance Monitoring
tracesSampleRate: ENVIRONMENT === "production" ? 0.1 : 1.0,
profilesSampleRate: 0.1,
// Error Sampling
sampleRate: 1.0,
// Data Handling
sendDefaultPii: false,
maxBreadcrumbs: 50,
attachStacktrace: true,
// Request Data
maxValueLength: 1000,
// Before Send Hook - sanitize/filter events
beforeSend: beforeSendHandler,
// Before Breadcrumb Hook
beforeBreadcrumb: beforeBreadcrumbHandler,
// Debug mode
debug: ENVIRONMENT === "development",
});
// Set global tags
Sentry.setTag("app.component", "backend");
Sentry.setTag("app.runtime", "nodejs");
Sentry.setTag("app.version", process.version);
console.log(`Sentry initialized for environment: ${ENVIRONMENT}`);
}
/**
* Process events before sending to Sentry.
* Use this to sanitize sensitive data or filter events.
*/
function beforeSendHandler(event, hint) {
// Remove sensitive headers
if (event.request && event.request.headers) {
const sensitiveHeaders = ["authorization", "cookie", "x-api-key"];
sensitiveHeaders.forEach((header) => {
if (event.request.headers[header]) {
event.request.headers[header] = "[REDACTED]";
}
});
}
// Filter out specific exceptions
const exception = hint.originalException;
if (exception && exception.name === "ExpectedBusinessError") {
return null; // Don't send this event
}
// Add custom fingerprint for specific errors
if (exception && exception.code === "ECONNREFUSED") {
event.fingerprint = ["database-connection-error"];
}
return event;
}
/**
* Process breadcrumbs before adding to the event.
*/
function beforeBreadcrumbHandler(breadcrumb, hint) {
// Filter out health check requests
if (
breadcrumb.category === "http" &&
breadcrumb.data &&
breadcrumb.data.url &&
breadcrumb.data.url.includes("/health")
) {
return null;
}
// Sanitize sensitive data in breadcrumbs
if (breadcrumb.data && breadcrumb.data.body) {
const body = breadcrumb.data.body;
if (typeof body === "string" && body.includes("password")) {
breadcrumb.data.body = "[REDACTED]";
}
}
return breadcrumb;
}
// =============================================================================
// CONTEXT MANAGEMENT
// =============================================================================
/**
* Set user context for error tracking.
* Call this after user authentication.
*
* @param {Object} user - User information
* @param {string} user.id - User ID (required)
* @param {string} [user.email] - User email
* @param {string} [user.username] - Username
* @param {string} [user.ipAddress] - IP address
*/
function setUserContext(user) {
Sentry.setUser({
id: user.id,
email: user.email,
username: user.username,
ip_address: user.ipAddress,
...user.extra,
});
}
/**
* Clear user context (e.g., on logout).
*/
function clearUserContext() {
Sentry.setUser(null);
}
/**
* Add a breadcrumb to track user actions/events.
*
* @param {string} message - Breadcrumb message
* @param {string} [category='custom'] - Category for grouping
* @param {string} [level='info'] - Severity level
* @param {Object} [data={}] - Additional data
*/
function addBreadcrumb(message, category = "custom", level = "info", data = {}) {
Sentry.addBreadcrumb({
message,
category,
level,
data,
timestamp: Date.now() / 1000,
});
}
/**
* Set custom context for the current scope.
*
* @param {string} name - Context name
* @param {Object} context - Context data
*/
function setContext(name, context) {
Sentry.setContext(name, context);
}
// =============================================================================
// ERROR CAPTURING
// =============================================================================
/**
* Capture an exception with optional extra context.
*
* @param {Error} error - The error to capture
* @param {Object} [extraContext={}] - Additional context
* @returns {string} Event ID
*/
function captureException(error, extraContext = {}) {
return Sentry.withScope((scope) => {
Object.entries(extraContext).forEach(([key, value]) => {
scope.setExtra(key, value);
});
return Sentry.captureException(error);
});
}
/**
* Capture a message (non-exception event).
*
* @param {string} message - The message to capture
* @param {string} [level='info'] - Severity level
* @param {Object} [extraContext={}] - Additional context
* @returns {string} Event ID
*/
function captureMessage(message, level = "info", extraContext = {}) {
return Sentry.withScope((scope) => {
Object.entries(extraContext).forEach(([key, value]) => {
scope.setExtra(key, value);
});
return Sentry.captureMessage(message, level);
});
}
// =============================================================================
// DECORATORS / WRAPPERS
// =============================================================================
/**
* Wrap a function to automatically track errors.
*
* @param {string} operationName - Name of the operation
* @param {Function} fn - Function to wrap
* @returns {Function} Wrapped function
*/
function trackErrors(operationName, fn) {
return function (...args) {
return Sentry.withScope((scope) => {
scope.setTag("operation", operationName);
scope.setExtra("function", fn.name || "anonymous");
addBreadcrumb(`Executing ${operationName}`, "function", "info");
try {
const result = fn.apply(this, args);
// Handle promises
if (result && typeof result.then === "function") {
return result.catch((error) => {
scope.setExtra("error_type", error.name);
throw error;
});
}
return result;
} catch (error) {
scope.setExtra("error_type", error.name);
throw error;
}
});
};
}
/**
* Wrap an async function with a transaction for performance monitoring.
*
* @param {string} name - Transaction name
* @param {string} [op='function'] - Operation type
* @param {Function} fn - Async function to wrap
* @returns {Function} Wrapped function
*/
function withTransaction(name, op = "function", fn) {
return async function (...args) {
return Sentry.startSpan(
{
name,
op,
},
async (span) => {
try {
const result = await fn.apply(this, args);
span.setStatus({ code: 1 }); // OK
return result;
} catch (error) {
span.setStatus({ code: 2, message: error.message }); // ERROR
throw error;
}
}
);
};
}
// =============================================================================
// EXPRESS INTEGRATION EXAMPLE
// =============================================================================
/**
* Create an Express application with Sentry integration.
*/
function createExpressApp() {
let express;
try {
express = require("express");
} catch (e) {
console.log("Express not installed. Run: npm install express");
return null;
}
const app = express();
// Sentry request handler - must be first middleware
app.use(Sentry.Handlers.requestHandler());
// Sentry tracing handler
app.use(Sentry.Handlers.tracingHandler());
// Parse JSON bodies
app.use(express.json());
// Request context middleware
app.use((req, res, next) => {
const requestId = req.headers["x-request-id"] || generateRequestId();
req.requestId = requestId;
Sentry.setTag("request_id", requestId);
// Set user context if authenticated
const userId = req.headers["x-user-id"];
if (userId) {
setUserContext({
id: userId,
ipAddress: req.ip,
});
}
next();
});
// Routes
app.get("/", (req, res) => {
addBreadcrumb("User visited homepage", "navigation");
res.json({ status: "ok", message: "Welcome to the API" });
});
app.get("/api/users/:userId", (req, res) => {
const { userId } = req.params;
addBreadcrumb(`Fetching user ${userId}`, "api", "info", { userId });
if (userId === "0") {
throw new Error("Invalid user ID");
}
res.json({ userId, name: "Test User" });
});
app.get("/api/error", (req, res) => {
// This will trigger an error
throw new Error("Test error from /api/error endpoint");
});
app.get("/api/async-error", async (req, res, next) => {
try {
await asyncOperationThatFails();
res.json({ status: "ok" });
} catch (error) {
next(error);
}
});
app.get("/api/message", (req, res) => {
captureMessage("User triggered test message", "info", {
endpoint: "/api/message",
customData: { test: true },
});
res.json({ status: "message sent" });
});
app.post("/api/process", async (req, res, next) => {
try {
const result = await Sentry.startSpan(
{ name: "process_data", op: "task" },
async (span) => {
// Simulate sub-operations with child spans
await Sentry.startSpan(
{ name: "validate_input", op: "validation" },
async () => {
await sleep(50);
}
);
await Sentry.startSpan(
{ name: "save_to_database", op: "db.query" },
async () => {
await sleep(100);
}
);
return { processed: true };
}
);
res.json(result);
} catch (error) {
next(error);
}
});
// Sentry error handler - must be before other error handlers
app.use(Sentry.Handlers.errorHandler());
// Custom error handler
app.use((err, req, res, next) => {
console.error("Error:", err.message);
res.status(500).json({
error: err.message,
requestId: req.requestId,
});
});
return app;
}
// =============================================================================
// HELPER FUNCTIONS
// =============================================================================
function generateRequestId() {
return `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function asyncOperationThatFails() {
await sleep(100);
throw new Error("Async operation failed");
}
// =============================================================================
// MAIN EXAMPLE
// =============================================================================
async function main() {
console.log("=".repeat(60));
console.log("Bugsink/Sentry Node.js SDK Integration Example");
console.log("=".repeat(60));
// Initialize Sentry
initSentry();
// Set user context
setUserContext({
id: "user-123",
email: "developer@example.com",
username: "developer",
extra: {
subscriptionTier: "premium",
},
});
// Add breadcrumbs
addBreadcrumb("Application started", "app", "info");
addBreadcrumb("User authenticated", "auth", "info");
addBreadcrumb("Loading dashboard", "navigation", "info");
// Example 1: Capture a handled exception
console.log("\n1. Capturing handled exception...");
try {
JSON.parse("invalid json");
} catch (error) {
const eventId = captureException(error, {
operation: "json_parsing",
input: "invalid json",
});
console.log(` Exception captured with event ID: ${eventId}`);
}
// Example 2: Capture a message
console.log("\n2. Capturing info message...");
const messageId = captureMessage("User completed onboarding flow", "info", {
stepsCompleted: 5,
timeTakenSeconds: 120,
});
console.log(` Message captured with event ID: ${messageId}`);
// Example 3: Use wrapper for automatic tracking
console.log("\n3. Using trackErrors wrapper...");
const processData = trackErrors("data_processing", (data) => {
if (!data || data.length === 0) {
throw new Error("Data cannot be empty");
}
return data.length;
});
try {
processData([]);
} catch (error) {
console.log(" Error tracked automatically via wrapper");
}
// Example 4: Transaction for performance monitoring
console.log("\n4. Creating performance transaction...");
const batchOperation = withTransaction(
"batch_operation",
"task",
async () => {
await sleep(100);
return "completed";
}
);
await batchOperation();
console.log(" Transaction recorded");
// Example 5: Scoped context
console.log("\n5. Using scoped context...");
Sentry.withScope((scope) => {
scope.setTag("feature", "new_checkout");
scope.setExtra("cartItems", 3);
scope.setExtra("totalAmount", 99.99);
captureMessage("Checkout initiated", "info");
});
console.log(" Scoped message captured");
// Example 6: Transaction with child spans
console.log("\n6. Creating transaction with spans...");
await Sentry.startSpan(
{ name: "order_processing", op: "task" },
async (span) => {
await Sentry.startSpan(
{ name: "fetch_order", op: "db.query" },
async () => {
await sleep(50);
}
);
await Sentry.startSpan(
{ name: "payment_api", op: "http.client" },
async () => {
await sleep(100);
}
);
await Sentry.startSpan(
{ name: "update_order_status", op: "db.query" },
async () => {
await sleep(50);
}
);
}
);
console.log(" Transaction with spans recorded");
// Clean up
clearUserContext();
console.log("\n" + "=".repeat(60));
console.log("All examples completed!");
console.log(
`Check your Bugsink dashboard at: https://${SENTRY_DSN.split("@")[1].split("/")[0]}`
);
console.log("=".repeat(60));
// Flush events before exit
await Sentry.flush(5000);
}
// Run if executed directly
if (require.main === module) {
main().catch(console.error);
}
// Export for use as module
module.exports = {
initSentry,
setUserContext,
clearUserContext,
addBreadcrumb,
setContext,
captureException,
captureMessage,
trackErrors,
withTransaction,
createExpressApp,
};