I should use prom-client in my nextjs app. but I have no idea for it. I cant find any example for it
I want to add Prometheus with prom-client to my nextjs app. I should use histogram in getServerSideProps function. this is my current code:
// api/metrics.js
import { promRegister } from "Utils/promClient";
export default async function handler(req, res) {
const client = await import("prom-client");
const register = new client.Registry();
const collectDefaultMetrics = client.collectDefaultMetrics;
collectDefaultMetrics({ register });
res.setHeader("Content-Type", promRegister.contentType);
res.status(200).send(await promRegister.metrics());
// Utils/promClient.js
import { register, Histogram, collectDefaultMetrics } from "prom-client";
register.clear();
export const performanceMeasurHistogram = new Histogram({
name: "getData",
help: "getData",
labelNames: ["status", "controller", "pageUrl"],
buckets: [0.5 ,1, 5, 10, 20, 50, 100],
});
const collectDefault = collectDefaultMetrics;
collectDefault();
export const promRegister = register;
// page/index.js
export async function getServerSideProps({ res, req }) {
const timer = performanceMeasurHistogram.startTimer();
const data = await axios.get(...)
timer({
status: data.status,
controller: data.request.path,
pageUrl:"/",
});
}
You can try to install the npm package:
Then, you can try create a new file called
prometheus.jsin your Next.js pages directory:Then import httpRequestCount:
Add a route for the /metrics endpoint in your next.config.js file:
Then you done!
you can start your Next.js application and navigate to
http://localhost:3000/metricsto view the Prometheus metrics.