, (*1)
Metrics Collector
Overview
The Metrics Collector is a simple library that provides base classes to easily extend and collect custom metrics., (*2)
Usage
This library does not provide any metrics by default. To create new metrics, create a Composer package and then add a dependency to this package:, (*3)
composer require edisonlabs/metrics
Now create the metrics classes extending edisonlabs/metrics
classes., (*4)
Example: Number of PHP files., (*5)
// src/EdisonLabs/Metric/NumberOfPhpFiles.php
namespace EdisonLabs\Metric;
use EdisonLabs\Metrics\Metric\AbstractMetricBase;
class NumberOfPhpFiles extends AbstractMetricBase
{
public function getName()
{
return 'Number of PHP files';
}
public function getDescription()
{
return 'The total number of PHP files';
}
public function getMetric()
{
// Put the logic to calculate the total of PHP files here.
// ..
// Random example.
return rand(10, 50);
}
}
Configure the autoload in composer.json
:, (*6)
"autoload": {
"psr-4": {
"EdisonLabs\\Metric\\": "src/EdisonLabs/Metric"
}
}
Re-create the Composer autoloader:, (*7)
composer dump-autoload
Collecting metrics
There are two ways to collect the metrics: programmatically and by command-line., (*8)
Programmatically
// collector.php
use EdisonLabs\Metrics\Collector;
$date = strtotime('now');
$config = array();
$collector = new Collector($date, $config);
$metrics = $collector->getMetrics();
Command
The command is located at vendor/bin/metrics
. Include the vendor/bin
directory in the system $PATH
to run this command from anywhere., (*9)
Type metrics --help
to see all the available options., (*10)
Saving metrics
Create datastore classes to save your metrics:, (*11)
// src/EdisonLabs/Metric/Datastore/SqLite.php
namespace EdisonLabs\Metric\Datastore;
use EdisonLabs\Metrics\Metric\Datastore\AbstractMetricDatastore;
class SqLite extends AbstractMetricDatastore
{
public function getName()
{
return 'SQLite';
}
public function getDescription()
{
return 'Stores metrics to SQLite';
}
public function save()
{
$metrics = $this->getMetrics();
// Put your logic to store the metrics to SQLite here.
return true;
}
}
Programmatically
// datastore.php
use EdisonLabs\Metrics\Collector;
use EdisonLabs\Metrics\DatastoreHandler;
$date = strtotime('now');
$config = array();
$collector = new Collector($date, $config);
$metrics = $collector->getMetrics();
$datastoreHandler = new DatastoreHandler($date, $config);
$datastore = $datastoreHandler->getDatastoreByName('SQLite');
$datastore->setMetrics($metrics);
$datastore->save();
Command
Use the option --save
to list and save your metrics., (*12)
metrics --save=SQLite
The --save
option accepts multiple values., (*13)
metrics --save=SQLite,MySql,MyCustomDatastore