{"id":23003720,"url":"https://github.com/stechstudio/laravel-metrics","last_synced_at":"2026-02-20T19:10:26.190Z","repository":{"id":57059378,"uuid":"107796032","full_name":"stechstudio/laravel-metrics","owner":"stechstudio","description":"Easily track metrics from Laravel events and ship to InfluxDB, Prometheus, CloudWatch, or PostHog.","archived":false,"fork":false,"pushed_at":"2025-02-21T01:22:17.000Z","size":398,"stargazers_count":51,"open_issues_count":0,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-28T09:03:09.791Z","etag":null,"topics":["cloudwatch","influxdb","laravel","metrics","posthog","prometheus"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stechstudio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-21T16:24:33.000Z","updated_at":"2025-03-01T05:12:39.000Z","dependencies_parsed_at":"2024-02-07T23:31:17.581Z","dependency_job_id":"79fde29b-0b61-492f-a96e-277264497343","html_url":"https://github.com/stechstudio/laravel-metrics","commit_stats":{"total_commits":81,"total_committers":9,"mean_commits":9.0,"dds":"0.12345679012345678","last_synced_commit":"5cc23e55b26d0350eae204acac3e1bcdfa20737b"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-metrics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-metrics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-metrics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-metrics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stechstudio","download_url":"https://codeload.github.com/stechstudio/laravel-metrics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247150712,"owners_count":20892189,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudwatch","influxdb","laravel","metrics","posthog","prometheus"],"created_at":"2024-12-15T07:15:09.065Z","updated_at":"2026-02-20T19:10:21.157Z","avatar_url":"https://github.com/stechstudio.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Metrics\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/stechstudio/laravel-metrics.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-metrics)\n[![Total Downloads](https://img.shields.io/packagist/dt/stechstudio/laravel-metrics.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-metrics)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n\nThis package makes it incredibly easy to ship app metrics to backends such as PostHog, InfluxDB or CloudWatch.\n\nThere are two major components: a facade that lets you create metrics on your own, and an event listener to\nautomatically send metrics for Laravel events.\n\n## Installation\n\nYou know the drill...\n\n```\ncomposer require stechstudio/laravel-metrics\n```\n\n## Backend configuration\n\n### PostHog\n\n1. Install the PostHog PHP client: `composer require posthog/posthog-php`\n\n2. Add the following to your `.env` file:\n\n```\nMETRICS_BACKEND=posthog\nPOSTHOG_API_KEY=...\n```\n\n### InfluxDB v1.7 and under\n\n1. Install the InfluxDB PHP client: `composer require influxdb/influxdb-php`\n\n2. Add the following to your `.env` file:\n\n```\nMETRICS_BACKEND=influxdb\nIDB_USERNAME=...\nIDB_PASSWORD=...\nIDB_HOST=...\nIDB_DATABASE=...\nIDB_VERSION=1 # Default\n\n# Only if you are not using the default 8086\nIDB_TCP_PORT=...\n\n# If you want to send metrics over UDP instead of TCP\nIDB_UDP_PORT=...\n```\n\n### InfluxDB V1.8 and above\n\n1. Install the InfluxDB PHP client: `composer require influxdata/influxdb-client-php`\n\n2. Add the following to your `.env` file:\n\n3. In order to use UDP with InfluxDB V1.8+ you must follow\nextra [setup steps](https://github.com/influxdata/influxdb-client-php#writing-via-udp)\n\nAdd the following to your `.env` file:\n\n```\nMETRICS_BACKEND=influxdb\nIDB_TOKEN=...\nIDB_DATABASE=... # Use the name of your desired bucket for this value\nIDB_HOST=...\nIDB_ORG=...\nIDB_VERSION=2\n\n# Only if you are not using the default 8086\nIDB_TCP_PORT=...\n\n# If you want to send metrics over UDP instead of TCP\nIDB_UDP_PORT=...\n```\n\n### CloudWatch\n\n1. Install the AWS PHP SDK: `composer require aws/aws-sdk-php`.\n\n2. Add the following to your `.env` file:\n\n```\nMETRICS_BACKEND=cloudwatch\nCLOUDWATCH_NAMESPACE=...\n\nAWS_DEFAULT_REGION=...\nAWS_ACCESS_KEY_ID=...\nAWS_SECRET_ACCESS_KEY=...\n```\n\n### Prometheus\n1. Install the Prometheus PHP client: `composer require promphp/prometheus_client_php`\n2. Configuring the backend to use Prometheus, makes sense only if you have an endpoint to expose them.\n   Its purpose is only to format the registered metrics in a way that Prometheus can scrape them.\n\n```\nMETRICS_BACKEND=prometheus\n```\n\n### NullDriver (for development)\n\nIf you need to disable metrics just set the backend to null:\n\n```\nMETRICS_BACKEND=null\n```\n\nThis `null` driver will simply discard any metrics.\n\n## Sending an individual metric\n\nYou can create metric by using the facade like this:\n\n```php\nMetrics::create('order_placed')\n    -\u003esetValue(1)\n    -\u003esetTags([\n        'source' =\u003e 'email-campaign',\n        'user' =\u003e 54\n    ]);\n```\n\nThe only required attribute is the `name`, everything else is optional.\n\n## Driver mapping\n\nThis is how we are mapping metric attributes in our backends.\n\n| Metric attribute | PostHog           | InfluxDB      | CloudWatch        | Prometheus                                    |\n|------------------|-------------------|---------------|-------------------|-----------------------------------------------|\n| name             | event             | measurement   | MetricName        | name                                          |\n| value            | properties[value] | fields[value] | Value             | value                                         |\n| unit             | _ignored_         | _ignored_     | Unit              | _ignored_                                     |\n| resolution       | _ignored_         | _ignored_     | StorageResolution | _ignored_                                     |\n| tags             | _ignored_         | tags          | Dimensions        | keys -\u003e labelNames\u003cbr/\u003e values -\u003e labelValues |\n| extra            | properties        | fields        | _ignored_         | _ignored_                                     |\n| timestamp        | _ignored_         | timestamp     | Timestamp         | _ignored_                                     |\n| description      | _ignored_         | _ignored_     | _ignored_         | help                                          |\n| namespace        | _ignored_         | _ignored_     | _ignored_         | namespace                                     |\n| type             | _ignored_         | _ignored_     | _ignored_         | used to register counter or gauge metric      |\n\nSee the [CloudWatch docs](http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html)\nand [InfluxDB docs](https://docs.influxdata.com/influxdb/latest/concepts/key_concepts/) for more information on their\nrespective data formats. Note we only do minimal validation, you are expected to know what data types and formats your\nbackend supports for a given metric attribute.\n\n## Sending metrics from Laravel events\n\nThe main motivation for this library was to send metrics automatically when certain events occur in a Laravel\napplication. So this is where things really get fun!\n\nLet's say you have a simple Laravel event called OrderReceived:\n\n```php\nclass OrderReceived {\n    protected $order;\n    \n    public function __construct($order)\n    {\n        $this-\u003eorder = $order;\n    }\n}\n```\n\nThe first step is to implement an interface:\n\n```php\nuse STS\\Metrics\\Contracts\\ShouldReportMetric;\n\nclass OrderReceived implements ShouldReportMetric {\n```\n\nThis will tell the global event listener to send a metric for this event.\n\nThere are two different ways you can then provide the metric details.\n\n### 1. Use the `ProvidesMetric` trait\n\nYou can also include a trait that helps with building this metric:\n\n```php\nuse STS\\Metrics\\Contracts\\ShouldReportMetric;\nuse STS\\Metrics\\Traits\\ProvidesMetric;\n\nclass OrderReceived implements ShouldReportMetric {\n    use ProvidesMetric;\n```\n\nIn this case, the trait will build a metric called `order_received` (taken from the class name) with a value of `1`.\n\n#### Customizing event metric data\n\nIf you decide to use the trait, you likely will want to customize the event metric data.\n\nYou can provide metric data with class attributes:\n\n```php\nclass OrderReceived implements ShouldReportMetric {\n    use ProvidesMetric;\n    \n    protected $metricName = \"new_order\";\n    protected $metricTags = [\"category\" =\u003e \"revenue\"];\n    ...\n```\n\nOr if some of your metric data is dynamic you can use getter methods:\n\n```php\npublic function getMetricValue()\n{\n    return $this-\u003eorder-\u003etotal;\n}\n```\n\nYou can provide any of our metric attributes using these class attributes or getter methods.\n\n### 2. Create the metric yourself\n\nDepending on how much detail you need to provide for your metric, it may be simpler to just build it yourself. In this\ncase you can ditch the trait and simply provide a public `createMetric` function that returns a new `Metric` instance:\n\n```php\nuse STS\\Metrics\\Contracts\\ShouldReportMetric;\nuse STS\\Metrics\\Metric;\n\nclass OrderReceived implements ShouldReportMetric {\n    protected $order;\n    \n    public function __construct($order)\n    {\n        $this-\u003eorder = $order;\n    }\n    \n    public function createMetric()\n    {\n        return (new Metric('order_received'))\n            -\u003esetValue(...)\n            -\u003esetTags([...])\n            -\u003esetTimestamp(...)\n            -\u003esetResolutions(...);\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstechstudio%2Flaravel-metrics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstechstudio%2Flaravel-metrics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstechstudio%2Flaravel-metrics/lists"}