phlux / statsd
A simple library for recording stats to a StatsD server
Installs: 4 477
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
Requires (Dev)
- codeclimate/php-test-reporter: ^0.4
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2018-10-01 00:33:26 UTC
README
A simple library for recording stats to a StatsD server.
Install
Via Composer
{ "require": { "phlux/statsd": "1.0.*" } }
Usage
Basic Usage
$connection = new Phlux\StatsD\Connection\UdpConnection('127.0.0.1', 8125); $metric = new Phlux\StatsD\Metric\CountMetric('button.clicks', 5); $connection->send($metric);
Connections
UDP Connections
The easiest way to get started is to create a UDP connection to a StatsD server:
$connection = new Phlux\StatsD\Connection\UdpConnection('127.0.0.1', 8125);
TCP Connections
Metrics can also be sent over TCP:
$connection = new Phlux\StatsD\Connection\TcpConnection('127.0.0.1', 8125, 5); // Timeout of 5 seconds
Fake Connections
In the event that you need to disable StatsD metrics you can use a fake connection:
$connection = new Phlux\StatsD\Connection\FakeConnection();
Namespaced Connections
You can also namespace all metrics that are being sent to StatsD:
$connection = new Phlux\StatsD\Connection\NamespacedConnection( new Phlux\StatsD\Connection\UdpConnection('127.0.0.1', 8125), 'phlux' );
Batched Connections
Batching metrics can be useful if you need to take network MTU into consideration:
$connection = new Phlux\StatsD\Connection\BatchedConnection( new Phlux\StatsD\Connection\UdpConnection('127.0.0.1', 8125), Phlux\StatsD\Connection\BatchedConnection::FAST_ETHERNET_MTU );
Metrics
Documentation on metric types can be read here: https://github.com/etsy/statsd/blob/master/docs/metric_types.md
// Counters $counter = new Phlux\StatsD\Metric\CountMetric('button.clicks', 1); // Gauges $gauge = new Phlux\StatsD\Metric\GaugeMetric('users.connected', 12); // Sets $set = new Phlux\StatsD\Metric\SetMetric('users.unique', $userId); // Timers $timer = new Phlux\StatsD\Metric\TimerMetric('response.time', 48); // Sampled Metrics $timer = new Phlux\StatsD\Metric\SampledMetric( new Phlux\StatsD\Metric\TimerMetric('response.time', 48), 0.1 ); // Namespaced Metrics $timer = new Phlux\StatsD\Metric\NamespacedMetric( new Phlux\StatsD\Metric\TimerMetric('response.time', 48), 'phlux' );