Laravel catch Server-to-server requests
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?
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:
https://github.com/itsgoingd/clockwork/blob/954d18ef6b0fa01190769341e09e981303a7c17c/Clockwork/Request/Timeline/Event.php#L94-L98
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:

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!
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.