Envoyer des événements avec DogStatsD
Rapport de recherche Datadog : Bilan sur l'adoption de l'informatique sans serveur Rapport : Bilan sur l'adoption de l'informatique sans serveur

Envoyer des événements avec DogStatsD

Envoi

Une fois DogStatsD installé, vous pouvez envoyer des événements à votre flux d’événements Datadog via la fonction suivante :

event(<TITRE>, <TEXTE>, <TIMESTAMP>, <HOSTNAME>, <CLÉ_AGRÉGATION>, <PRIORITÉ>, <NOM_TYPE_SOURCE>, <TYPE_ALERTE>, <TAGS>)

Définitions :

ParamètreTypeObligatoireDescription
<TITRE>ChaîneOuiLe titre de l’événement
<TEXTE>ChaîneOuiLe corps de texte de l’événement
<TIMESTAMP>Nombre entierOuiLe timestamp de l’événement selon l’epoch Unix (par défaut, défini sur l’heure actuelle du serveur DogStatsD)
<HOSTNAME>ChaîneNonLe nom du host
<CLÉ_AGGRÉGATION>ChaîneNonLa clé à utiliser pour agréger les événements
<PRIORITÉ>ChaîneNonIndique la priorité de l’événement (normal ou low).
<NOM_TYPE_SOURCE>ChaîneNonLe nom du type de source
<TYPE_ALERTE>ChaîneNonerror, warning, success ou info (valeur par défaut : info)
<TAGS>Liste de chaînesNonLa liste des tags associés à cet événement.

Exemples

Visualisez les erreurs et les exceptions dans Datadog avec un événement DogStatsD :

event.py

from datadog import initialize, statsd

options = { 'statsd_host':'127.0.0.1', 'statsd_port':8125 }

initialize(**options)

statsd.event('An error occurred', 'Error message', alert_type='error', tags=['env:dev'])

event.rb

require 'datadog/statsd'

statsd = Datadog::Statsd.new('localhost', 8125)

statsd.event('An error occurred', "Error message", alert_type: 'error', tags: ['env:dev'])

event.go

package main

import ( "log" "time"

<span class="s">&#34;github.com/DataDog/datadog-go/statsd&#34;</span>

)

func main() {

<span class="nx">dogstatsdClient</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">statsd</span><span class="p">.</span><span class="nf">New</span><span class="p">(</span><span class="s">&#34;127.0.0.1:8125&#34;</span><span class="p">)</span>

<span class="k">if</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
    <span class="nx">log</span><span class="p">.</span><span class="nf">Fatal</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">for</span> <span class="p">{</span>
    <span class="nx">dogstatsdClient</span><span class="p">.</span><span class="nf">SimpleEvent</span><span class="p">(</span><span class="s">&#34;An error occurred&#34;</span><span class="p">,</span> <span class="s">&#34;Error message&#34;</span><span class="p">)</span>
    <span class="nx">time</span><span class="p">.</span><span class="nf">Sleep</span><span class="p">(</span><span class="mi">10</span> <span class="o">*</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">)</span>
<span class="p">}</span>

}

event.java

import com.timgroup.statsd.Event;
import com.timgroup.statsd.NonBlockingStatsDClientBuilder;
import com.timgroup.statsd.StatsDClient;

public class DogStatsdClient {

<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>

    <span class="n">StatsDClient</span> <span class="n">Statsd</span> <span class="o">=</span> <span class="k">new</span> <span class="n">NonBlockingStatsDClientBuilder</span><span class="o">()</span>
        <span class="o">.</span><span class="na">prefix</span><span class="o">(</span><span class="s">&#34;statsd&#34;</span><span class="o">).</span>
        <span class="o">.</span><span class="na">hostname</span><span class="o">(</span><span class="s">&#34;localhost&#34;</span><span class="o">)</span>
        <span class="o">.</span><span class="na">port</span><span class="o">(</span><span class="n">8125</span><span class="o">)</span>
        <span class="o">.</span><span class="na">build</span><span class="o">();</span>

    <span class="n">Event</span> <span class="n">event</span> <span class="o">=</span> <span class="n">Event</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
      <span class="o">.</span><span class="na">withTitle</span><span class="o">(</span><span class="s">&#34;An error occurred&#34;</span><span class="o">)</span>
      <span class="o">.</span><span class="na">withText</span><span class="o">(</span><span class="s">&#34;Error message&#34;</span><span class="o">)</span>
      <span class="o">.</span><span class="na">withAlertType</span><span class="o">(</span><span class="n">Event</span><span class="o">.</span><span class="na">AlertType</span><span class="o">.</span><span class="na">ERROR</span><span class="o">)</span>
      <span class="o">.</span><span class="na">build</span><span class="o">();</span>

    <span class="n">Statsd</span><span class="o">.</span><span class="na">recordEvent</span><span class="o">(</span><span class="n">event</span><span class="o">);</span>
<span class="o">}</span>

}

event.cs

using StatsdClient;

public class DogStatsdClient { public static void Main() { var dogstatsdConfig = new StatsdConfig { StatsdServerName = "127.0.0.1", StatsdPort = 8125, };

    <span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">dogStatsdService</span> <span class="p">=</span> <span class="k">new</span> <span class="n">DogStatsdService</span><span class="p">())</span>
    <span class="p">{</span>
        <span class="n">dogStatsdService</span><span class="p">.</span><span class="n">Configure</span><span class="p">(</span><span class="n">dogstatsdConfig</span><span class="p">);</span>
        <span class="n">dogStatsdService</span><span class="p">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#34;An error occurred&#34;</span><span class="p">,</span> <span class="s">&#34;Error message&#34;</span><span class="p">,</span> <span class="n">alertType</span><span class="p">:</span> <span class="s">&#34;error&#34;</span><span class="p">,</span> <span class="n">tags</span><span class="p">:</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span> <span class="s">&#34;env:dev&#34;</span> <span class="p">});</span>
    <span class="p">}</span>
<span class="p">}</span>

}

event.php

<?php

require DIR . '/vendor/autoload.php';

use DataDog\DogStatsd;

$statsd = new DogStatsd( array('host' => '127.0.0.1', 'port' => 8125, ) );

$statsd->event('An error occurred.', array( 'text' => 'Error message', 'alert_type' => 'error' ) );

La bibliothèque DogStatsD-PHP vous permet d’envoyer des événements à l’API Datadog directement via TCP. Cette méthode est plus lente mais aussi plus fiable que lorsque vous faites appel à l’instance DogStatsD de l’Agent, les événements étant transmis de votre application à l’Agent via UDP. Pour l’utiliser, vous devez configurer la bibliothèque avec vos clés d’API et d’application Datadog au lieu de l’instance DogStatsD locale :

event_through_api.php

<?php

require DIR . '/vendor/autoload.php';

use DataDog\DogStatsd;

$statsd = new DogStatsd( array('api_key' => '<CLÉ_API_DATADOG>', 'app_key' => '<CLÉ_APPLICATION_DATADOG>', ) );

$statsd->event('An error occurred.', array( 'text' => 'Error message', 'alert_type' => 'error' ) );

Remarques :

  • L’envoi d’événements via cette méthode fait appel à cURL pour les requêtes d’API.
  • Nous vous conseillons d’utiliser un bloc de code try/catch pour éviter d’envoyer des avertissements ou des erreurs en cas de problème de communication avec l’API Datadog.

Pour aller plus loin