alexpon92/ php2elk-metrics
将应用程序和监控指标传输到ELK的包
Requires
- php: >=7.1
- elasticsearch/elasticsearch: ^7.4
Requires (Dev)
- orchestra/testbench: ^5.1
- phpunit/phpunit: ^8.0
- roave/security-advisories: dev-master
README
概述
此包帮助将应用程序指标传输到Elasticsearch的某些索引,这些索引已为指标字段准备好的映射。在Elasticsearch之上使用Kibana为您提供创建信息仪表板或触发一些监控事件的机会。您可以将其与Laravel应用程序或其他框架一起使用。此包基于官方Elasticsearch客户端(https://github.com/elastic/elasticsearch-php)。
安装
要通过composer安装,请在终端运行以下命令
composer require alexpon92/php2elk-metrics
配置
在您的应用程序中注册主要包提供者
Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider::class
这为您提供了发布默认配置并编辑它的机会
php artisan vendor:publish --provider="Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider" --tag=config
此外,您还可以发布另一个提供者以在服务容器中注册主要包服务
php artisan vendor:publish --provider="Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider" --tag=service-provider
它将包服务提供者放置在您的 App\Providers
目录中。
使用
- 创建您的自定义指标包具有
BaseMetric
类,您的所有指标都必须扩展此基类。 - 实现必要的方法
/** * 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
方法提供唯一的指标名称。
arraySerialize
方法有助于将指标字段序列化为关联数组。
自定义指标示例
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 ]; } }
- 将您的新指标添加到指标注册表
如果您已通过命令发布默认包提供者
php artisan vendor:publish --provider="Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider" --tag=service-provider
您应该编辑它并在注册方法中添加您的新的指标
$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; } );
- 为您的索引添加Elasticsearch中的指标映射,例如在Kibana开发控制台中
PUT <index-name>/_mappings
{
"properties": {
"some_duration_metric": {
"properties": {
"user_name": {
"type": "keyword"
},
"duration": {
"type": "float"
}
}
}
}
}
重要!如果您不添加新指标的映射并从您的应用程序生成指标,Elasticsearch将您的新的字段转换为字符串,您无法将其用于聚合等,仅用于具体搜索。只有重新索引可能有助于这种情况。
- 现在您可以在您的应用程序中生成它
$producer = app(\Php2ElkMetrics\MetricsProducer\MetricsProducer::class); $metric = new SomeVisitDurationMetric('John', 12.02); $producer->produceMetric($metric);
发布后,Elastic中的文档将具有以下结构
{
"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
}
- 异步指标生成包具有默认监听器,以异步模式生成指标以防止任何额外的延迟。要使用它,您应该在
App\Providers\EventServiceProvider
中的protected $subscribe
属性中注册Php2ElkMetrics\Laravel\Listeners\MetricsEventListener
$metric = new SomeVisitDurationMetric('John', 12.02); event(new \Php2ElkMetrics\Events\MetricEvent($metric, new \DateTime()));
- 批量指标生成
$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);
创建索引和默认映射
包有一个Artisan命令来为您的指标准备索引
php artisan php2elk:setup-index {--connection_name=} {--index_name=} {--default_metrics}
--connection_name
- 指定配置中的连接名称(或默认连接) --index_name
- 索引名称(或未提供时配置中的默认索引) --default_metrics
- 标志以添加默认包指标
包有一些默认指标和收集器
- PostgreSQL的死亡行收集器
\Php2ElkMetrics\Laravel\Collectors\Postgres\DeadRowsMetricsCollector
此收集器估计特定数据库中的死亡行数并生成Php2ElkMetrics\Metrics\DefaultMetrics\Postgres\TableDeadRowsMetric
您还可以使用Artisan命令在crontab中启动此指标的收集
php artisan php2elk:php2elk:collect-postgres-dead-rows
- 失败的队列作业收集器
\Php2ElkMetrics\Laravel\Collectors\FailedQueues\FailedQueuesMetricsCollector
此收集器估计失败的作业数量并生成\Php2ElkMetrics\Laravel\DefaultMetrics\FailedQueue\FailedQueueJobsMetric
您还可以使用Artisan命令在crontab中启动此指标的收集
php artisan php2elk:php2elk:php2elk:collect-failed-queues
- 延迟指标
\Php2ElkMetrics\Metrics\BaseMetric\LatencyMetric
此指标有助于估计应用程序中某些操作的延迟。
检查映射
要检查Elasticsearch中的指标映射,包有命令
php artisan php2elk:check-mappings
它可以帮助您防止索引损坏,并在部署新版本之前检查Elasticsearch中的新指标映射。
非Laravel用户
待续