Datadog Python Library (datadogpy)

0.52.1 · active · verified Sat Mar 28

The `datadog` Python library (`datadogpy`) provides convenient interfaces for interacting with Datadog's HTTP API and sending metrics, events, and service checks via DogStatsD. It supports both UDP and Unix Domain Socket (UDS) transports for DogStatsD and includes a CLI tool ('dog') for API operations. As of version 0.52.1, it continues to be actively maintained, focusing on core API interactions and DogStatsD client functionality.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Datadog client using environment variables for API and Application keys and then send both an event via the HTTP API and custom metrics via DogStatsD. Remember to have the Datadog Agent running for DogStatsD metrics.

import os
from datadog import initialize, api, statsd

# Configure with API and App keys, preferably from environment variables
options = {
    'api_key': os.environ.get('DD_API_KEY', 'YOUR_DATADOG_API_KEY'),
    'app_key': os.environ.get('DD_APP_KEY', 'YOUR_DATADOG_APP_KEY'),
    # Uncomment and set api_host if your Datadog account is outside the US (e.g., EU)
    # 'api_host': 'https://api.datadoghq.eu'
}
initialize(**options)

# Send an event to the Datadog Event Stream
try:
    title = "Python Quickstart Event!"
    text = "This is a test event sent from the datadog Python library."
    tags = ["env:dev", "service:my-app", "source:python-script"]
    response = api.Event.create(title=title, text=text, tags=tags)
    print(f"Event sent successfully: {response.get('status')}. Event ID: {response.get('event', {}).get('id')}")
except Exception as e:
    print(f"Error sending event: {e}")

# Send a custom metric via DogStatsD (requires Datadog Agent running)
try:
    statsd.increment('my_app.page_views', tags=['page:home', 'version:1.0'])
    statsd.gauge('my_app.users_online', 150, tags=['region:us-east'])
    print("Metrics sent via DogStatsD.")
except Exception as e:
    print(f"Error sending metrics via DogStatsD: {e}")

view raw JSON →