Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rodrigobahia's avatar

Route Monitoring

I created a middleware and registered it in Kernel.app => API to save a log whenever the API routes are used, as I need to monitor some behaviors.

I am able to monitor all requests correctly using $_SERVER['REQUEST_URI'], but I also need to record the time it took for this route to return, thus understanding the performance of the endpoints. Currently, I use the handle() function of the middleware that is executed at the beginning of the request.

Can you tell me any way I can get this request time to save in the log?

Thanks

0 likes
6 replies
rodrigobahia's avatar

@Sinnbeck I did it as follows, in handle() I left only $this->startTime = microtime(true), I added the terminate() function and in it I put the rules to save the log, with the following time calculation

$duration = microtime(true) - $this->startTime; round($duration * 1000, 2)

I will analyze if the time calculation is working correctly, thanks for the help

Sinnbeck's avatar

@rodrigobahia Happy to help :) If it works out, remember to close the thread by marking a best answer

JussiMannisto's avatar

@rodrigobahia I don't know if you've noticed, but index.php defines a timestamp constant named LARAVEL_START at the start of every request, before the framework is bootstrapped. That can be used for measurements.

1 like

Please or to participate in this conversation.