public static function event($title, $vals = array())
{
// Assemble the request
$vals['title'] = $title;
// If sending events via UDP
if (self::$__submitEventsOver === 'UDP') {
return self::eventUdp($vals);
}
// Convert a comma-separated string of tags into an array
if (array_key_exists('tags', $vals) && is_string($vals['tags'])) {
$tags = explode(',', $vals['tags']);
$vals['tags'] = array();
foreach ($tags as $tag) {
$vals['tags'][] = trim($tag);
}
}
/**
* @var boolean Flag for returning success
*/
$success = true;
// Get the url to POST to
$url = self::$__datadogHost . self::$__eventUrl . '?api_key=' . self::$__apiKey . '&application_key=' . self::$__applicationKey;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, self::$__apiCurlSslVerifyPeer);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, self::$__apiCurlSslVerifyHost);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($vals));
// Nab response and HTTP code
$response_body = curl_exec($curl);
$response_code = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
try {
// Check for cURL errors
if ($curlErrorNum = curl_errno($curl)) {
throw new Exception('Datadog event API call cURL issue #' . $curlErrorNum . ' - ' . curl_error($curl));
}
// Check response code is 202
if ($response_code !== 200 && $response_code !== 202) {
throw new Exception('Datadog event API call HTTP response not OK - ' . $response_code . '; response body: ' . $response_body);
}
// Check for empty response body
if (!$response_body) {
throw new Exception('Datadog event API call did not return a body');
}
// Decode JSON response
if (!($decodedJson = json_decode($response_body, true))) {
throw new Exception('Datadog event API call did not return a body that could be decoded via json_decode');
}
// Check JSON decoded "status" is OK from the Datadog API
if ($decodedJson['status'] !== 'ok') {
throw new Exception('Datadog event API response status not "ok"; response body: ' . $response_body);
}
} catch (Exception $e) {
$success = false;
// Use error_log for API submission errors to avoid warnings/etc.
error_log($e->getMessage());
}
curl_close($curl);
return $success;
}