The calls your app makes to other people's APIs are the latency you can't fix by optimizing your own code, and the failures you can't prevent by writing correct code. Stripe has a slow afternoon, a shipping provider's API hangs, a third party you've never thought about quietly adds 400ms to checkout, and from your monitoring it just looks like "checkout got slower" with no obvious cause inside your app. Tracking outgoing HTTP turns "our app is slow" into "this specific downstream is slow," so you know whose problem it is.
Layer 1: Http facade events
Laravel's Http client fires three events you can listen to in a service provider:
RequestSending: before the request goes outResponseReceived: after a response arrivesConnectionFailed: when the connection couldn't be established
app/Providers/AppServiceProvider.php
use Illuminate\Http\Client\Events\RequestSending;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Http\Client\Events\ConnectionFailed;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
public function boot(): void
{
Event::listen(function (ResponseReceived $event) {
Log::info('Outgoing HTTP', [
'url' => $event->request->url(),
'method' => $event->request->method(),
'status' => $event->response->status(),
'duration_ms' => $event->response->transferStats?->getTransferTime() * 1000,
]);
});
Event::listen(function (ConnectionFailed $event) {
Log::warning('HTTP connection failed', [
'url' => $event->request->url(),
'method' => $event->request->method(),
]);
});
}Good for audit trail. Missing: per-host aggregation. A single log line doesn't tell you Stripe is degraded. You need counts and percentiles over time.
Layer 2: Guzzle middleware
If you're on Guzzle directly (not Http::), use middleware:
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Illuminate\Support\Facades\Log;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
$stack = HandlerStack::create();
$stack->push(function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
$start = microtime(true);
// The handler returns a promise; log once the response arrives
return $handler($request, $options)->then(
function (ResponseInterface $response) use ($request, $start) {
Log::info('Outgoing', [
'host' => $request->getUri()->getHost(),
'path' => $request->getUri()->getPath(),
'status' => $response->getStatusCode(),
'duration_ms' => round((microtime(true) - $start) * 1000, 2),
]);
return $response;
}
);
};
});
$client = new Client(['handler' => $stack]);Layer 3: Per-host aggregation
The goal isn't logging every call. It's seeing which downstream services are hurting you. Aggregate by host:
- Request count per host (which services do we depend on most?)
- p95 duration per host (which service is slow?)
- 4xx and 5xx counts per host (which service is failing?)
- Failure-to-timeout ratio (is this connection issues or real errors?)
You can build this with an outgoing_requests table and a nightly rollup job, but then the table, the job and the charts are yours to maintain. An APM that records outgoing HTTP is usually the better tradeoff.
Set timeouts on every outgoing call
Raw Guzzle defaults to no timeout, so a hung downstream will hang your PHP worker indefinitely. Laravel's Http client sets 30 seconds (and a 10-second connect timeout) by default, which is still too long for most user-facing calls. Set your own:
// Set per-call (recommended for user-facing requests)
Http::timeout(5)
->connectTimeout(2)
->get('https://api.stripe.com/v1/charges');
// Set defaults for every call in AppServiceProvider::boot()
Http::globalOptions(['timeout' => 5, 'connect_timeout' => 2]);
// Always use ->throw() when you need to fail loudly
Http::timeout(5)->get($url)->throw();Patterns that reduce outgoing HTTP pain
- Move to async jobs. If you can call an external API in a queued job instead of the request, do it. The user stops waiting on the third party's response time.
- Cache responses. Use Laravel's Cache facade with short TTLs for read-heavy external data (pricing tables, currency rates, etc.).
- Circuit break. When a downstream fails repeatedly, fail fast instead of waiting for every timeout. A simple cache-based failure counter works.
- Parallel calls. Http::pool() fires multiple requests concurrently, which turns three sequential 300ms calls into roughly one 300ms wait.
- Correlation IDs. Pass X-Request-Id on every outgoing call so downstream logs can be correlated back to the originating request.