Laravel Pulse: Real-Time Application Health Monitoring
Monitoring is crucial for maintaining a healthy application. Laravel Pulse provides an instant, at-a-glance dashboard of your application's health and performance without the need for complex external APM tools.
Installation
composer require laravel/pulse
php artisan vendor:publish --provider="Laravel\Pulse\PulseServiceProvider"
php artisan migrate
Pulse stores its metrics in your database.
The Dashboard
Access your dashboard at /pulse. You need to authorize access in App\Providers\PulseServiceProvider:
protected function gate(): void
{
Gate::define('viewPulse', function (User $user) {
return in_array($user->email, ['admin@example.com']);
});
}
Key Metrics Monitored
Pulse tracks several key metrics out of the box:
- Server Stats: CPU, Memory, Disk usage.
- Application Usage: Top users making requests.
- Exceptions: Most frequent errors occurring in your app.
- Slow Routes: Endpoints that take the longest to respond.
- Slow Queries: SQL queries that need optimization.
- Jobs: Queue throughput and failure rates.
- Cache: Hit and miss ratios.
Configuring Recorders
You can customize what to record in config/pulse.php.
For example, to lower the threshold for "Slow Queries":
'recorders' => [
\Laravel\Pulse\Recorders\SlowQueries::class => [
'threshold' => 500, // 500ms
'sample_rate' => 1, // Record 100% of slow queries
],
],
Customizing the Dashboard
You can modify the dashboard layout by publishing the view:
php artisan vendor:publish --tag=pulse-dashboard
This creates resources/views/vendor/pulse/dashboard.blade.php. You can rearrange cards using standard Blade components:
<x-pulse>
<livewire:pulse.servers cols="full" />
<livewire:pulse.usage cols="4" rows="2" />
<livewire:pulse.slow-queries cols="8" />
</x-pulse>
Custom Cards (Livewire)
You can build your own metrics. For example, a "Business Revenue" card.
- Create a Recorder.
- Create a Livewire component for the card.
1. Recording Custom Data
Use the Pulse facade to record arbitrary entries.
// In a payment controller
Pulse::record('revenue', 'stripe', $amount, now());
2. Creating the Component
php artisan make:livewire RevenueCard
In the component class, fetch data using Pulse queries.
Pulse vs Telescope vs Horror
- Telescope: Best for debugging individual requests (inspecting headers, exact query flow). Great for local dev.
- Horizon: Best for queue management specifically (Redis only).
- Pulse: Best for aggregate performance health (aggregating trends over time). Great for production overview.
It's common to use all three: Telescope locally, Horizon for workers, and Pulse for production health checks.
Running the Worker
Pulse needs to aggregate data periodically.
php artisan pulse:check
In production, run the pulse:work command via Supervisor or a daemon to process incoming metrics without slowing down user requests.
php artisan pulse:work
Summary
Laravel Pulse fills the gap between local debugging and enterprise APM solutions like Datadog or New Relic. It's:
- Free and open-source.
- Self-hosted (data stays with you).
- Lightweight compared to external agents.
- Beautifully designed out of the box.
Start monitoring your slow queries today before your users report them!