Cheatsheet for Datadog monitoring tool

Metrics

To send a metric to Datadog using Python:

import datadog
import time

datadog.initialize(api_key='YOUR_API_KEY_HERE')

while True:
    datadog.api.Metric.send(metric='myapp.requests.count', points=1)
    time.sleep(1)

To send a metric to Datadog using Bash:

#!/bin/bash

API_KEY="YOUR_API_KEY_HERE"
APP_KEY="YOUR_APP_KEY_HERE"
METRIC_NAME="myapp.requests.count"
METRIC_VALUE=1

curl -X POST -H "Content-type: application/json" \
-d "{\"series\" :
          [{\"metric\":\"$METRIC_NAME\",
            \"points\":[[$(date +%s),$METRIC_VALUE]],
            \"type\":\"gauge\",
            \"host\":\"$(hostname)\",
            \"tags\":[\"environment:production\"]}
          ]
}" \
"https://api.datadoghq.com/api/v1/series?api_key=$API_KEY&application_key=$APP_KEY"

Tags

To send a metric with tags to Datadog using Python:

import datadog
import time

datadog.initialize(api_key='YOUR_API_KEY_HERE')

while True:
    datadog.api.Metric.send(metric='myapp.requests.count', points=1, tags=['environment:production', 'app:myapp'])
    time.sleep(1)

To send a metric with tags to Datadog using Bash:

#!/bin/bash

API_KEY="YOUR_API_KEY_HERE"
APP_KEY="YOUR_APP_KEY_HERE"
METRIC_NAME="myapp.requests.count"
METRIC_VALUE=1

curl -X POST -H "Content-type: application/json" \
-d "{\"series\" :
          [{\"metric\":\"$METRIC_NAME\",
            \"points\":[[$(date +%s),$METRIC_VALUE]],
            \"type\":\"gauge\",
            \"host\":\"$(hostname)\",
            \"tags\":[\"environment:production\",\"app:myapp\"]}
          ]
}" \
"https://api.datadoghq.com/api/v1/series?api_key=$API_KEY&application_key=$APP_KEY"

Dashboards

To create a dashboard in Datadog using JSON:

{
    "title": "My Dashboard",
    "description": "This is my dashboard",
    "graphs": [
        {
            "title": "Requests per second",
            "definition": {
                "requests": [
                    {
                        "q": "avg:myapp.requests.count{environment:production} by {app}.rollup(avg, 60)",
                        "type": "line",
                        "style": {
                            "palette": "dog_classic",
                            "line_type": "solid",
                            "line_width": "normal",
                            "line_interpolation": "linear"
                        },
                        "conditional_formats": []
                    }
                ],
                "viz": "timeseries",
                "autoscale": true
            }
        }
    ]
}

To create a dashboard in Datadog using the web UI:

  1. Navigate to the Dashboards page in the Datadog web UI.
  2. Click the “New Dashboard” button.
  3. Configure your dashboard by adding graphs and widgets using the visual editor.
  4. Save your dashboard.

Alerts

To create an alert in Datadog using the web UI:

  1. Navigate to the Monitors page in the Datadog web UI.
  2. Click the “New Monitor” button.
  3. Configure your monitor by selecting the metric or event you want to monitor, and setting the alert
  4. thresholds and notification settings.
  5. 4. Save your monitor.

To create an alert in Datadog using JSON:

{
    "name": "My Alert",
    "query": "avg:myapp.requests.count{environment:production} by {app} > 10",
    "message": "Requests per second for {{#is_alert}}ALERT{{/is_alert}}{{#is_warning}}WARNING{{/is_warning}}: {{value}}",
    "tags": ["environment:production", "app:myapp"],
    "options": {
        "notify_no_data": true,
        "no_data_timeframe": 10,
        "timeout_h": 1,
        "evaluation_delay": 5,
        "escalation_message": "Alert escalated to level 2"
    }
}

To create an alert using Python:

import datadog

datadog.initialize(api_key='YOUR_API_KEY_HERE')

options = {
    'notify_no_data': True,
    'no_data_timeframe': 10,
    'timeout_h': 1,
    'evaluation_delay': 5,
    'escalation_message': 'Alert escalated to level 2'
}

query = 'avg:myapp.requests.count{environment:production} by {app} > 10'

alert = datadog.api.Monitor.create(
    type='metric alert',
    query=query,
    name='My Alert',
    message='Requests per second for {{#is_alert}}ALERT{{/is_alert}}{{#is_warning}}WARNING{{/is_warning}}: {{value}}',
    tags=['environment:production', 'app:myapp'],
    options=options
)

Logs

To send a log to Datadog using Python:

import datadog

datadog.initialize(api_key='YOUR_API_KEY_HERE')

datadog.api.Event.create(title='Log message', text='This is my log message', tags=['environment:production', 'app:myapp'])

To send a log to Datadog using Bash:

#!/bin/bash

API_KEY="YOUR_API_KEY_HERE"
APP_KEY="YOUR_APP_KEY_HERE"
TITLE="Log message"
TEXT="This is my log message"
TAGS="environment:production,app:myapp"

curl -X POST -H "Content-type: application/json" \
-d "{\"title\":\"$TITLE\",
     \"text\":\"$TEXT\",
     \"tags\":\"$TAGS\"
}" \
"https://api.datadoghq.com/api/v1/events?api_key=$API_KEY&application_key=$APP_KEY"

Leave a Reply

Your email address will not be published. Required fields are marked *