-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
main.js
112 lines (96 loc) · 2.53 KB
/
main.js
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
const http = require('http');
const express = require('express');
const app = express();
const Sentry = require('../../../dist');
Sentry.init({
debug: true,
dsn: 'http://test@example.com/1337',
beforeSend(event) {
console.log(JSON.stringify(event, null, 2));
return null;
},
});
function hasSentryTraceHeader(req) {
return req.headers && typeof req.headers['sentry-trace'] === 'string';
}
class Tracing {
static start() {
return (req, _res, next) => {
const transaction = `${req.method.toUpperCase()} ${req.originalUrl}`;
const span = hasSentryTraceHeader(req)
? Span.fromTraceparent(req.headers['sentry-trace'], {
transaction,
})
: Sentry.startTransaction({
name: transaction,
});
Sentry.getCurrentHub().configureScope(scope => {
scope.setSpan(span);
});
next();
};
}
static finish() {
return (_req, _res, next) => {
const scope = Sentry.getCurrentHub().getScope();
if (!scope) {
return next();
}
const span = scope.getSpan();
if (!span) {
return next();
}
Sentry.getCurrentHub().finishSpan(span);
next();
};
}
}
async function databaseCall(_query) {
const span = Sentry.getCurrentHub().startSpan({
op: 'db',
});
return new Promise(resolve => {
setTimeout(() => {
Sentry.getCurrentHub().finishSpan(span);
resolve('http://whatever.com/raw');
}, Math.random() * 100);
});
}
async function httpCall(_url) {
const span = Sentry.getCurrentHub().startSpan({
op: 'http',
});
return new Promise(resolve => {
setTimeout(() => {
Sentry.getCurrentHub().finishSpan(span);
resolve('httpCall');
}, Math.random() * 100);
});
}
async function encodeData(_data) {
const span = Sentry.getCurrentHub().startSpan({
op: 'encode',
});
return new Promise(resolve => {
setTimeout(() => {
Sentry.getCurrentHub().finishSpan(span);
resolve('encodedData');
}, Math.random() * 100);
});
}
app.use(Sentry.Handlers.requestHandler());
app.use(Tracing.start());
app.get('/trace', async (_req, res, next) => {
const url = await databaseCall('SELECT url FROM queue WHERE processed = 0 LIMIT 1');
const raw = await httpCall(url);
const encoded = await encodeData(raw);
res.status(200).send(encoded);
next();
});
app.use(Tracing.finish());
app.use(Sentry.Handlers.errorHandler());
const server = app.listen(3000, () => {
http.get('http://localhost:3000/trace', res => {
server.close();
});
});