forked from clementduveau/intro-to-mltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
490 lines (430 loc) · 16.7 KB
/
index.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
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
const traceUtils = require('./tracing')('server', 'mythical-server');
const Pyroscope = require('@pyroscope/nodejs');
const { expressMiddleware } = require('@pyroscope/nodejs');
const logUtils = require('./logging')('mythical-server', 'server');
(async () => {
const traceObj = await traceUtils();
const logEntry = await logUtils(traceObj);
const { tracer, api } = traceObj;
const promClient = require('prom-client');
const express = require('express');
const bodyParser = require('body-parser');
const { Client } = require('pg');
const { nameSet, servicePrefix, spanTag } = require('./endpoints')();
// Prometheus client registration
const app = express();
const register = promClient.register;
register.setContentType(promClient.Registry.OPENMETRICS_CONTENT_TYPE);
// Database full teardown timeout
const teardownTimeout = 24 * 60 * 60 * 1000; // Default is every 24 hours
let teardownInProgress = false;
// Use JSON parsing in the request body
app.use(bodyParser.json());
let pgClient;
// Database actions
const Database = {
GET: 0,
POST: 1,
DELETE: 2,
DROP: 3,
CREATE: 4,
};
// Status response bucket (histogram)
const responseBucket = new promClient.Histogram({
name: 'mythical_request_times',
help: 'Response times for the endpoints',
labelNames: ['method', 'status', spanTag],
buckets: [1, 4, 8, 10, 20, 50, 100, 200, 500, 1000],
enableExemplars: true,
});
// Database action function
const databaseAction = async (action) => {
// Which action?
const span = api.trace.getSpan(api.context.active());
span.setAttribute('span.kind', api.SpanKind.CLIENT);
if (action.method === Database.GET) {
const results = await pgClient.query(`SELECT name from ${action.table}`);
return results.rows;
} else if (action.method === Database.POST) {
return await pgClient.query(`INSERT INTO ${action.table}(name) VALUES ($1)`, [ action.name ]);
} else if (action.method === Database.DELETE) {
return await pgClient.query(`DELETE FROM ${action.table} WHERE name = $1`, [ action.name ]);
} else if (action.method == Database.DROP) {
const traceId = api.trace.getSpan(api.context.active()).spanContext();
for (const table of nameSet) {
await pgClient.query(`DROP TABLE ${table}`);
logEntry({
level: 'info',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
message: `traceId=${traceId} message="Dropped table ${table}..."`,
});
}
return;
} else if (action.method === Database.CREATE) {
const traceId = api.trace.getSpan(api.context.active()).spanContext();
for (const table of nameSet) {
await pgClient.query(`CREATE TABLE IF NOT EXISTS ${table}(id serial PRIMARY KEY, name VARCHAR (50) UNIQUE NOT NULL);`);
logEntry({
level: 'info',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
message: `traceId=${traceId} message="Created table ${table}..."`,
});
}
return;
}
logEntry({
level: 'error',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
message: 'message="Method was not valid, throwing error"',
});
throw new Error(`Not a valid ${spanTag} method!`);
}
// Response time bucket function (adds a Prometheus value)
const responseMetric = (details) => {
const timeMs = Date.now() - details.start;
const spanContext = api.trace.getSpan(api.context.active()).spanContext();
responseBucket.observe({
labels: details.labels,
value: timeMs,
exemplarLabels: {
traceID: spanContext.traceId,
spanID: spanContext.spanId,
},
});
};
// Metrics endpoint handler (for Prometheus scraping)
app.get('/metrics', async (req, res) => {
res.set('Content-Type', register.contentType);
res.send(await register.metrics());
});
// Initialise the Pyroscope library to send pprof data.
Pyroscope.init({
appName: 'mythical-beasts-server',
});
app.use(expressMiddleware());
// Generic GET endpoint
app.get('/:endpoint', async (req, res) => {
const endpoint = req.params.endpoint;
const currentSpan = api.trace.getSpan(api.context.active());
const spanContext = currentSpan.spanContext();
const traceId = spanContext.traceId;
currentSpan.setAttribute(spanTag, endpoint);
let metricBody = {
labels: {
method: 'GET',
},
start: Date.now(),
};
metricBody.labels[spanTag] = endpoint;
if (!nameSet.includes(endpoint)) {
res.status(404).send(`${endpoint} is not a valid endpoint`);
metricBody.labels.status = '404';
responseMetric(metricBody);
return;
}
// If we're in the middle of a teardown, don't do anything
if (teardownCheck({
spanContext,
endpoint,
method: 'GET',
res,
}) === true) {
return;
}
// Retrieve all the names
try {
const results = await databaseAction({
method: Database.GET,
table: endpoint,
});
// Metrics
metricBody.labels.status = '200';
responseMetric(metricBody);
logEntry({
level: 'info',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
endpointLabel: spanTag,
endpoint,
message: `traceID=${traceId} endpoint=${endpoint} http.method=GET status=SUCCESS`,
});
res.send(results);
} catch (err) {
metricBody.labels.status = '500';
responseMetric(metricBody);
logEntry({
level: 'error',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
endpointLabel: spanTag,
endpoint,
message: `traceID=${traceId} endpoint=${endpoint} http.method=GET status=FAILURE err="${err}"`,
});
res.status(500).send(err);
}
});
// Generic POST endpoint
app.post('/:endpoint', async (req, res) => {
const endpoint = req.params.endpoint;
const currentSpan = api.trace.getSpan(api.context.active());
const spanContext = currentSpan.spanContext();
const traceId = spanContext.traceId;
let metricBody = {
labels: {
method: 'POST',
},
start: Date.now(),
};
metricBody.labels[spanTag] = endpoint;
if (!nameSet.includes(endpoint)) {
res.status(404).send(`${endpoint} is not a valid endpoint`);
metricBody.labels.status = '404';
responseMetric(metricBody);
return;
}
if (!req.body || !req.body.name) {
// Here we'd use 'respondToCall()' which would POST a metric for the response
// code
metricBody.labels.status = '400';
responseMetric(metricBody);
}
// If we're in the middle of a teardown, don't do anything
if (teardownCheck({
spanContext,
endpoint,
method: 'POST',
res,
}) === true) {
return;
}
// POST a new unicorn name
try {
await databaseAction({
method: Database.POST,
table: endpoint,
name: (Math.random() < 0.1) ? null : req.body.name,
});
// Metrics
metricBody.labels.status = '201';
responseMetric(metricBody);
logEntry({
level: 'info',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
endpointLabel: spanTag,
endpoint,
message: `traceID=${traceId} endpoint=${endpoint} http.method=POST status=SUCCESS`,
});
res.sendStatus(201);
} catch (err) {
// Metrics
metricBody.labels.status = '500';
responseMetric(metricBody);
logEntry({
level: 'error',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
endpointLabel: spanTag,
endpoint,
message: `traceID=${traceId} endpoint=${endpoint} http.method=GET status=FAILURE err="${err}"`,
});
res.status(500).send(err);
}
});
// Generic DELETE endpoint
app.delete('/:endpoint', async (req, res) => {
const endpoint = req.params.endpoint;
const currentSpan = api.trace.getSpan(api.context.active());
const spanContext = currentSpan.spanContext();
const traceId = spanContext.traceId;
let metricBody = {
labels: {
method: 'DELETE',
},
start: Date.now(),
};
metricBody.labels[spanTag] = endpoint;
if (!nameSet.includes(endpoint)) {
res.status(404).send(`${endpoint} is not a valid endpoint`);
metricBody.labels.status = '404';
responseMetric(metricBody);
return;
}
if (!req.body || !req.body.name) {
// Here we'd use 'respondToCall()' which would POST a metric for the response
// code
metricBody.labels.status = '400';
responseMetric(metricBody);
}
// If we're in the middle of a tearxdown, don't do anything
if (teardownCheck({
spanContext,
endpoint,
method: 'GET',
res,
}) === true) {
return;
}
// Delete a manticore name
try {
await databaseAction({
method: Database.DELETE,
table: endpoint,
name: req.body.name,
});
// Metrics
metricBody.labels.status = '204';
responseMetric(metricBody);
logEntry({
level: 'info',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
endpointLabel: spanTag,
endpoint,
message: `traceID=${traceId} endpoint=${endpoint} http.method=DELETE status=SUCCESS`,
});
res.sendStatus(204);
} catch (err) {
// Metrics
metricBody.labels.status = '500';
responseMetric(metricBody);
logEntry({
level: 'error',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
endpointLabel: spanTag,
endpoint,
message: `traceID=${traceId} endpoint=${endpoint} http.method=DELETE status=FAILURE err="${err}"`,
});
res.status(500).send(err);
}
});
// Destroy the DB table and recreate it
const tableWipe = async () => {
const requestSpan = tracer.startSpan('server');
const { traceId } = requestSpan.spanContext();
// Create a new context for this request
await api.context.with(api.trace.setSpan(api.context.active(), requestSpan), async () => {
// You know, there are positives and negatives to using an event thread
// based model. But when it comes to stuff like this, I sure don't miss
// pthread mutices. One variable change. One.
teardownInProgress = true;
try {
// DROP the table
logEntry({
level: 'info',
job: `${process.env.NAMESPACE}/${servicePrefix}-server`,
namespace: process.env.NAMESPACE,
message: `traceId=${traceId} message="DROPing tables..."`,
});
await databaseAction({
method: Database.DROP,
});
// Recreate the tables for each endpoint
logEntry({
level: 'info',
job: `{servicePrefix}-server`,
namespace: process.env.NAMESPACE,
message: `traceId=${traceId} message="CREATEing tables..."`,
});
await databaseAction({
method: Database.CREATE,
});
} catch(err) {
logEntry({
level: 'info',
job: `${servicePrefix}-server`,
namespace: process.env.NAMESPACE,
message: `traceId=${traceId} error="${err}"`,
});
} finally {
teardownInProgress = false;
requestSpan.end();
}
});
};
// Checks to see if there's a teardown in progress
const teardownCheck = (details) => {
const { spanContext, endpoint, method, res } = details;
// If we're in the middle of a teardown, don't do anything.
if (teardownInProgress) {
logEntry({
level: 'error',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
message: `traceID=${traceId} endpoint=${endpoint} http.method=${method} status=FAILURE err='Table is not available'`,
});
res.status(500).send('Table is not available');
return true;
}
return false;
};
// Create the DB and connect to it
const startServer = async () => {
const requestSpan = tracer.startSpan('server');
// Create a new context for this request
await api.context.with(api.trace.setSpan(api.context.active(), requestSpan), async () => {
try {
logEntry({
level: 'info',
job: `${servicePrefix}-server`,
namespace: process.env.NAMESPACE,
message: 'Installing postgres client...',
});
pgClient = new Client({
host: 'mythical-database',
port: 5432,
user: 'postgres',
password: 'mythical',
});
await pgClient.connect();
const results = await pgClient.query(`SELECT COUNT(*) FROM pg_catalog.pg_database WHERE datname = '${spanTag}';`);
if (results.rows[0].exists === false) {
logEntry({
level: 'info',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
message: 'Database entry did not exist, creating...',
});
await pgClient.query(`CREATE DATABASE ${spanTag}`);
}
logEntry({
level: 'info',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
message: 'Creating tables...',
});
// Create the tables.
await databaseAction({
method: Database.CREATE,
});
// Listen to API connections.
app.listen(4000);
// Schedule a table wipe in the future.
setInterval(() => tableWipe(), teardownTimeout);
logEntry({
level: 'info',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
message: `${servicePrefix} server up and running...`,
});
} catch (err) {
pgClient.end();
logEntry({
level: 'info',
namespace: process.env.NAMESPACE,
job: `${servicePrefix}-server`,
message: `${servicePrefix} server could not start, trying again in 5 seconds... ${err}`,
});
setTimeout(() => startServer(), 5000);
} finally {
requestSpan.end();
}
});
};
// Start up the API server
startServer();
})();