Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Request lifecycle duration handler #44122

Merged
merged 2 commits into from
Sep 15, 2022

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Sep 14, 2022

This PR is in a similar spirit the the cumulative query duration PR. It allows developers to easily handle when an application request lifecycle exceeds a certain threshold.

use Carbon\CarbonInterval as Interval;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Log;


public function boot()
{
    if ($this->app->runningInConsole()) {
        return;
    }

    $this->app[Kernel::class]->whenRequestLifecycleIsLongerThan(
        Interval::seconds(1),
        fn ($startedAt, $request, $response) => /* ... */
    );

}

The feature includes the duration of the request handle method and the terminating of the middleware and application.

This gives developers a nice hook around the entire lifecycle of the request, as seen: https://github.com/laravel/laravel/blob/c1dc4199b83466a3a6a8c70953250b0e2ec70001/public/index.php#L51-L55

This is an easy starting point for tracking slow requests and not a full blown replacement for APM, but gives developers some quick insight into their system with little to no extra knowledge / setup.

$this->app[Kernel::class]->whenRequestLifecycleIsLongerThan(
    Interval::seconds(1),
    fn ($startedAt, $request, $response) =>
        Log::notice('Request exceeded duration threshold.', [
            'user' => $request->user()?->id,
            'url' => $request->fullUrl(),
            'duration' => $startedAt->floatDiffInSeconds(),
        ])
);

The handler is invoked after the request has been returned to the user.

Command compliment: #44125

@timacdonald timacdonald marked this pull request as ready for review September 14, 2022 07:58
@taylorotwell taylorotwell merged commit 4cae1c9 into laravel:9.x Sep 15, 2022
@timacdonald timacdonald deleted the request-duration branch September 16, 2022 00:09
@christophrumpel
Copy link
Contributor

Hey, just gave this a try. Is there a reason this does NOT work, when I use the HTTP Kernel namespace, and not the kernel contract? (with the contract it works)

@timacdonald
Copy link
Member Author

timacdonald commented Sep 26, 2022

Hey christoph,

Laravel only binds the Kernel against the contract, so resolving the contract is the only way (that I know of) to resolve the instance Laravel is using.

If you resolve the "HTTP Kernel namespace" instance, you are resolving a new instance and not the one that is handling the incoming request.

If you wanted to be able to resolve the HTTP namespace Kernel, you would need to bind it to the same instance that the contract uses...

Like:

$this->app->singleton(\App\Http\Kernel::class, fn ($a) => 
    $a[\Illuminate\Contracts\Http\Kernel::class)
);

Which feels a little backwards. Alternatively, we could PR a helper to the service provider that handles the resolution under the hood:

public function boot()
{
    $this->whenRequestLifecycleIsLongerThan(/* ... *);
}

which proxies the call to the resolved contract instance, but I don't know that we need it.

@christophrumpel
Copy link
Contributor

Hey Tim, thanks for clearing this up. I don't think there is a need currently to change it. It's just something people have to be aware of when using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants