Standalone DataDog PHP Client, works without datadog agent
Lightweight php-only datadog client
This is a simple php client for datadog which does not require setting up the DataDog Agent This library supports sending metric data or events at the moment, as those should be the most common use cases. Feel free to extend it to your own needs or sending pull requests.
This library can be installed using composer
composer require matthiasbayer/datadog-client
To start sending requests to Datadog you need to provider your personal API key first. You find that key in Datadog API Settings.
$client = new Client('mysecretapikey');
Events can be sent directly to Datadog using the provided Event class:
$myEvent = new Event('This is my test event', 'Optional event title'); $client->sendEvent($myEvent);
Additional properties can be set the usual way:
There is also a shortcut method which handles all that stuff for you:
// Create and send event in one call $client->event('My test event', 'Optional event title', array( 'alert_type' => Event::TYPE_ERROR ));
Timestamp of the event. Defaults to current timestamp.priority
Event priority. Datadog supports LOW and NORMALalertType
Event alert_type. Datadog supports INFO, WARNING, ERROR and SUCCESSaggregationKey
Arbitary string used to group events
Datadog requires metric data to be encapsulated into a series. One series contains one or more metric objects, which itself contains one or more measurement points.
// Create Series $mySeries = new Series(); // Create a new metric with multiple points $firstMetric = new Metric('my.metric.name', array( array(20), // Dummy points array(13456789, 30), // Point with timestamp set array(40), // If not set, timestamp default to current time )); // Create a new metric with one point $secondMetric = new Metric('my.second.metric', array(20)); $mySeries->addMetrics(array( $firstMetric, $secondMetric )); // Send data $client->sendSeries($mySeries);
If you want to send just one metric at a time, you can use the Client::sendMetric method:
Again, there is a shortcut method which can handle that for you:
// Create a new metric with multiple points $myMetric = new Metric('my.metric.name', array( array(20), // Dummy points array(13456789, 30), // Point with timestamp set array(40), // If not set, timestamp default to current time )); $client->sendMetric($myMetric);
$client->metric('my.test.metric', array( array(20), array(13456789, 30), array(40), ));
Name of the metric. Can contain underscored or dots for easier grouping, e.g. web.exception.404type
Type of the metric. Datadog supports gauge or counterhost
Hostname of the source machinepoints
Array of data points.
A point consists of an optional timestamp and a numeric value. If no timestamp is specified, the current timestamp will be used. Order matters. If a timestamp is specified, it should be the first value.
// Timestamp will be set to time() $simplePoint = array(20); // Set timestamp to explicit value $pointWithTimestamp = array(1234567, 20);