
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
nextjs-prometheus
Advanced tools
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);
FAQs
Observability of a nextjs application w/ Prometheus
The npm package nextjs-prometheus receives a total of 156 weekly downloads. As such, nextjs-prometheus popularity was classified as not popular.
We found that nextjs-prometheus demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.