stechstudio / laravel-metrics
轻松跟踪Laravel事件或您自己的指标
Requires
- php: ^8.1
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- aws/aws-sdk-php: ^3.2
- guzzlehttp/guzzle: ^7.5
- influxdata/influxdb-client-php: ^3.4
- influxdb/influxdb-php: ^1.15
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
- posthog/posthog-php: ^3.0
- promphp/prometheus_client_php: ^2.10
Suggests
- aws/aws-sdk-php: AWS CloudWatch integration
- influxdata/influxdb-client-php: For integration with InfluxDB 1.8 or later
- influxdb/influxdb-php: For integration with InfluxDB 1.7 or earlier
- posthog/posthog-php: PostHog integration
- promphp/prometheus_client_php: For integration with Prometheus
README
此包使得将应用指标发送到PostHog、InfluxDB或CloudWatch等后端变得极其简单。
有两个主要组件:一个门面,让您可以创建自己的指标,以及一个事件监听器,用于自动发送Laravel事件的指标。
安装
您知道下一步该怎么做...
composer require stechstudio/laravel-metrics
后端配置
PostHog
-
安装PostHog PHP客户端:
composer require posthog/posthog-php
-
将以下内容添加到您的
.env
文件中
METRICS_BACKEND=posthog
POSTHOG_API_KEY=...
InfluxDB v1.7及以下版本
-
安装InfluxDB PHP客户端:
composer require influxdb/influxdb-php
-
将以下内容添加到您的
.env
文件中
METRICS_BACKEND=influxdb
IDB_USERNAME=...
IDB_PASSWORD=...
IDB_HOST=...
IDB_DATABASE=...
IDB_VERSION=1 # Default
# Only if you are not using the default 8086
IDB_TCP_PORT=...
# If you want to send metrics over UDP instead of TCP
IDB_UDP_PORT=...
InfluxDB V1.8及以上版本
-
安装InfluxDB PHP客户端:
composer require influxdata/influxdb-client-php
-
将以下内容添加到您的
.env
文件中 -
为了使用InfluxDB V1.8+的UDP,您必须遵循额外的设置步骤
将以下内容添加到您的.env
文件中
METRICS_BACKEND=influxdb
IDB_TOKEN=...
IDB_DATABASE=... # Use the name of your desired bucket for this value
IDB_HOST=...
IDB_ORG=...
IDB_VERSION=2
# Only if you are not using the default 8086
IDB_TCP_PORT=...
# If you want to send metrics over UDP instead of TCP
IDB_UDP_PORT=...
CloudWatch
-
安装AWS PHP SDK:
composer require aws/aws-sdk-php
。 -
将以下内容添加到您的
.env
文件中
METRICS_BACKEND=cloudwatch
CLOUDWATCH_NAMESPACE=...
AWS_DEFAULT_REGION=...
AWS_ACCESS_KEY_ID=...
AWS_SECRET_ACCESS_KEY=...
Prometheus
- 安装Prometheus PHP客户端:
composer require promphp/prometheus_client_php
- 配置后端以使用Prometheus,只有在您有端点可以公开它们时才有意义。其目的是仅以Prometheus可以抓取的方式格式化已注册的指标。
METRICS_BACKEND=prometheus
NullDriver(用于开发)
如果您需要禁用指标,只需将后端设置为null即可
METRICS_BACKEND=null
此null
驱动程序将简单地丢弃任何指标。
发送单个指标
您可以通过门面创建指标,如下所示
Metrics::create('order_placed') ->setValue(1) ->setTags([ 'source' => 'email-campaign', 'user' => 54 ]);
唯一的必填属性是name
,其他所有属性都是可选的。
驱动映射
这是我们映射后端中指标属性的方式。
有关各自数据格式的更多信息,请参阅CloudWatch文档和InfluxDB文档。请注意,我们仅进行最小验证,您应该知道后端支持哪些数据类型和格式,对于给定的指标属性。
从Laravel事件发送指标
此库的主要动机是在Laravel应用程序发生某些事件时自动发送指标。因此,这里将变得非常有趣!
假设您有一个简单的Laravel事件,名为OrderReceived
class OrderReceived { protected $order; public function __construct($order) { $this->order = $order; } }
第一步是实现一个接口
use STS\Metrics\Contracts\ShouldReportMetric; class OrderReceived implements ShouldReportMetric {
这将告诉全局事件监听器为该事件发送一个指标。
您有两种不同的方式可以提供指标详情。
1. 使用ProvidesMetric
特质
您还可以包含一个有助于构建此指标的重质
use STS\Metrics\Contracts\ShouldReportMetric; use STS\Metrics\Traits\ProvidesMetric; class OrderReceived implements ShouldReportMetric { use ProvidesMetric;
在这种情况下,特质将构建一个名为order_received
(从类名中提取)的指标,其值为1
。
自定义事件指标数据
如果您决定使用特质,您可能希望自定义事件指标数据。
您可以使用类属性提供指标数据
class OrderReceived implements ShouldReportMetric { use ProvidesMetric; protected $metricName = "new_order"; protected $metricTags = ["category" => "revenue"]; ...
或者,如果您的某些指标数据是动态的,您可以使用getter方法
public function getMetricValue() { return $this->order->total; }
您可以使用这些类属性或getter方法提供任何我们的指标属性。
2. 自己创建指标
根据您需要为指标提供多少细节,自己构建可能更简单。在这种情况下,您可以放弃特性,只需提供一个公共的 createMetric
函数,该函数返回一个新的 Metric
实例
use STS\Metrics\Contracts\ShouldReportMetric; use STS\Metrics\Metric; class OrderReceived implements ShouldReportMetric { protected $order; public function __construct($order) { $this->order = $order; } public function createMetric() { return (new Metric('order_received')) ->setValue(...) ->setTags([...]) ->setTimestamp(...) ->setResolutions(...); } }