ensi/laravel-metrics
Composer 安装命令:
composer require ensi/laravel-metrics
包简介
laravel metrics
README 文档
README
The package adds general-purpose metrics for a laravel web application. It is an addition toensi/laravel-prometheus
Installation
You can install the package via composer:
composer require ensi/laravel-metrics
Publish the config with:
php artisan vendor:publish --provider="Ensi\LaravelMetrics\MetricsServiceProvider"
Basic Usage
Add Http Middleware
# app/Http/Kernel.php protected $middleware = [ // ... other middlewares \Ensi\LaravelMetrics\HttpMiddleware\HttpMetricsMiddleware::class, ];
Add Guzzle Middleware to your http clients
$handlerStack = HandlerStack::create(); // Basic usage - only collects host-level metrics $handlerStack->push(GuzzleMiddleware::middleware()); // With detailed path-level metrics $handlerStack->push(GuzzleMiddleware::middleware('http_client', true, false)); // With histogram statistics $handlerStack->push(GuzzleMiddleware::middleware('http_client', false, true)); // With both path metrics and statistics $handlerStack->push(GuzzleMiddleware::middleware('http_client', true, true)); $client = new Client(['handler' => $handlerStack]); $response1 = $client->get('http://httpbin.org/get');
The middleware() method accepts three parameters:
$type(string, default: 'http_client'): Metric type identifier$collectPathMetrics(bool, default: false): Enable per-path metrics collection (http_client_path_*)$collectPathStats(bool, default: false): Enable detailed statistics collection (http_client_path_stats)
Configuration
The structure of the configuration file
return [ 'ignore_commands' => [ 'kafka:consume', ], 'ignore_routes' => [ 'prometheus.*' ], 'http_requests_stats_groups' => [ '<stats-group-name>' => [ // If your app runs in multiple containers and each of them is responsible for its own metrics, // then you don't need to use the "summary" 'type' => 'summary', 'route_names' => ['*'], // or use prefix, like ['catalog.*', 'profile.favorites'], 'time_window' => 30, 'quantiles' => [0.5, 0.75, ,0.95], ], '<stats-group-name>' => [ 'type' => 'histogram', 'route_names' => ['*'], // or use prefix, like ['catalog.*', 'profile.favorites'], 'buckets' => [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10], ], ], 'http_client_path_stats_buckets' => [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10], 'watch_queues' => [ 'default', ], ];
ignore_routes - a list of names of routes for which you do not need to track the processing time of http requests.
ignore_commands - a list of team names for which you do not need to track metrics.
http_requests_stats_groups - a list of histograms and percentiles. Each stats group has a list of the names of the routes that it tracks.
Thus, you can count statistics not for the entire application, but for individual groups of endpoints.
http_client_path_stats_buckets - bucket configuration for the http_client_path_stats histogram metric used when collecting detailed statistics for HTTP client requests.
Metrics
The names of the metrics are presented without the namespace.
| Name | Type | Labels | Description |
|---|---|---|---|
| http_requests_total | Counter | code, endpoint | Counter of incoming http requests |
| http_request_duration_seconds | Counter | code, type, endpoint | Time counter for processing incoming http requests |
| http_stats_<name> | Histogram or Summary | Statistics on request processing time for the endpoint group specified in the config | |
| http_client_requests_total | Counter | host | Counter of outgoing HTTP client requests |
| http_client_seconds_total | Counter | host | Time counter for outgoing HTTP client requests |
| http_client_path_requests_total | Counter | host, path | Counter of outgoing HTTP client requests per path |
| http_client_path_seconds_total | Counter | host, path | Time counter for outgoing HTTP client requests per path |
| http_client_path_stats | Histogram | host, path | Statistics on outgoing HTTP client request processing time |
| log_messages_count | Counter | level, endpoint | Number of messages in the log |
| queue_job_dispatched_total | Counter | connection, queue, job | The number of jobs sent to the queue |
| queue_job_runs_total | Counter | connection, queue, job | The number of processed jobs in the queue |
| queue_job_run_seconds_total | Counter | connection, queue, job | Time counter for completing tasks in the queue |
| command_runs_total | Counter | command, status | Number of completed commands |
| command_run_seconds_total | Counter | command, status | Command execution time counter |
| workers_total | Gauge | worker | Number of swoole workers |
| workers_idle | Gauge | worker | Number of free swoole workers |
Contributing
Please see CONTRIBUTING for details.
Testing
- composer install
- composer test
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.
ensi/laravel-metrics 适用场景与选型建议
ensi/laravel-metrics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 71.55k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 12 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ensi/laravel-metrics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ensi/laravel-metrics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 71.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-10