leventcz/laravel-top
Real-time, lightweight monitoring for Laravel from the CLI. Listens to request events, aggregates short-lived metrics in Redis, and displays 5-second averages across all app servers. Ideal for production to spot load, latency, and busiest routes.
composer require leventcz/laravel-top
config/database.php (default connection works).php artisan vendor:publish --tag="top"
php artisan top
Incident Response:
php artisan top in a separate terminal during outages to monitor live metrics.averageDuration to identify bottlenecks (e.g., >500ms).php artisan top --filter="duration>500"
Post-Deployment Validation:
$routes = Top::routes()->where('averageDuration', '>', 300)->values('uri');
// Log or alert on degraded routes.
Load Testing:
Top::startRecording() to capture metrics during artificial load:
Top::startRecording(30); // Record for 30 seconds
// Simulate traffic...
$summary = Top::http();
Custom Monitoring:
$criticalRoutes = Top::routes()
->where('averageMemoryUsage', '>', 1000) // >1MB
->pluck('uri');
config['top']['connection'] = 'cache') to avoid cluttering the default DB.php artisan top to deployment scripts to validate performance post-merge.Cache::remember('top_metrics', 10, fn() => Top::http())).Redis Dependency:
try {
$metrics = Top::http();
} catch (\RedisException $e) {
Log::error("Top metrics unavailable: Redis down", ['error' => $e]);
}
Recording Mode Quirks:
recording_mode=always only works during HTTP requests (queues/commands are ignored).Top::startRecording() for manual control.Preflight Requests:
$routes = Top::routes()->where('method', '!=', 'OPTIONS');
Memory Spikes:
No Data?:
redis-cli ping.recording_mode in config/top.php (default: runtime).Illuminate\Http\RequestHandled).Stale Metrics:
TTL constant).Facade Errors:
composer dump-autoload
php artisan optimize:clear
Custom Metrics:
Leventcz\Top\Events\MetricCollected event to log additional data:
Top::extend(function ($collector) {
$collector->custom('custom_metric', fn() => memory_get_usage(true));
});
Route Filtering:
app/Providers/TopServiceProvider.php:
public function boot()
{
Top::macro('filterRoutes', function ($callback) {
return $this->routes()->filter($callback);
});
}
Output Formatting:
Leventcz\Top\Console\TopCommand class.config/database.php keys (e.g., default, cache).TTL constant (default: 5s) in vendor/leventcz/laravel-top/src/TopServiceProvider.php (not recommended for production).TOP_REDIS_CONNECTION=cache
TOP_RECORDING_MODE=always
How can I help you explore Laravel packages today?