Ability to measure request time. Logging middleware #34420
Replies: 8 comments 9 replies
-
Yeah, the whole telemetry story is a bit disappointing. I think the only approach is to go the custom server route and then write a custom Express middleware. Only then you have the problem again that you can't resolve the API route name from the url's path name causing a high cardinality for your metrics. You could then mess around with trying to detect your route variables with some regex and then replace then with some token e.g. I do think you might be able to do it via |
Beta Was this translation helpful? Give feedback.
-
+1 on this, also trying to figure it out. Would love some help here. It's frustrating because the docs mention that this should be possible using waitUntil, but there's no explanation of how: https://nextjs.org/docs/api-reference/next/server#nextfetchevent |
Beta Was this translation helpful? Give feedback.
-
+1. I recently got a warning from Vercel about CPU utilization but the averages in Vercel Dashboard don't show any problems. I can't use The Vercel Support replied to me that I should measure and log middleware execution in my latest production build, but I have really no clue how to do that. Some guidance on that in docs would be great. All I can do atm to test this is guess and hammer preview builds with requests to observe averages. Not ideal.
|
Beta Was this translation helpful? Give feedback.
-
Hey! Thanks for raising this. Do you know any updates on this? Unfortunately, I'm looking for a similar solution, and I can't find anything (neither in official docs nor googling). Thanks for any tips on how you solved that! |
Beta Was this translation helpful? Give feedback.
-
+1 on this. The current implementation of middleware does not allow such a feature. |
Beta Was this translation helpful? Give feedback.
-
Are there any updates on this? |
Beta Was this translation helpful? Give feedback.
-
I had same issue, what I ended it for now it to patch next
|
Beta Was this translation helpful? Give feedback.
-
it needs to be a middleware feature, there's no way to put this on all api and server actions etc, it's not a good idea I thought about using some sort of Proxy magic import { NextResponse } from 'next/server';
export function middleware(req) {
const response = NextResponse.next();
// Create a Proxy to trap reads
const p = new Proxy({}, {
get(target, prop) {
if (prop === 'toString') {
console.log('Header value was read!');
return () => 'triggered-value'; // Provide a default string value
}
return Reflect.get(target, prop);
}
});
response.headers.set('X-Trigger-On-Read', p);
return response;
} but the above won't work and is dumb |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the feature you'd like to request
Hey guys,
Currently im trying to measure the time nextjs needs for serving the response.
For this use case I tried to implement this logic inside a "global" /pages/_middleware.ts.
Unfortunately NextResponse does not provide any possibility to do so.
So I cannot add an event listener. Is there a way to implement such logic?
The logging example provided in the Middleware documentation is missing!
Describe the solution you'd like
I want a possibility to measure the actual response time of a request.
Describe alternatives you've considered
The Express Request Interface inherits a class called
Writable
where you have the possibility to register event listeners.It would be great if the interface
NextResponse
also had such methods.Beta Was this translation helpful? Give feedback.
All reactions