This is an extremely simple PHP datadogstatsd client. Requires PHP >= 5.3.0.
See CHANGELOG.md for changes.
Add the following to your composer.json:
"datadog/php-datadogstatsd": "1.0.*"
Note: The first version shipped in composer is 0.0.3
Clone repository at github.com/DataDog/php-datadogstatsd
Setup: require './src/DogStatsd.php';
To instantiate a DogStatsd object using composer:
require __DIR__ . '/vendor/autoload.php';
use DataDog\DogStatsd;
use DataDog\BatchedDogStatsd;
$statsd = new DogStatsd();
$statsd = new BatchedDogStatsd();DogStatsd constructor, takes a configuration array. The configuration can take any of the following values (all optional):
host: the host of your DogStatsd server, default tolocalhostport: the port of your DogStatsd server. default to8125
When sending events over TCP the following options can be set (see Events section):
api_key: needed to sendeventover TCPapp_key: needed to sendeventover TCPcurl_ssl_verify_host: Config pass-through forCURLOPT_SSL_VERIFYHOSTdefaults 2curl_ssl_verify_peer: Config pass-through forCURLOPT_SSL_VERIFYPEERdefault 1datadog_host: where to send events defaulthttps://app.datadoghq.com
The 'tags' argument can be a array or a string. Value can be set to null.
# Both call will send the "app:php1" and "beta" tags.
$statd->increment('your.data.point', 1, array('app' => 'php1', 'beta' => null));
$statd->increment('your.data.point', 1, "app:php1,beta");To increment things:
$statd->increment('your.data.point');
$statd->increment('your.data.point', .5);
$statd->increment('your.data.point', 1, array('tagname' => 'value'));To decrement things:
$statd->decrement('your.data.point');To time things:
$start_time = microtime(true);
run_function();
$statd->timing('your.data.point', microtime(true) - $start_time);
$statd->timing('your.data.point', microtime(true) - $start_time, 1, array('tagname' => 'value'));For documentation on the values of events, see http://docs.datadoghq.com/api/#events.
Submitting events via TCP vs UDP
- TCP - High-confidence event submission. Will log errors on event submission error.
- UDP - "Fire and forget" event submission. Will not log errors on event submission error. No acknowledgement of submitted event from Datadog.
No matter wich transport is used the event function has the same API.
Since the UDP method uses the a local dogstatsd instance we don't need to setup any additional application/api access.
$statsd = new DogStatsd();
$statd->event('Fire and forget!',
array(
'text' => 'Sending errors via UDP is faster but less reliable!',
'alert_type' => 'success'
)
);- Default method
- No configuration
- Faster
- Less reliable
- No logging on communication errors with Datadog (fire and forget)
To submit events via TCP, you'll need to first configure the library with your Datadog credentials, since the event function submits directly to Datadog instead of sending to a local dogstatsd instance.
You can find your api and app keys in the API tab.
$statsd = new DogStatsd(
array('api_key' => 'myApiKey',
'app_key' => 'myAppKey',
)
);
$statd->event('A thing broke!',
array(
'alert_type' => 'error',
'aggregation_key' => 'test_aggr'
)
);
$statd->event('Now it is fixed.',
array(
'alert_type' => 'success',
'aggregation_key' => 'test_aggr'
)
);- Slower
- More reliable
- Logging on communication errors with Datadog (uses cURL for API request)
- Logs via error_log and try/catch block to not throw warnings/errors on communication issues with API
- Add a configurable timeout for event submission via TCP
- Write unit tests
- Document service check functionality