-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathapp.ts
More file actions
83 lines (67 loc) · 2.41 KB
/
app.ts
File metadata and controls
83 lines (67 loc) · 2.41 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
import * as Sentry from '@sentry/node-core/light';
import express from 'express';
// IMPORTANT: Initialize Sentry BEFORE creating the Express app
// This is required for automatic request isolation to work
Sentry.init({
dsn: process.env.E2E_TEST_DSN,
debug: true,
tracesSampleRate: 1.0,
tunnel: 'http://localhost:3031/', // Use event proxy for testing
});
// Create Express app AFTER Sentry.init()
const app = express();
const port = 3030;
app.get('/test-error', (_req, res) => {
Sentry.setTag('test', 'error');
Sentry.captureException(new Error('Test error from light mode'));
res.status(500).json({ error: 'Error captured' });
});
app.get('/test-isolation/:userId', async (req, res) => {
const userId = req.params.userId;
const isolationScope = Sentry.getIsolationScope();
const currentScope = Sentry.getCurrentScope();
Sentry.setUser({ id: userId });
Sentry.setTag('user_id', userId);
currentScope.setTag('processing_user', userId);
currentScope.setContext('api_context', {
userId,
timestamp: Date.now(),
});
// Simulate async work with variance so we run into cases where
// the next request comes in before the async work is complete
// to showcase proper request isolation
await new Promise(resolve => setTimeout(resolve, Math.random() * 500 + 100));
// Verify isolation after async operations
const finalIsolationData = isolationScope.getScopeData();
const finalCurrentData = currentScope.getScopeData();
const isIsolated =
finalIsolationData.user?.id === userId &&
finalIsolationData.tags?.user_id === userId &&
finalCurrentData.contexts?.api_context?.userId === userId;
res.json({
userId,
isIsolated,
scope: {
userId: finalIsolationData.user?.id,
userIdTag: finalIsolationData.tags?.user_id,
currentUserId: finalCurrentData.contexts?.api_context?.userId,
},
});
});
app.get('/test-isolation-error/:userId', (req, res) => {
const userId = req.params.userId;
Sentry.setTag('user_id', userId);
Sentry.setUser({ id: userId });
Sentry.captureException(new Error(`Error for user ${userId}`));
res.json({ userId, captured: true });
});
app.get('/test-trace-continuation', (_req, res) => {
Sentry.captureException(new Error('Trace continuation error'));
res.json({ ok: true });
});
app.get('/health', (_req, res) => {
res.json({ status: 'ok' });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});