You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://packagist.org/packages/cosmastech/statsd-client-adapter)[](https://packagist.org/packages/cosmastech/statsd-client-adapter)[](https://packagist.org/packages/cosmastech/statsd-client-adapter)[](https://packagist.org/packages/cosmastech/statsd-client-adapter)
2
+
3
+
2
4
# StatsD Client Adapter
3
5
This package was originally designed to solve the problem of:
4
6
* I use DataDog on production, but
5
7
* I don't want to push stats to DataDog on my dev or test environments
6
8
7
-
Where might I want to push those precious stats? Maybe to a log? Maybe to a locally running [StatsD server](https://github.com/statsd/statsd)? 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?
9
+
Where might I want to push those precious stats? Maybe to a log? Maybe to a locally running [StatsD server](https://github.com/statsd/statsd)?
10
+
What if in my unit tests, I want to confirm that logs are being pushed, but not go through the hassle of an integration
11
+
test set up that configures the StatsD server?
8
12
9
13
While [PHP League's statsd package](https://github.com/thephpleague/statsd) is great, it doesn't allow for sending DataDog specific stats
10
14
(such as [histogram](https://docs.datadoghq.com/metrics/types/?tab=histogram) or [distribution](https://docs.datadoghq.com/metrics/types/?tab=distribution)).
11
15
Nor does the DataDog client allow for pushing to another StatsD implementation easily.
12
16
13
17
The aim here is to allow for a single interface that can wrap around both, and be easily extended for different implementations.
14
18
19
+
20
+
## Adapters
21
+
22
+
### InMemoryClientAdapter
23
+
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.
24
+
25
+
See [examples/in_memory.php](examples/in_memory.php) for how you might implement this.
26
+
27
+
### DataDogStatsDClientAdapter
28
+
This is a wrapper around DataDog's [php-datadogstatsd](https://github.com/dataDog/php-datadogstatsd/) client.
29
+
30
+
If you wish to use this adapter, please make sure you install the php-datadogstatsd client.
31
+
32
+
```shell
33
+
composer require datadog/php-datadogstatsd
34
+
```
35
+
36
+
For specifics on their configuration, see the [official DogStatsD documentation](https://docs.datadoghq.com/developers/dogstatsd/?code-lang=php&tab=hostagent#client-instantiation-parameters).
37
+
38
+
See [examples/datadog.php](examples/datadog.php) for how you might implement this.
39
+
40
+
### DatadogLoggingClient
41
+
Envisioned as a client for local development, this adapter writes to a class which implements the [psr-logger interface](https://packagist.org/packages/psr/log).
42
+
You can find a [list](https://packagist.org/providers/psr/log-implementation) of packages that implement the interface on packagist.
43
+
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](https://github.com/Seldaek/monolog).
44
+
45
+
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.
46
+
47
+
See [examples/log_datadog.php](examples/log_datadog.php) for how you might implement this.
48
+
49
+
### LeagueStatsDClientAdapter
50
+
You can also write to an arbitrary statsd server by leveraging [PHP League's statsd package](https://github.com/thephpleague/statsd).
51
+
52
+
First ensure that the package has been installed.
53
+
```shell
54
+
composer require league/statsd
55
+
```
56
+
57
+
For information on how to configure Client, [read their documentation](https://github.com/thephpleague/statsd?tab=readme-ov-file#configuring).
58
+
59
+
**Note** the `histogram()` and `distribution()` methods are both no-op by default, as they are not available on statsd.
60
+
61
+
See [examples/league.php](examples/league.php) for how you might implement this.
62
+
15
63
## Gotchas
16
-
1. Only increment/decrement on PHPLeague'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.
17
-
2. There are `histogram()` and `distribution()` methods on `LeagueStatsDClientAdapter`, but they only raise a PHP error and are no-op.
64
+
1. 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.
65
+
2. There are `histogram()` and `distribution()` methods on `LeagueStatsDClientAdapter`, but they will not be sent to statsd.
0 commit comments