leventcz/laravel-top
Real-time CLI monitoring for Laravel. Runs php artisan top to track key request metrics, busiest routes, and performance across all servers. Aggregates recent Laravel event data in Redis with short TTL, designed for production and Octane.
Installation:
composer require leventcz/laravel-top
Publish Configuration (optional, uses defaults otherwise):
php artisan vendor:publish --tag="top"
Run the CLI tool:
php artisan top
php artisan top during a production incident to identify the busiest routes, memory spikes, or slow queries in real-time. The output shows:
Top Routes section to identify performance bottlenecks.Top::routes() or Top::http() in custom scripts or tests to programmatically access metrics.config/top.php for Redis connection settings and recording modes.Real-Time Monitoring in Production:
php artisan top in a production environment to monitor live traffic across all servers (via shared Redis).recording_mode config to toggle between runtime (default, only during CLI execution) and always (continuous recording).Programmatic Access:
$topRoutes = Top::routes();
$topRoutes->each(function ($route) {
if ($route->averageDuration > 1000) { // >1s
// Trigger alert or log
}
});
CI/CD Integration:
php artisan top --format=json | jq '.http.averageDuration' > /tmp/deploy_metrics.json
Multi-Server Environments:
Laravel Octane:
recording_mode=always cautiously, as it may impact Octane’s event loop performance under high load.Redis Optimization:
Custom Metrics:
jobs.processed) and pushing data to Redis using the same pattern as the core package.Alerting:
php artisan top --format=json | jq -r '.routes[] | select(.averageDuration > 1000) | "🚨 Slow route: \(.uri) - \(.averageDuration)ms"' | curl -X POST -d @- https://hooks.slack.com/services/...
Testing:
public function test_route_performance()
{
$routes = Top::routes();
$this->assertLessThan(500, $routes->first()->averageDuration, 'Route is too slow');
}
Redis Dependency:
try {
$metrics = Top::http();
} catch (\RedisException $e) {
// Fallback to logs or cached metrics
Log::error('Top metrics failed: ' . $e->getMessage());
}
Recording Mode Quirks:
recording_mode=always records metrics continuously, which may impact performance in high-traffic apps. Use sparingly and monitor Redis load.runtime mode (default), metrics are only recorded when php artisan top is running. This can lead to gaps in data if the CLI isn’t actively monitored.Excluded Metrics:
Preflight Requests:
$routes = Top::routes()->reject(fn ($route) => $route->method === 'OPTIONS');
Multi-Server Data Accuracy:
Laravel 13.x/Octane Edge Cases:
CLI Output Issues:
php artisan top hangs or crashes, check Redis connectivity and Laravel logs (storage/logs/laravel.log).config/top.php matches your config/database.php settings.Stale or Missing Data:
recording_mode=always, ensure the process running Laravel isn’t terminated (e.g., in a long-running Octane server).High Memory Usage:
redis-cli info memory. If spikes occur, reduce the aggregation window or optimize Redis settings (e.g., maxmemory-policy).Facade API Race Conditions:
Top::startRecording() calls). Use locks if needed:
\Illuminate\Support\Facades\Lock::options(['timeout' => 10])->block('top-recording-lock', function () {
Top::startRecording();
});
Custom Metrics:
// In a service provider
event(new \Leventcz\Top\Events\RequestHandled($request));
// Then push custom data to Redis using the same key pattern as the package.
Override Templates:
vendor/leventcz/laravel-top/resources/views. Publish the views first:
php artisan vendor:publish --tag="top-views"
Add New Data Sources:
illuminate.query) and push data to Redis using the package’s Top::store() method (if exposed in future versions).Modify Aggregation Logic:
Leventcz\Top\Services\Aggregator class.Redis Connection:
default) assumes Redis is configured in config/database.php. If using a custom connection, specify it explicitly:
'connection' => 'cache',
Recording Mode:
recording_mode=always is not recommended for production unless you’re actively monitoring Redis performance. Prefer runtime mode for most use cases.Environment-Specific Settings:
'recording_mode' => env('TOP_RECORDING_MODE', 'runtime'),
.env:
TOP_RECORDING_MODE=always
TOP_REDIS_CONNECTION=cache
High Traffic:
redis-benchmark and tune Redis settings (e.g., appendfsync everysec, maxmemory-policy allkeys-lru).Octane Compatibility:
Memory Leaks:
recording_mode=always is active. The package should not leak memory, but high request rates may stress Redis.How can I help you explore Laravel packages today?