-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
49 lines (38 loc) · 1.42 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
// running locally using express
const express = require('express');
const app = express();
const port = process.env.PORT || 8000;
// random response time variable
const responseTime = require("response-time");
const prom = require("prom-client"); // prometheus client for metircs collection
const collectDefaultMetrics = prom.collectDefaultMetrics;
collectDefaultMetrics({register: prom.register});
// this collects the request's response time from the server
const reqResTime = new prom.Histogram({
name: "https_express_req_res_time",
help: "This tells how much time is taken by request and response",
labelNames: ["method", "route", "status_code"],
buckets: [1, 50, 100, 200, 400, 500, 800, 1000, 2000]
});
app.use(responseTime((req, res, time) => {
reqResTime.labels({
method: req.method,
route: req.url,
status_code: res.statusCode,
})
.observe(time);
}));
app.use(express.static('public'));
// prom client gets metrics and hosts it to '../metrics' location
app.get("/metrics", async (req, res) =>{
res.setHeader('Content-Type', prom.register.contentType);
const metrics = await prom.register.metrics();
res.send(metrics);
});
app.get('/', (req, res) => {
res.sendFile('M:/SIH/sih-front/index.html');
});
// Start the server
app.listen(port, () => {
console.log(`Server is listening on port ${port}! Server running on http://yourip:8000`);
});