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

Laravel catch Server-to-server requests #629

Open
joelharkes opened this issue Mar 28, 2023 · 2 comments
Open

Laravel catch Server-to-server requests #629

joelharkes opened this issue Mar 28, 2023 · 2 comments

Comments

@joelharkes
Copy link

Laravel introduced events to catch server-to-server calls made in the framework: https://laravel.com/docs/10.x/http-client#events

protected $listen = [
    'Illuminate\Http\Client\Events\RequestSending' => [
        'App\Listeners\LogRequestSending',
    ],
    'Illuminate\Http\Client\Events\ResponseReceived' => [
        'App\Listeners\LogResponseReceived',
    ],
    'Illuminate\Http\Client\Events\ConnectionFailed' => [
        'App\Listeners\LogConnectionFailed',
    ],
];

These can be used to catch server-to-server calls and show them in the logging or in a separate tab:

for example i did some simple HTTP request logging:

  Event::listen(function (RequestSending $requestSending) {
            $headersText = collect($requestSending->request->headers())
                ->map(fn($values, $key) => $key . ": ". (is_array($values) ? implode(", ", $values) : $values) )
                ->join("\r\n");
            clock("{$requestSending->request->method()} {$requestSending->request->url()}\r\n{$headersText}\r\n\r\n{$requestSending->request->body()}");
        });

Can this be added to the default framework integration?

@FeBe95
Copy link
Contributor

FeBe95 commented Apr 21, 2023

Background:

Some additional info on this from my side: I first read your article on dev.to @joelharkes while researching this exact feature. Later, I found this open issue and noticed that you are the author of both the article and this issue. 😅
The HTTP events are available at least since Laravel 8.x (https://laravel.com/docs/8.x/http-client#events)!

Code modifications:

Next I implemented your suggested code and modified it a bit in order to show the HTTP requests in the timeline. I stripped the header & body info from my code as I didn't need them for my specific use case.
I kept the logging feature since the links in the timeline are not clickable.

Further I changed the event registration to be inside the global EventServiceProvider.php file. FYI: I didn't need to add the ClockworkMiddleware::class into the Kernel.php as it works just fine without it in my case.

You can find my modified implementation in this gist:
https://gist.github.com/FeBe95/e6fcfd704799b0c17a02bce6ecd451ec

Clockwork modifications:

Apart from that I also modified Clockworks' code by adding one extra line here:

public function toArray()
{
return [
'description' => $this->description,
'start' => $this->start,

To turn it into this:

public function toArray()
{
	return [
		'name'        => $this->name,
		'description' => $this->description,
		'start'       => $this->start,

With this change I am able to achieve a behavior similar to how SQL queries are displayed:
I can set a short method + host/path string in the name field to display in the timeline bar and the full url string in the description field to display in the popup:

popup

Additional thoughts:

I'd love to see a default framework integration! Maybe a new "HTTP" tab could be introduced, just like the "Log" and "Database" tab. A new color + icon combination in the timeline for the requests would also be a nice addition.

I am very much looking forward to seeing this feature getting integrated into clockwork directly!

@itsgoingd
Copy link
Owner

Hey, thanks for the suggestion. I will leave this for the next major release, since I want to do a more complete integration, with a new tab for the HTTP requests as suggested above.

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

No branches or pull requests

3 participants