vend / statsd
Simple extensible StatsD client library for PHP 5.5+
Installs: 279 054
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 8
Forks: 1
Open Issues: 0
Requires
- php: >=5.5.0
- psr/log: ~1.0
Requires (Dev)
- phpunit/phpunit: ^4.8
This package is not auto-updated.
Last update: 2023-08-28 23:26:17 UTC
README
Simple extensible StatsD client for PHP 5.5+
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.