vend/statsd

This package is abandoned and no longer maintained. No replacement package was suggested.

Simple extensible StatsD client library for PHP 5.5+

v1.1.1 2016-02-17 03:27 UTC

README

Simple extensible StatsD client for PHP 5.5+

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Latest Unstable Version

Features

  • Correctly splits large numbers of metrics across multiple datagrams
  • Can substitute out the underlying format of the metrics being sent, to support things like Datadog's tags and histograms
  • Supports sample rates, but leaves skipping the send to the caller (use the factory and socket directly)
  • Doesn't block during metric sending, and only supports UDP (opinionated, but that's all you should need)

Usage

This library uses dependency injection throughout. To instantiate a Client, you'll need a Socket and a Factory:

use Vend\Statsd\Client;
use Vend\Statsd\Socket;
use Vend\Statsd\Factory;

$client = new Client(
    new Socket(),
    new Factory()
);

Once you have a client, you can use the familiar statsd methods to enqueue metrics on the client. The client doesn't send them until ->flush() is called.

$client->increment('some.metric_key'); // incremented by 1
$client->decrement('some.metric_key'); // decremented by 1
$client->counter('some.counter', 3);   // incremented by 3
$client->gauge('some.gauge', 10);
$client->timer('some.timer', 0.25);
$client->set('some.set', 'some_value');

$client->flush(); // actually sends the metrics

These methods return the MetricInterface produced by the factory (so you can then attach extra information).

You can also use the factory and socket to directly and immediately send metrics. The ->getData() method on MetricInterface provides the serialized string that should be sent to the statsd server.

$socket = new Socket('127.0.0.1', 8125);
$factory = new Factory();

$socket->open();
$socket->write($factory->increment('some.key')->getData());
$socket->close();

Extending

For an example of how to extend the library, see the Vend\Statsd\Datadog namespace. A different Factory is used by passing it to the Client. The Datadog-specific factory will allow the metrics to carry tag information.

NullClient, ClientAwareInterface and ClientAwareTrait

Helpful utility classes for performing optional (setter) injection of statsd clients into your classes. Use them in pretty much the same way as their PSR3 equivalents.