alexpon92/php2elk-metrics
Composer 安装命令:
composer require alexpon92/php2elk-metrics
包简介
Package to transfer application and monitoring metrics to ELK
关键字:
README 文档
README
- Overview
- Installation
- Configuration
- Usage
- Index and default mappings creation
- Check mappings
- Non Laravel users
Overview
This package helps to transfer application metrics to elasticsearch to some indices with prepared mappings for metrics fields. Using Kibana on top of elastic gives you an opportunity to create informative dashboards or trigger some monitoring events. You can use it with Laravel applications or with other frameworks. This package is based on official elasticsearch client (https://github.com/elastic/elasticsearch-php).
Installation
To install through composer, run the following command from terminal:
composer require alexpon92/php2elk-metrics
Configuration
Register the main package provider in your applications
Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider::class
It gives you an opportunity to publish default config and edit it:
php artisan vendor:publish --provider="Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider" --tag=config
Also, you may publish another provider to register main package services in your service container:
php artisan vendor:publish --provider="Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider" --tag=service-provider
It will place package service provider in your App\Providers directory.
Usage
- Create your custom metrics package has
BaseMetricclass, and all of your metrics must extend this base class. - Implement necessary methods:
/** * Unique metric name * @return string */ abstract public static function getName(): string; /** * Method to convert metric contents to array * * @return array */ abstract public function arraySerialize(): array;
getName method provides unique metric name.
arraySerialize method helps to serialize metric fields to associative array.
Example of custom metric:
namespace App\Metrics; use Php2ElkMetrics\Metrics\BaseMetric; use DateTime; class SomeVisitDurationMetric extends BaseMetric { /** * @var string */ private $userName; /** * @var float */ private $duration; /** * SomeVisitDurationMetric constructor. * * @param string $userName * @param float $duration * @param DateTime|null $time */ public function __construct(string $userName, float $duration, ?DateTime $time) { $this->userName = $userName; $this->duration = $duration; $this->time = $time; // time is protected field from BaseMetric class } public static function getName(): string { return 'some_duration_metric'; } public function arraySerialize(): array { return [ 'user_name' => $this->userName, 'duration' => $this->duration ]; } }
- Add your new metrics to metrics registry
If you have published default package provider with command:
php artisan vendor:publish --provider="Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider" --tag=service-provider
You should edit it and add your new metric in register method:
$this->app->singleton( Registry::class, static function ($app) { /** @var Container $app */ $metricRegistry = new Registry(); $defaultIndex = config('php2elk-metrics.default_index'); $defaultConnection = config('php2elk-metrics.connections.default'); $metricRegistry->add( new MetricsConfig( SomeVisitDurationMetric::getName(), $defaultIndex, $defaultConnection ) ); return $metricRegistry; } );
- Add metric mappings in elasticsearch for your index, for instance in kibana dev console:
PUT <index-name>/_mappings
{
"properties": {
"some_duration_metric": {
"properties": {
"user_name": {
"type": "keyword"
},
"duration": {
"type": "float"
}
}
}
}
}
IMPORTANT! If you will not add mapping for new metrics and produce metrics from your application, your new fields will be converted to strings by elastics and you can't use it in aggregations etc., only for concrete search. Only reindex may help in this situation.
- Now you can produce it in your application
$producer = app(\Php2ElkMetrics\MetricsProducer\MetricsProducer::class); $metric = new SomeVisitDurationMetric('John', 12.02); $producer->produceMetric($metric);
After publish, document in elastic will have next structure:
{
"application": "some-app", //may be changed in config
"instance": "test-instance", //may be changed in config
"timestamp": 1587735900, //time which is passed in metrics constructor
"metric_name": "some_duration_metric", //unique metric key, to be able to filter it in elastic index
"some_duration_metric": { //metric object content
"user_name": "John",
"count": 15.02
}
- Asynch Metrics producing
Package has default listener to produce metric in asynch mode to prevent any additional delays.
To use it, you should register
Php2ElkMetrics\Laravel\Listeners\MetricsEventListenerin yourApp\Providers\EventServiceProviderinprotected $subscribeattribute
$metric = new SomeVisitDurationMetric('John', 12.02); event(new \Php2ElkMetrics\Events\MetricEvent($metric, new \DateTime()));
- Bulk metrics producing
$producer = app(\Php2ElkMetrics\MetricsProducer\MetricsProducer::class); $metricsCollection = new \Php2ElkMetrics\Metrics\MetricsCollection(); $metricsCollection ->add(new SomeVisitDurationMetric('John', 12.02)) ->add(new SomeVisitDurationMetric('Alex', 30.02)); $producer->bulkProduce($metricsCollection);
Create Index and default mappings
Package has artisan command to prepare an index for your metrics:
php artisan php2elk:setup-index {--connection_name=} {--index_name=} {--default_metrics}
--connection_name - to specify connection name from your config (or default connection)
--index_name - index name (or default index from config if not passed)
--default_metrics - flag to add default package metrics
Package has some default metrics and collectors:
- Dead rows collector for PostgreSQL
\Php2ElkMetrics\Laravel\Collectors\Postgres\DeadRowsMetricsCollectorThis collector estimates number of dead rows in concrete database and producePhp2ElkMetrics\Metrics\DefaultMetrics\Postgres\TableDeadRowsMetricAlso you can use artisan command to launch collection of this metric it in crontab
php artisan php2elk:php2elk:collect-postgres-dead-rows
- Failed queue jobs collector
\Php2ElkMetrics\Laravel\Collectors\FailedQueues\FailedQueuesMetricsCollectorThis collector estimates number of failed jobs and produce\Php2ElkMetrics\Laravel\DefaultMetrics\FailedQueue\FailedQueueJobsMetricAlso you can use artisan command to launch collection of this metric it in crontab
php artisan php2elk:php2elk:php2elk:collect-failed-queues
- Latency metric
\Php2ElkMetrics\Metrics\BaseMetric\LatencyMetricThis metric helps to estimate latency of some actions in your application.
Check mappings
To check metrics mappings in elasticsearch package has command
php artisan php2elk:check-mappings
It helps you not to corrupt your index and to check new metrics mappings in elasticsearch before deploy of new version.
Non Laravel users
WIP
alexpon92/php2elk-metrics 适用场景与选型建议
alexpon92/php2elk-metrics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.98k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「Metrics」 「elasticsearch」 「ELK」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alexpon92/php2elk-metrics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alexpon92/php2elk-metrics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alexpon92/php2elk-metrics 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
simple api library.
A abstraction for generating metrics in Laravel.
Laravel SDK for working with prometheus metrics
Production-ready Laravel Prometheus metrics package with built-in collectors for HTTP, database, cache, queue, events, errors, filesystem, and mail monitoring
Tool to show code coverage metrics, measured by PHP Unit in Cobertura format, in console and CI/CD pipeline
Symfony bundle for Elasticsearch integration with round-robin load balancing
统计信息
- 总下载量: 23.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-27