Datadogstatsd::event PHP Method

event() public static method

Requires PHP >= 5.3.0
public static event ( string $title, array $vals = [] ) : null
$title string Title of the event
$vals array Optional values of the event. See http://docs.datadoghq.com/guides/dogstatsd/#events for the valid keys
return null
    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;
    }

Usage Example

Ejemplo n.º 1
0
function runFunction()
{
    global $apiKey;
    global $appKey;
    $startTime = microtime(true);
    $testArray = array();
    for ($i = 0; $i < rand(1, 1000000000); $i++) {
        $testArray[$i] = $i;
        // Simulate an event at every 1000000th element
        if ($i % 1000000 == 0) {
            echo "Event simulated.\n";
            Datadogstatsd::event('A thing broke!', array('alert_type' => 'error', 'aggregation_key' => 'test_aggr'));
            Datadogstatsd::event('Now it is fixed.', array('alert_type' => 'success', 'aggregation_key' => 'test_aggr'));
        }
    }
    unset($testArray);
    Datadogstatsd::timing('test.data.point', microtime(true) - $startTime, 1, array('tagname' => 'php_example_tag_2'));
}