PHP Graphite feeder

Graphite Data Feeder is a library that collects and aggregates metric values and then sends batches of data directly to the Graphite data port.

The library acts like a daemon witch collects the data points and aggregates them. After the raw data was received and aggregated, the results are sent to the graphite data port (carbon relay daemon). The carbon relay will not alter the data points because they were already aggregated by the same retention settings.
graphite aggregator

How to install PHP Graphite feeder

composer require gavrila/graphite-feeder

 

Basic usage

Get the Carbon retention settings for your metric (Read more about Carbon storage schema here). Retentions are set in the file graphite/storage-schemas.conf and should look like this:

[default]
pattern = .*
retentions = 10s:7d
[process_name_test]
pattern = test.stats.process_name.elapsed_time
retentions = 15s:7d,1m:21d,15m:5y

Create a GraphiteFeeder client and then add data to its buffer.

Buffers will be created automatically according the retention settings. When you add metrics, the library will send the data to the corresponding buffer.

After all data was added , it can be aggregated and sent to the graphite port.

You can make intermediary flushes if you are sure that all data from a custom period was added to the client.

use  Gavrila\GraphiteFeeder;
...
$retentions = '15s:7d,1m:21d,15m:5y';

$connector = new GraphiteFeeder\Connector\Fsock(
    'graphite.host',
    '2003',
    'tcp'
);

$client = new GraphiteFeeder\Client($connector, $retentions);

$data = [
    new GraphiteFeeder\Entity\Data(
        'test.stats.process_name.elapsed_time',
        10,
        time()    
    ),
    new GraphiteFeeder\Entity\Data(
        'test.stats.process_name.elapsed_time',
        2.5,
        time()    
    ),
];

foreach ($data as $item) {
    $client->dataBuffer->add($item);
}
        
$written = $client->flushAllData();