Datadog Python Library (datadogpy)
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
- gotcha Confusion between `datadog` and `datadog-api-client` libraries. `datadog` (this library) is the older, more general client. `datadog-api-client` is a newer, generated client providing comprehensive access to all Datadog API endpoints (including v2 and async capabilities). They have different installation paths (`pip install datadog` vs `pip install datadog-api-client`) and import patterns (`from datadog import ...` vs `from datadog_api_client import ...`). Ensure you install and import the correct library for your needs.
- gotcha API and Application Keys are critical for authentication. For API interactions (e.g., `api.Event.create`), both an API key and an Application key are almost always required.
- gotcha Regional endpoints must be configured for non-US Datadog accounts. If your Datadog instance is in the EU, for example, the default API host will be incorrect.
- gotcha DogStatsD metrics require a running Datadog Agent. The `datadog.statsd` client sends UDP packets to a local DogStatsD server (usually part of the Datadog Agent). If the Agent is not running or not accessible on the configured host/port, metrics will be dropped silently or fail.
- gotcha This `datadog` library does not provide APM (Application Performance Monitoring) tracing and profiling functionality. That is handled by the separate `ddtrace` library.
Install
-
pip install datadog
Imports
- initialize
from datadog import initialize
- api
from datadog import api
- statsd
from datadog import statsd
- ThreadStats
from datadog import ThreadStats
- ApiClient
from datadog_api_client import ApiClient
Quickstart
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}")