This module provides prometheus instrumentation for server-side rendering via getServerSideProps, middleware, api
Note: Due to his nature the minimum supported Next.js version is 12.0.9. If you are using Next.js middleware the minimum supported version is 12.2.0. Currently is not supporting the new App Route. Open a new issue if you need such support.
nextjs-prometheus is instrumenting prometheus by monkey-patching the nextJS modules responsibile for SSR operations. It is currently wrapping:
It is also collecting the child process metrics using collectDefaultMetrics from prom-client
Disclaimer monkey-patching relies on the internal implementation of a module, which may change over time, leading to compatibility and maintenance issues.
Monkey-patching for Application Performance Monitoring (APM) provides a powerful and flexible approach, enabling seamless integration and deep insights into your application's inner workings without disrupting its original structure, ultimately enhancing observability and optimizing performance.
To use this library you can add the following script to your package.json
"start": "NODE_OPTIONS='--require nextjs-prometheus' next start",
Another aproach would be to wrap the method you wanna track in a HOF. Here I leave an example for getServerSideProps.
// withPrometheusMetrics.js
function withPrometheusMetrics(getServerSidePropsFunction) {
return async function (context) {
const start = process.hrtime();
// Call the original getServerSideProps function
const result = await getServerSidePropsFunction(context);
// Calculate the duration of the call
const diff = process.hrtime(start);
const duration = diff[0] + diff[1] / 1e9;
// Update Prometheus metrics
getServerSidePropsCounter.inc();
getServerSidePropsHistogram.observe(duration);
return result;
};
}
module.exports = withPrometheusMetrics;
You can then use the HOF inside your pages.
// pages/yourPage.js
import withPrometheusMetrics from '../withPrometheusMetrics';
export async function getServerSideProps(context) {
// Your original getServerSideProps logic
}
export default withPrometheusMetrics(getServerSideProps);