PHPackages                             michaelesmith/metric-data - 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. michaelesmith/metric-data

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

michaelesmith/metric-data
=========================

abstract metric data storage system

0.1(11y ago)07MITPHPPHP &gt;=5.4

Since Aug 29Pushed 7y ago1 watchersCompare

[ Source](https://github.com/michaelesmith/metric-data)[ Packagist](https://packagist.org/packages/michaelesmith/metric-data)[ Docs](https://github.com/michaelesmith/EventData)[ RSS](/packages/michaelesmith-metric-data/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Build Status](https://camo.githubusercontent.com/6b2a1c3655e3d0c00d26155d98ab21513c7bc3e5d2aad5922198798f586d01e6/68747470733a2f2f7472617669732d63692e6f72672f6d69636861656c65736d6974682f6d65747269632d646174612e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/michaelesmith/metric-data)

What is it?
===========

[](#what-is-it)

This is a library that allows for easy abstraction of metric data collection and storage, allowing it to be stored locally or via external API.

Installation
============

[](#installation)

`composer require "michaelesmith/metric-data"`

All About Metrics
=================

[](#all-about-metrics)

For this library all data points are referred to as metrics and used via `MetricData\Metric\MetricInterface` which defines 3 required methods `getDateTime()`, `getType()` and `getData()`. You can implement the interface to define your own metrics based on your specific business logic but a few basic implementations are provided in:

- `MetricData\Metric\Metric`: Data can be any type
- `MetricData\Metric\IntegerMetric`: Data is typed to an integer
- `MetricData\Metric\FloatMetric`: Data is typed to an float
- `MetricData\Metric\ArrayMetric`: Data is an array

Basic Storage Examples
======================

[](#basic-storage-examples)

This section will walk through examples for each of the storage types. In these examples when a child sotrage is needed a `NullStorage` is generally used for illustration.

Closure
-------

[](#closure)

This storage allows you to quickly integrate virtually any backend via a closure. It is till recommend in most case to create a full blown extension class once you prototype the system and are ready for production.

```
use MetricData\Metric\Metric;
use MetricData\Storage\ClosureStorage;

$storage = new ClosureStorage(
    function (MetricInterface $metric) use ($someAPI){
        $someAPI->send($metric->getType(), $metric->getData());
    }
);

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);
```

Delayed
-------

[](#delayed)

This storage allows you to queue up metric data during a request and the flush them to another storage, which will allow you to delay the possibly long processing time until after the response has been sent to the user.

```
use MetricData\Metric\Metric;
use MetricData\Storage\DelayedStorage;
use MetricData\Storage\NullStorage;

$storage = new DelayedStorage(new NullStorage());

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);
$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        5
    )
);
$storage->store(
    new Metric(
        new \DateTime(),
        'another_collection_type',
        1
    )
);

$storage->flush();
```

File
----

[](#file)

This storage will store the metrics in a JOSN encoded file. This can be usefully for testing or if you want to process the events in mass with some other tool out of band of the user request / response cycle.

```
use MetricData\Metric\Metric;
use MetricData\Storage\FileStorage;

$storage = new FileStorage('var/metrics.json');

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);
```

KeenIO
------

[](#keenio)

This storage allows you to send metric data to the [KeenIO API](https://keen.io).

```
use KeenIO\Client\KeenIOClient;
use MetricData\Metric\Metric;
use MetricData\Storage\KeenIOStorage;

$client = KeenIOClient::factory([
    'projectId' => $projectId,
    'writeKey'  => $writeKey,
]);

$storage = new KeenIOStorage($client);

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);
```

Logging
-------

[](#logging)

This storage will log to a PSR-3 logger before passing the metric to a child storage.

```
use MetricData\Metric\Metric;
use MetricData\Storage\LoggingStorage;
use MetricData\Storage\NullStorage;

$logger = // Some PSR-3 compatible logger sucha as [monolog](https://github.com/Seldaek/monolog)

$storage = new LoggingStorage($logger, 'info', new NullStorage());

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);
```

Memory
------

[](#memory)

This storage is not particularly useful in production but is very fast when used in a testing environment while still allowing for the storing of events to be tested.

```
use MetricData\Metric\Metric;
use MetricData\Storage\MemoryStorage;

$storage = new MemoryStorage();

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);

$storage->getMetrics(); // retrieves all metrics stored
$storage->count(); // gets the number of metrics stored in this request
$storage->first(); // gets only the first metric stored
$storage->last(); // gets only the last metrics stored
```

Multiple
--------

[](#multiple)

This storage allows metrics to be sent to multiple child storages.

```
use MetricData\Metric\Metric;
use MetricData\Storage\MulitpleStorage;
use MetricData\Storage\NullStorage;

$storage = new MulitpleStorage([
    new NullStorage(),
    new NullStorage(),
    new NullStorage(),
]);

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);
```

Null
----

[](#null)

This storage is the equivalent of `/dev/null`, any metric stored here will simple disappear into the ether. Not useful for production but can be used in scenarios where you don't need to store metrics like unit tests.

```
use MetricData\Metric\Metric;
use MetricData\Storage\NullStorage;

$storage = new NullStorage();

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);
```

Advanced Storage Examples
=========================

[](#advanced-storage-examples)

While these storages can be used individually, their power comes from the ability to combine them to build powerful functionality specific to the needs of different application environments but which can easily interchange allowing the application to be unaware of the particular configuration.

Development
-----------

[](#development)

```
use MetricData\Metric\Metric;
use MetricData\Storage\FileStorage;
use MetricData\Storage\LoggingStorage;
use MetricData\Storage\MemoryStorage;
use MetricData\Storage\MultipleStorage;

$storage = new LoggingStorage($logger, 'info', // will log all metrics to the application log
    new MultipleStorage([
       new FileStorage('var/metrics.json'), // provides a record of each metric stored
       $memoryStorage = new MemoryStorage(), // can be retrieved by profiling tools like the Symfony toolbar
   ])
);

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);
```

Testing
-------

[](#testing)

```
use MetricData\Metric\Metric;
use MetricData\Storage\NullStorage;

$storage = new NullStorage();

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);
```

Production
----------

[](#production)

```
use KeenIO\Client\KeenIOClient;
use MetricData\Metric\Metric;
use MetricData\Storage\KeenIOStorage;
use MetricData\Storage\LoggingStorage;

$client = KeenIOClient::factory([
    'projectId' => $projectId,
    'writeKey'  => $writeKey,
]);

$storage = new LoggingStorage($logger, 'info', // will log all metrics to the application log
    $delayedStorage = new DelayedStorage( // allow the user response to be sent before performing the expensive API calls
        new KeenIOStorage($client) // production metrics will be sent to an external api
    )
);

$storage->store(
    new Metric(
        new \DateTime(),
        'metric_collection_type',
        7
    )
);

// send user response

if (isset($delayedStorage)) {
    $delayedStorage->flush();
}
```

Contributing
============

[](#contributing)

Have an idea to make something better? Submit a pull request. Need integration of some other backend service? Build it. I would be happy to add a link here. PR's make the open source world turn. 🌎 🌏 🌍 ![:octocat:](https://github.githubassets.com/images/icons/emoji/octocat.png ":octocat:") Happy Coding!

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

4272d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/02cd7a5417015ea4b37ad2b111bde525384de978cf5b2bbf9ef9080ac90342e5?d=identicon)[michaelesmith](/maintainers/michaelesmith)

---

Top Contributors

[![michaelesmith](https://avatars.githubusercontent.com/u/181546?v=4)](https://github.com/michaelesmith "michaelesmith (15 commits)")

---

Tags

eventevent data

### Embed Badge

![Health badge](/badges/michaelesmith-metric-data/health.svg)

```
[![Health](https://phpackages.com/badges/michaelesmith-metric-data/health.svg)](https://phpackages.com/packages/michaelesmith-metric-data)
```

###  Alternatives

[doctrine/event-manager

The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.

6.1k501.1M115](/packages/doctrine-event-manager)[league/event

Event package

1.6k141.6M184](/packages/league-event)[laminas/laminas-eventmanager

Trigger and listen to events within a PHP application

1.0k69.8M225](/packages/laminas-laminas-eventmanager)[winzou/state-machine

A very lightweight yet powerful PHP state machine

52113.7M18](/packages/winzou-state-machine)[spatie/laravel-event-sourcing

The easiest way to get started with event sourcing in Laravel

9003.7M26](/packages/spatie-laravel-event-sourcing)[spatie/laravel-google-calendar

Manage events on a Google Calendar

1.4k1.5M21](/packages/spatie-laravel-google-calendar)

PHPackages © 2026

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