-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
118 lines (100 loc) · 3.14 KB
/
app.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
113
114
115
116
117
118
const express = require('express');
const bodyParser = require("body-parser");
const cors = require('cors');
var app = express();
// Initalize Sentry (import library and instantiate)
const Sentry = require('@sentry/node');
const Tracing = require('@sentry/tracing');
Sentry.init({
dsn: 'https://276b9c69b15b41a3ae98d07206889b24@sentry.io/1366275',
integrations: [
// Enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// Enable Express.js middleware tracing
new Tracing.Integrations.Express({ app }),
],
// Sample rate can be set as a decimal between 0 and 1
// representing the percent of transactions to record
//
// For our example, we will collect all transactions
tracesSampleRate: 1.0,
});
let Inventory = {
wrench: 0,
nails: 0,
hammer: 1
};
let checkout = (cart) => {
let tempInventory = Inventory;
// check if we have enough Inventory
cart.forEach((item) => {
if (tempInventory[item.id] <= 0) {
throw Error("No inventory for " + item.id);
}
tempInventory[item.id]--;
});
// update Inventory if we have enough to fulfill this order
Inventory = tempInventory;
};
// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());
// TracingHandler creates a trace for every incoming request
app.use(Sentry.Handlers.tracingHandler());
app.use(bodyParser.json());
app.use(cors());
app.all('*', function (req, res, next) {
let transactionId = req.header('X-Transaction-ID'),
sessionId = req.header("X-Session-ID"),
order = req.body;
if (transactionId) {
Sentry.configureScope(scope => {
scope.setTag("transaction_id", transactionId);
});
}
if (sessionId) {
Sentry.configureScope(scope => {
scope.setTag("session_id", sessionId);
});
}
if (order.email) {
Sentry.configureScope(scope => {
scope.setUser({ email: order.email });
});
}
Sentry.configureScope(scope => {
scope.setExtra("inventory", JSON.stringify(Inventory));
});
next();
});
app.post('/checkout', function (req, res) {
let order = req.body;
console.log("Processing order for: " + order.email);
checkout(order.cart);
res.send('Success');
});
app.get('/capture-message', function (req, res) {
// Simulate an API call that takes a random amount
// of time and goes long
let delay = 2500;
Sentry.captureMessage('Custom Message');
setTimeout(() => { res.send('Success'); }, delay);
});
app.get('/unhandled', function (req, res) {
let obj = {};
obj.doesNotExist();
});
app.get('/handled', function (req, res) {
try {
let obj = {};
obj.doesNotExist();
res.send('Success');
} catch (error) {
Sentry.captureException(error);
res.status(500).send("Something broke");
}
});
// The error handler must be before any other error middleware
app.use(Sentry.Handlers.errorHandler());
app.listen(process.env.PORT || 3001, function () {
console.log('CORS-enabled web server listening on port 3001');
});