-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·61 lines (51 loc) · 1.45 KB
/
server.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
const express = require('express');
const app = express();
const client = require('prom-client');
let register = new client.Registry();
const headsCount = new client.Counter({
name: "heads_count",
help: "Number of heads"
});
const tailsCount = new client.Counter({
name: "tails_count",
help: "Number of tails"
});
const flipCount = new client.Counter({
name: "flip_count",
help: "Number of flips"
});
register.registerMetric(headsCount);
register.registerMetric(tailsCount);
register.registerMetric(flipCount);
register.setDefaultLabels({
app: 'coin-api'
});
client.collectDefaultMetrics({ register });
app.get('/flip-coins', (request, response) => {
const times = request.query.times;
if (times && times > 0) {
flipCount.inc(Number(times));
let heads = 0;
let tails = 0;
for (let i = 0; i < times; i++) {
let randomNumber = Math.random();
if (randomNumber < 0.5) {
heads++;
} else {
tails++;
}
}
headsCount.inc(heads);
tailsCount.inc(tails);
response.json({ heads, tails });
} else {
response.send('hello! i work!!');
}
});
app.get('/metrics', async (request, response) => {
response.setHeader('Content-type', register.contentType);
response.end(await register.metrics());
});
app.listen(5000, () => {
console.log('Started server. Listening on port 5000.');
});