Download the PHP package stechstudio/laravel-metrics without Composer
On this page you can find all versions of the php package stechstudio/laravel-metrics. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download stechstudio/laravel-metrics
More information about stechstudio/laravel-metrics
Files in stechstudio/laravel-metrics
Package laravel-metrics
Short Description Easily track metrics from Laravel events, or on your own
License MIT
Informations about the package laravel-metrics
Laravel Metrics
This package makes it incredibly easy to ship app metrics to backends such as PostHog, InfluxDB or CloudWatch.
There are two major components: a facade that lets you create metrics on your own, and an event listener to automatically send metrics for Laravel events.
Installation
You know the drill...
Backend configuration
PostHog
-
Install the PostHog PHP client:
composer require posthog/posthog-php - Add the following to your
.envfile:
InfluxDB v1.7 and under
-
Install the InfluxDB PHP client:
composer require influxdb/influxdb-php - Add the following to your
.envfile:
InfluxDB V1.8 and above
-
Install the InfluxDB PHP client:
composer require influxdata/influxdb-client-php -
Add the following to your
.envfile: - In order to use UDP with InfluxDB V1.8+ you must follow extra setup steps
Add the following to your .env file:
CloudWatch
-
Install the AWS PHP SDK:
composer require aws/aws-sdk-php. - Add the following to your
.envfile:
Prometheus
- Install the Prometheus PHP client:
composer require promphp/prometheus_client_php - Configuring the backend to use Prometheus, makes sense only if you have an endpoint to expose them. Its purpose is only to format the registered metrics in a way that Prometheus can scrape them.
NullDriver (for development)
If you need to disable metrics just set the backend to null:
This null driver will simply discard any metrics.
Sending an individual metric
You can create metric by using the facade like this:
The only required attribute is the name, everything else is optional.
Driver mapping
This is how we are mapping metric attributes in our backends.
| Metric attribute | PostHog | InfluxDB | CloudWatch | Prometheus |
|---|---|---|---|---|
| name | event | measurement | MetricName | name |
| value | properties[value] | fields[value] | Value | value |
| unit | ignored | ignored | Unit | ignored |
| resolution | ignored | ignored | StorageResolution | ignored |
| tags | ignored | tags | Dimensions | keys -> labelNames values -> labelValues |
| extra | properties | fields | ignored | ignored |
| timestamp | ignored | timestamp | Timestamp | ignored |
| description | ignored | ignored | ignored | help |
| namespace | ignored | ignored | ignored | namespace |
| type | ignored | ignored | ignored | used to register counter or gauge metric |
See the CloudWatch docs and InfluxDB docs for more information on their respective data formats. Note we only do minimal validation, you are expected to know what data types and formats your backend supports for a given metric attribute.
Sending metrics from Laravel events
The main motivation for this library was to send metrics automatically when certain events occur in a Laravel application. So this is where things really get fun!
Let's say you have a simple Laravel event called OrderReceived:
The first step is to implement an interface:
This will tell the global event listener to send a metric for this event.
There are two different ways you can then provide the metric details.
1. Use the ProvidesMetric trait
You can also include a trait that helps with building this metric:
In this case, the trait will build a metric called order_received (taken from the class name) with a value of 1.
Customizing event metric data
If you decide to use the trait, you likely will want to customize the event metric data.
You can provide metric data with class attributes:
Or if some of your metric data is dynamic you can use getter methods:
You can provide any of our metric attributes using these class attributes or getter methods.
2. Create the metric yourself
Depending on how much detail you need to provide for your metric, it may be simpler to just build it yourself. In this
case you can ditch the trait and simply provide a public createMetric function that returns a new Metric instance:
Default tags and extra data
You can set default tags and extra data on the driver that will be merged into every metric:
Dynamic extra data with closures
If you need extra data that is evaluated at the time each metric is dispatched (rather than when it's initially set), you can pass a closure:
The closure will be called fresh each time a metric is formatted. This also works on individual metrics:
User ID resolution
Drivers automatically resolve the current user ID via getUserId():
- If a user is authenticated:
auth()->id() - Otherwise:
getAnonymousId(), which returns a UUID stored in the session (stable across requests), or a static UUID for non-session contexts (CLI, queues)
This is used by the PostHog driver as the distinctId, and is available to any driver via $driver->getUserId().
Custom user ID resolver
You can override the default resolution by providing your own closure. The driver instance is passed to your closure, so you can fall back to the built-in anonymous ID if needed:
This will apply to all drivers. You can also set it on a specific driver: