Reference for code :
https://github.com/RitikaxG/Promethus-Metrics-Counter-Guage-Histogram-
Prometheus is a self-hosted monitoring system that:
/metrics HTTP endpoints.In short: Prometheus = metrics scraper + time-series DB + alerting engine (pull-based monitoring).


Prometheus can’t be scaled horizontally
Can’t create multiple machines that’s distributing load among themselves.
Prometheus store data in time series format
Even if no one’s hitting ur application, u re still collecting metrics every x seconds
import express from "express"
import type { Request, Response, NextFunction } from "express";
const app = express();
app.use(express.json())
const middleware = (req : Request,res : Response, next : NextFunction) => {
const startTime = Date.now();
res.on("finish",()=>{
const endTime = Date.now();
console.log(`Time taken : ${endTime - startTime}ms | Method Type : ${req.method} | Request route : ${req.path}`)
})
next()
}
// Every endpoint will use middleware
app.use(middleware);
app.get("/cpu",(req,res) => {
let sum = 0
for(let i=0;i<100000000;i++){
sum += i
}
return res.json({
message : "Expensive operation successfully completed",
sum
})
})
app.get("/health",(req,res) => {
return res.send("ok")
})
app.listen(3000,()=>{
console.log("Listening on port 3000")
})
