cosmastech / statsd-client-adapter
A StatsD client adapter for use with DataDog or PHPLeague's statsd clients
Requires (Dev)
- cosmastech/psr-logger-spy: ^0.0.2
- datadog/php-datadogstatsd: ^1.6.1
- friendsofphp/php-cs-fixer: ^3.59
- league/statsd: ^2.0.0
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^11.2.5
Suggests
- datadog/php-datadogstatsd: For DataDog stats
- league/statsd: For generic statsd clients
- monolog/monolog: For psr log implementation
- nesbot/carbon: For using Carbon as psr/clock-implementation
Provides
- psr/clock-implementation: 3.0.0
This package is auto-updated.
Last update: 2024-11-21 14:27:53 UTC
README
StatsD Client Adapter
This package was originally designed to solve the problem of:
- I use DataDog on production, but
- I don't want to push stats to DataDog on my dev or test environments
Where might I want to push those precious stats? Maybe to a log? Maybe to a locally running StatsD server? What if in my unit tests, I want to confirm that logs are being pushed, but not go through the hassle of an integration test set up that configures the StatsD server?
While PHP League's statsd package is great, it doesn't allow for sending DataDog specific stats (such as histogram or distribution). Nor does the DataDog client allow for pushing to another StatsD implementation easily.
The aim here is to allow for a single interface that can wrap around both, and be easily extended for different implementations.
If you would like to use this library in Laravel, check out laravel-statsd-adapter.
Adapters
InMemoryClientAdapter
This adapter simply records your stats in an object in memory. This is best served as a way to verify stats are recorded in your unit tests.
See examples/in_memory.php for how you might implement this.
DataDogStatsDClientAdapter
This is a wrapper around DataDog's php-datadogstatsd client.
If you wish to use this adapter, please make sure you install the php-datadogstatsd client.
composer require datadog/php-datadogstatsd
For specifics on their configuration, see the official DogStatsD documentation.
See examples/datadog.php for how you might implement this.
DatadogLoggingClient
Envisioned as a client for local development, this adapter writes to a class which implements the psr-logger interface. You can find a list of packages that implement the interface on packagist. If you are using a framework like Symfony or Laravel, then you already have one of the most popular and reliable implementations installed: monolog/monolog.
For a local development setup, you could just write the stats to a log. This writes the format exactly as it would be sent to DataDog.
See examples/log_datadog.php for how you might implement this.
LeagueStatsDClientAdapter
You can also write to an arbitrary statsd server by leveraging PHP League's statsd package.
First ensure that the package has been installed.
composer require league/statsd
For information on how to configure Client, read their documentation.
Note the histogram()
and distribution()
methods are both no-op by default, as they are not available on statsd.
See examples/league.php for how you might implement this.
Gotchas
- Only increment/decrement on DataDog's implementation allow for including the sample rate. If you are using a sample rate with other calls, their sample rate will not be included as part of the stat.
- There are
histogram()
anddistribution()
methods onLeagueStatsDClientAdapter
, but they will not be sent to statsd.
Testing
composer test