africandrogba
africandrogba

Reputation: 432

How to run prometheus library in the middleware of Nextjs?

import { Histogram } from 'prom-client';

const durationHistogram =  new Histogram({
  name: 'web_app_request_duration_seconds',
  help: 'Duration of HTTP requests in seconds',
  labelNames: ['method', 'route', 'code'],
  buckets: [0.1, 0.3, 1.5, 10, 30, 100],
});

export function middleware(request: NextRequest) {

  const stop = durationHistogram.startTimer();

  const response = NextResponse.next()
  stop({ route: request.nextUrl.pathname, method: request.method, code: response.status })
  return response;
}

This code errors out due to

A Node.js API is used (process.uptime) which is not supported in the Edge Runtime.

How to get around this error considering that vercel has no interest in supporting nodejs runtime for middleware ? What are other ideas to track the duration of each ssr request ?

Upvotes: 4

Views: 613

Answers (0)

Related Questions