-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (71 loc) · 2.15 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
import express from "express";
import responseTime from "response-time";
import client from "prom-client";
import { createLogger, transports } from "winston";
import LokiTransport from "winston-loki";
const options = {
transports: [
new LokiTransport({
labels: {
appName: "express",
},
host: "http://127.0.0.1:3100",
}),
],
};
const logger = createLogger(options);
import { doSomeHeavyTask } from "./utill.js";
const app = express();
const PORT = process.env.PORT || 8080;
const collectDefaultMetrics = client.collectDefaultMetrics;
collectDefaultMetrics({ register: client.register });
const reqResTime = new client.Histogram({
name: "http_express_req_res_time",
help: "This tells how much time is taken by req and res",
labelNames: ["method", "route", "Status_code"],
buckets: [1, 50, 100, 200, 400, 500, 800, 1000, 2000, 3000],
});
const totalReqCounter = new client.Counter({
name: "total_req",
help: "Tells total req",
});
app.use(
responseTime((req, res, time) => {
totalReqCounter.inc();
reqResTime
.labels({
method: req.method,
route: req.url,
Status_code: res.statusCode,
})
.observe(time);
})
);
app.get("/", (req, res) => {
logger.info("Req come on / route");
return res.json({ message: "Hello from Express Server" });
});
app.get("/slow", async (req, res) => {
try {
logger.info("Req come on /slow route");
const timeTaken = await doSomeHeavyTask();
console.log(`Heavy task completed in ${timeTaken.timeTaken} ms`);
return res.json({
status: "Success",
message: `Heavy task completed in ${timeTaken.timeTaken} ms`,
});
} catch (e) {
logger.error(e.message);
return res
.status(500)
.json({ status: "Error", error: "Internal Server Error" });
}
});
app.get("/metrics", async (req, res) => {
res.setHeader("Content-Type", client.register.contentType);
const metrics = await client.register.metrics();
res.send(metrics);
});
app.listen(PORT, () =>
console.log(`Express Server Started at http:localhost:${PORT}`)
);