nibtime / next-safe-middleware

Strict CSP (Content-Security-Policy) for Next.js hybrid apps https://web.dev/strict-csp/
https://next-safe-middleware.vercel.app
MIT License
78 stars 20 forks source link

excessive CPU usage warning from Vercel for middleware #45

Closed nibtime closed 2 years ago

nibtime commented 2 years ago

Just checked my emails and received a warning from Vercel about excessive CPU usage of middleware, both on my hobby account and a Pro Team account. For middleware, there is a limit of 50ms CPU execution time per request, on average

I merged the release commit for 0.8.0 on Jul 7, 2022 at 6:13PM (UTC), with also redeployed the e2e app with the new version.

Strange though is, that the avg. CPU time per request doesn't execeed 50ms on any day + a project on the Pro account that uses middleware doesn't use the new release and still runs on 0.6.0 with Beta middleware. Also both alert arrived around the same time, (Fr. 8 Juli, 3:08PM and 3:10PM (UTC).

Hobby CPU utilization ![2022-07-12 02_47_12](https://user-images.githubusercontent.com/52962482/178384537-8f851f89-3f02-4e45-a09c-93518db3f282.png)
Pro Team CPU utilization ![2022-07-12 02_47_58](https://user-images.githubusercontent.com/52962482/178384506-6d88857f-d10b-4c37-9b92-b0c37ffab915.png)

I got in touch with Vercel support and asked what I should do. I'll update this issue once I get a reply

nibtime commented 2 years ago

The reply from @vercel was

Thank you for proactively looking at this! In your troubleshooting efforts, the function logs ... for the current production deployment may shed some light on what requests are resulting in high usage times.

However, function logs don't give insight into CPU utilization (there seems no good telemetry story for middleware yet, see https://github.com/vercel/next.js/discussions/34420).

I did some improvements to decrease CPU utilization in #47, somewhat into the blue, and by educated guessing. I also provided a telemetry wrapper middleware to measure execution time by logging to the console

I just hammered a new deployment with autocannon to observe effects on average CPU utilization in Vercel Dashboard. The changes to seem to have a significant effect:

CPU utilization (5,85ms avg) ![2022-07-23 12_12_40](https://user-images.githubusercontent.com/52962482/180601172-01623dfb-249c-4762-81c4-e85d440acaf2.png)
Invocations (4060) ![2022-07-23 12_12_57](https://user-images.githubusercontent.com/52962482/180601180-3730d6d8-4aa0-4426-91d0-d35dda4a0c6b.png)