PHPackages                             jackgleeson/stats-collector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jackgleeson/stats-collector

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jackgleeson/stats-collector
===========================

Lightweight utility to record, combine, retrieve and export statistics and log data across any PHP process

v1.5.0(4mo ago)117.2k↓21.4%1MITPHPPHP ^8.2CI failing

Since Dec 19Pushed 4mo agoCompare

[ Source](https://github.com/jackgleeson/stats-collector)[ Packagist](https://packagist.org/packages/jackgleeson/stats-collector)[ RSS](/packages/jackgleeson-stats-collector/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (26)Used By (0)

Stats Collector
===============

[](#stats-collector)

![GitHub tag](https://camo.githubusercontent.com/b795ee7c10b61b006d6f51419d8bf4d7df70b0ba2bb3f61a0360f43344ff0382/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7461672f6a61636b676c6565736f6e2f73746174732d636f6c6c6563746f722e737667)[![Build Status](https://camo.githubusercontent.com/af6eaa104fb9c6f4ef398da01397570d41659c4db1395d8bf48a140f491e2ad2/68747470733a2f2f7472617669732d63692e6f72672f6a61636b676c6565736f6e2f73746174732d636f6c6c6563746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jackgleeson/stats-collector)[![Coverage Status](https://camo.githubusercontent.com/65433fb5083263242e69e7ed07965d1200813692350bcedd1999188c718623c4/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6a61636b676c6565736f6e2f73746174732d636f6c6c6563746f722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/jackgleeson/stats-collector?branch=master)

Record, combine, retrieve and export custom statistics and log data during the lifecycle of any PHP process.

Once you have recorded some stats, you can create new stats from your data using traditional aggregate functions like average, count and sum. You can export your stats to an output of your choice, e.g. file, log, db, queue or other custom formats. Finally, you can then display and query the exported stats in whatever frontend you wish, e.g. grafana.

### Features

[](#features)

- Wildcard name expansion with regular expression support e.g. `$stats->get("[a-z0-9]?.*");`
- Create stats from stats. Get the data you need in one process e.g. `$stats->add("overall_total", $stats->sum("separate.totals.*"));`
- Clear separation of responsibility across general log output and statistical log output to help you stop polluting your application logs with statistical data.

### To-do

[](#to-do)

- Filter behaviour. `$filteredStatsCollecter = $stats->filter($this->lessThan(50), $this->equalTo(50),...); `
- Import behaviour. Allow Stats Collector to import previously exported data and carry on where it left off.
- Add tests for helpers and improve tests by mocking collaborators
- Add listener behaviour so that stats can be updated by updates to other stats e.g. moving averages

### Credits

[](#credits)

- [github.com/dflydev/dflydev-dot-access-data](https://github.com/dflydev/dflydev-dot-access-data) - dot namesapce utility

### Add Stats Collector to your project

[](#add-stats-collector-to-your-project)

```
$ composer require jackgleeson/stats-collector
```

### Basic Usage: record, increment and retrieve a stat

[](#basic-usage-record-increment-and-retrieve-a-stat)

```
//get an instance of stats collector
$stats = Statistics\Collector\Collector::getInstance();

//add a stat
$stats->add("clicks", 45);

//get a stat
$clicks = $stats->get("clicks");
$clicks; // 45

//increment a stat
$stats->inc("clicks");
$clicks = $stats->get("clicks");
$clicks; // 46
```

### Basic Usage: Custom stats namespace and wildcard operator usage

[](#basic-usage-custom-stats-namespace-and-wildcard-operator-usage)

```
$stats = Statistics\Collector\Collector::getInstance();

//add a custom namespace and add some stats to it
$stats->ns("crons.payments")
  ->add("total", 30)
  ->add("succeeded", 20)
  ->add("failed", 10);

// get payment cron stats using wildcard path
$paymentStats = $stats->getWithKey("crons.payments.*");

// $paymentStats contents
Array
(
    [crons.payments.failed] => 10
    [crons.payments.succeeded] => 20
    [crons.payments.total] => 30
)
```

### Basic Usage: Record the execution time of a process

[](#basic-usage-record-the-execution-time-of-a-process)

```
$stats = Statistics\Collector\Collector::getInstance();

$stats->start("timer");
// some lengthy process...
$stats->end("timer");
// get the exectuion time
$execution_time = $stats->diff('timer');
```

### Basic Usage: Export stats to file

[](#basic-usage-export-stats-to-file)

```
$stats = Statistics\Collector\Collector::getInstance();

//add a custom namespace and add some stats to it
$stats->ns("crons.payments")
  ->add("total", 30)
  ->add("succeeded", 20)
  ->add("failed", 10);

//export recorded stats to a txt file (see output below)
$exporter = new Statistics\Exporter\File("demo","outdir/dir");
$exporter->export($stats);
```

### Basic Usage: Export stats to file (output)

[](#basic-usage-export-stats-to-file-output)

```
$ cd output/dir
$ cat demo.stats
crons.payments.failed=10
crons.payments.succeeded=20
crons.payments.total=30
```

### Aggregate Usage: Add a bunch of stats across different namespaces and sum them

[](#aggregate-usage-add-a-bunch-of-stats-across-different-namespaces-and-sum-them)

```
$stats = Statistics\Collector\Collector::getInstance();

$stats->ns("noahs.ark.passengers")
  ->add("humans", 2)
  ->add("aliens", 0)
  ->add("animal.cats", 3)
  ->add("animal.dogs", 6)
  ->add("animal.birds", 25);

// total number of passengers on noahs ark
$totalPassengers = $stats->sum("noahs.ark.passengers.*"); // 36
$totalAnimals = $stats->sum("*passengers.animal*"); // 34
$totalCatsAndDogs = $stats->sum("*passengers.animal.[c,d]*"); // 9
```

### Aggregate Usage: Create a compound stat and work out its average

[](#aggregate-usage-create-a-compound-stat-and-work-out-its-average)

```
$stats = Statistics\Collector\Collector::getInstance();

$stats->ns("users")
  ->add("heights", 171)
  ->add("heights", [181, 222, 194, 143, 123, 161, 184]);

$averageHeights = $stats->avg('heights'); //172.375
```

Checkout [samples/shorthand-samples.php](https://github.com/jackgleeson/stats-collector/blob/master/samples/shorthand-samples.php) for a complete list of available functionality in action.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#checkout-samplesshorthand-samplesphp-for-a-complete-list-of-available-functionality-in-action)

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance77

Regular maintenance activity

Popularity29

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity86

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 96.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~140 days

Recently: every ~722 days

Total

22

Last Release

127d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/814c0148b13653c4ef03b3c2367da7c5919f202785ff224964c20f6b566603d5?d=identicon)[jackgleeson](/maintainers/jackgleeson)

---

Top Contributors

[![jackgleeson](https://avatars.githubusercontent.com/u/8114138?v=4)](https://github.com/jackgleeson "jackgleeson (131 commits)")[![ejegg](https://avatars.githubusercontent.com/u/1018606?v=4)](https://github.com/ejegg "ejegg (5 commits)")

---

Tags

prometheusreportingstatisticsstatswildcard

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jackgleeson-stats-collector/health.svg)

```
[![Health](https://phpackages.com/badges/jackgleeson-stats-collector/health.svg)](https://phpackages.com/packages/jackgleeson-stats-collector)
```

###  Alternatives

[league/config

Define configuration arrays with strict schemas and access values with dot notation

564302.2M24](/packages/league-config)[consolidation/output-formatters

Format text by applying transformations provided by plug-in formatters.

19669.3M18](/packages/consolidation-output-formatters)[grasmash/expander

Expands internal property references in PHP arrays file.

13960.7M6](/packages/grasmash-expander)[dflydev/dot-access-configuration

Given a deep data structure representing a configuration, access configuration by dot notation.

13414.5M4](/packages/dflydev-dot-access-configuration)[consolidation/filter-via-dot-access-data

This project uses dflydev/dot-access-data to provide simple output filtering for applications built with annotated-command / Robo.

4646.9M13](/packages/consolidation-filter-via-dot-access-data)[dmishh/settings-bundle

Database centric Symfony configuration management. Global and per-user settings supported.

115254.9k1](/packages/dmishh-settings-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
