Skip to content

Commit

Permalink
v2 (#18)
Browse files Browse the repository at this point in the history
* cleanup, php8 types, upgraded deps, first stab at posthog driver

* driver created callback

* no default value, some backends do better without it

* posthog test

* list PH up front

* ci

* ci

* .
  • Loading branch information
jszobody authored Aug 6, 2023
1 parent f05dcd8 commit 01eb0fc
Show file tree
Hide file tree
Showing 27 changed files with 406 additions and 459 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: CI PHP 8.2

on: [push]

jobs:
build-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: php-actions/composer@v6
with:
php_version: "8.2"

- name: PHPUnit Tests
uses: php-actions/phpunit@v3
env:
APP_ENV: testing
APP_KEY: 7yPSFp$SCRcRh8zkq?!B$!5tPEF$cxq3
XDEBUG_MODE: coverage
with:
bootstrap: vendor/autoload.php
configuration: phpunit.xml
php_extensions: xdebug
args: --coverage-text
php_version: "8.2"
version: "10"
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
vendor
.idea
composer.lock
build
build
.phpunit
.phpunit.cache
19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

74 changes: 46 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

[![Latest Version on Packagist](https://img.shields.io/packagist/v/stechstudio/laravel-metrics.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-metrics)
[![Total Downloads](https://img.shields.io/packagist/dt/stechstudio/laravel-metrics.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-metrics)
[![Build Status](https://img.shields.io/travis/stechstudio/laravel-metrics/master.svg?style=flat-square)](https://travis-ci.org/stechstudio/laravel-metrics)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)

This package makes it incredibly easy to ship app metrics to backends such as InfluxDB or CloudWatch.
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.

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...
Expand All @@ -19,9 +19,22 @@ composer require stechstudio/laravel-metrics

## Backend configuration

### PostHog

1. Install the PostHog PHP client: `composer require posthog/posthog-php`

2. Add the following to your `.env` file:

```
METRICS_BACKEND=posthog
POSTHOG_API_KEY=...
```

### InfluxDB

Add the following to your `.env` file:
1. Install the InfluxDB PHP client: `composer require influxdata/influxdb-client-php`

2. Add the following to your `.env` file:

```
METRICS_BACKEND=influxdb
Expand All @@ -39,9 +52,9 @@ IDB_UDP_PORT=...

### CloudWatch

First make sure you have AWS itself properly setup. That means `composer install aws/aws-sdk-php` and making sure you have your AWS credentials configured.
From there, you simply need to add:
1. Install the AWS PHP SDK: `composer require aws/aws-sdk-php`.

2. Add the following to your `.env` file:

```
METRICS_BACKEND=cloudwatch
Expand All @@ -60,7 +73,7 @@ If you need to disable metrics just set the backend to null:
METRICS_BACKEND=null
```

This `null` driver will simply discard any metrics.
This `null` driver will simply discard any metrics.

## Sending an individual metric

Expand All @@ -75,27 +88,31 @@ Metrics::create('order_placed')
]);
```

The only required attribute is the `name`, everything else is optional.
The only required attribute is the `name`, everything else is optional.

## Driver mapping

This is how we are mapping metric attributes in our backends.
This is how we are mapping metric attributes in our backends.

| Metric attribute | InfluxDB | CloudWatch |
| ---------------- | ------------- | ----------------- |
| name | measurement | MetricName |
| value | fields[value] | Value |
| unit | _ignored_ | Unit |
| resolution | _ignored_ | StorageResolution |
| tags | tags | Dimensions |
| extra | fields | _ignored_ |
| timestamp | timestamp | Timestamp |
| Metric attribute | PostHog | InfluxDB | CloudWatch |
|------------------|-------------------|---------------|-------------------|
| name | event | measurement | MetricName |
| value | properties[value] | fields[value] | Value |
| unit | _ignored_ | _ignored_ | Unit |
| resolution | _ignored_ | _ignored_ | StorageResolution |
| tags | _ignored_ | tags | Dimensions |
| extra | properties | fields | _ignored_ |
| timestamp | _ignored_ | timestamp | Timestamp |

See the [CloudWatch docs](http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html) and [InfluxDB docs](https://docs.influxdata.com/influxdb/latest/concepts/key_concepts/) 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.
See the [CloudWatch docs](http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html)
and [InfluxDB docs](https://docs.influxdata.com/influxdb/latest/concepts/key_concepts/) 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!
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:

Expand All @@ -111,21 +128,21 @@ class OrderReceived {
```

The first step is to implement an interface:

```php
use STS\Metrics\Contracts\ShouldReportMetric;

class OrderReceived implements ShouldReportMetric {
```

This will tell the global event listener to send a metric for this event.
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:

```php
use STS\Metrics\Contracts\ShouldReportMetric;
use STS\Metrics\Traits\ProvidesMetric;
Expand All @@ -138,7 +155,7 @@ In this case, the trait will build a metric called `order_received` (taken from

#### Customizing event metric data

If you decide to use the trait, you likely will want to customize the 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:

Expand All @@ -160,11 +177,12 @@ public function getMetricValue()
}
```

You can provide any of our metric attributes using these class attributes or 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:
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:

```php
use STS\Metrics\Contracts\ShouldReportMetric;
Expand Down
28 changes: 21 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@
}
],
"require": {
"illuminate/support": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0",
"influxdb/influxdb-php": "^1.14"
"php": "^8.0",
"illuminate/support": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": "^7.0",
"mockery/mockery": "^1.0",
"aws/aws-sdk-php": "^3.133"
"phpunit/phpunit": "^10.0",
"orchestra/testbench": "^8.0",
"mockery/mockery": "^1.6",
"aws/aws-sdk-php": "^3.2",
"influxdb/influxdb-php": "^1.15",
"posthog/posthog-php": "^3.0",
"influxdata/influxdb-client-php": "^3.4"
},
"suggest": {
"posthog/posthog-php": "PostHog integration",
"aws/aws-sdk-php": "AWS CloudWatch integration",
"influxdb/influxdb-php": "For integration with InfluxDB 1.7 or earlier",
"influxdata/influxdb-client-php": "For integration with InfluxDB 1.8 or later"
},
"autoload": {
"psr-4": {
Expand All @@ -40,8 +49,13 @@
"STS\\Metrics\\MetricsServiceProvider"
],
"aliases": {
"Metrics": "STS\\Metrics\\MetricsFacade"
"Metrics": "Metrics"
}
}
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
5 changes: 5 additions & 0 deletions config/metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'namespace' => env('CLOUDWATCH_NAMESPACE')
],
"posthog" => [
'key' => env('POSTHOG_API_KEY'),
'host' => env('POSTHOG_HOST', 'https://app.posthog.com'),
'distinct_prefix' => env('POSTHOG_DISTINCT_PREFIX', 'user:')
]
],
];
44 changes: 19 additions & 25 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="EventMetrics Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
</report>
</coverage>
<testsuites>
<testsuite name="EventMetrics Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging/>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
Loading

0 comments on commit 01eb0fc

Please sign in to comment.