leventcz/laravel-top

通过命令行实时监控Laravel应用。

v1.1.0 2024-05-27 03:17 UTC

This package is auto-updated.

Last update: 2024-09-09 15:26:27 UTC


README

Latest Version on Packagist GitHub Tests Action Status Licence

leventcz%2Flaravel-top | Trendshift

Real-time monitoring with Laravel Top

php artisan top

Top 为Laravel应用提供了从命令行直接进行实时监控的轻量级解决方案。它专为生产环境设计,让您轻松跟踪关键指标并识别最繁忙的路由。

工作原理

Top 监听Laravel事件,并将聚合数据保存到Redis中后台以计算指标。聚合数据以短TTL存储,确保不保留历史数据,并防止Redis过载。在显示期间,指标基于最后5秒数据的平均值计算。

Top 仅监听来自入站请求的事件,因此通过队列或命令执行的操作的指标不会反映出来。

由于数据存储在Redis中,因此top命令的输出反映了所有应用服务器的数据,而不仅仅是运行命令的服务器。

安装

与Laravel 10、Laravel 11和Laravel Octane兼容。

需要 PHP 8.2+ | Redis 5.0+

composer require leventcz/laravel-top

配置

您可以使用

php artisan vendor:publish --tag="top"
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Redis Connection
    |--------------------------------------------------------------------------
    |
    | Specify the Redis database connection from config/database.php
    | that Top will use to save data.
    | The default value is suitable for most applications.
    |
    */

    'connection' => env('TOP_REDIS_CONNECTION', 'default'),

    /*
    |--------------------------------------------------------------------------
    | Recording Mode
    |--------------------------------------------------------------------------
    |
    | Determine when Top should record application metrics based on this value.
    | By default, Top only listens to your application when it is running.
    | If you want to access metrics through the facade, you can select the "always" mode.
    |
    | Available Modes: "runtime", "always"
    |
    */

    'recording_mode' => env('TOP_RECORDING_MODE', 'runtime'),
];

外观

如果您想访问应用程序中的指标,您可以使用 Top 外观。

<?php

use Leventcz\Top\Facades\Top;
use Leventcz\Top\Data\Route;

// Retrieve HTTP request metrics
$requestSummary = Top::http();
$requestSummary->averageRequestPerSecond;
$requestSummary->averageMemoryUsage;
$requestSummary->averageDuration;

// Retrieve database query metrics
$databaseSummary = Top::database();
$databaseSummary->averageQueryPerSecond;
$databaseSummary->averageQueryDuration;

// Retrieve cache operation metrics
$cacheSummary = Top::cache();
$cacheSummary->averageHitPerSecond;
$cacheSummary->averageMissPerSecond;
$cacheSummary->averageWritePerSecond;

// Retrieve the top 20 busiest routes
$topRoutes = Top::routes();
$topRoutes->each(function(Route $route) {
    $route->uri;
    $route->method;
    $route->averageRequestPerSecond;
    $route->averageMemoryUsage;
    $route->averageDuration;
});

// Force Top to start recording for the given duration (in seconds)
Top::startRecording(int $duration = 5);

// Force Top to stop recording
Top::stopRecording();

// Check if Top is currently recording
Top::isRecording();

测试

composer test

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件