Socket
Socket
Sign inDemoInstall

nextjs-prometheus

Package Overview
Dependencies
4
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nextjs-prometheus

Observability of a nextjs application w/ Prometheus


Version published
Weekly downloads
48
decreased by-14.29%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

Prometheus NextJS instrumentation

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.

What is all about

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

Why Monkeypatching

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.

How to use it

To use this library you can add the following script to your package.json

 "start": "NODE_OPTIONS='--require nextjs-prometheus' next start",

Other alternatives

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

Last updated on 02 Jul 2023

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc