-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (59 loc) · 2.05 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
const express=require('express');
const app=express();
const PORT=process.env.PORT || 8000;
const {HeavyTask}=require("./utils")
const client=require("prom-client");
const responseTime=require("response-time");
const {createLogger,format,transports}=require("winston");
const LokiTransport=require("winston-loki");
const options={
transports:[
new LokiTransport({
labels:{
appName:"nodejs-prometheus"
},
host:'http://127.0.0.1:3100' })
]
}
const logger=createLogger(options);
const collectDefaultMetrics=client.collectDefaultMetrics;
collectDefaultMetrics({register:client.register})
const timeTOResponse=new client.Histogram({
name:"time_taken_for_response",
help:"this tells how much time is taken by req and res",
labelNames:["method",'route',"status_code"],
buckets:[1,50,100,200,300,400,500,1000,2000,3000,5000]
})
const totalRequest=new client.Counter({
name:"total_request",
help:"this tells how many request are made",
})
app.use(responseTime((req,res,time)=>{
totalRequest.inc();
timeTOResponse.labels({method:req.method,route:req.url,status_code:res.statusCode})
.observe(time);
}
))
app.get("/",(req,res)=>{
logger.info("Request is made to /");
res.status(200).json({success:true,message:"server is up and running"});
})
app.get("/heavytask",async(req,res)=>{
logger.info("Request is made to /heavytask");
try {
const timetaken=await HeavyTask();
return res.status(200).json({success:true,message:`Heavy task completed in ${timetaken}`});
} catch (error) {
logger.error(error);
return res.status(500).json({success:false,message:"Internal went wrong"});
}
})
//route for sending matrics to prometheus Server running in docker container
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(`Server is running on port ${PORT}`);
})