The Datadog Python library
Project description
The Datadog Python library
The Datadog Python Library is a collection of tools suitable for inclusion in existing Python projects or for the development of standalone scripts. It provides an abstraction on top of Datadog's raw HTTP interface and the Agent's DogStatsD metrics aggregation server, to interact with Datadog and efficiently report events and metrics.
- Library Documentation: https://datadogpy.readthedocs.io/en/latest/
- HTTP API Documentation: https://docs.datadoghq.com/api/
- DatadogHQ: https://datadoghq.com
See CHANGELOG.md for changes.
Installation
To install from pip:
pip install datadog
To install from source:
python setup.py install
Datadog API
To support all Datadog HTTP APIs, a generated library is available which will expose all the endpoints: datadog-api-client-python.
Find below a working example for submitting an event to your Event Stream:
from datadog import initialize, api
options = {
"api_key": "<YOUR_API_KEY>",
"app_key": "<YOUR_APP_KEY>",
}
initialize(**options)
title = "Something big happened!"
text = "And let me tell you all about it here!"
tags = ["version:1", "application:web"]
api.Event.create(title=title, text=text, tags=tags)
Consult the full list of supported Datadog API endpoints with working code examples in the Datadog API documentation.
Note: The full list of available Datadog API endpoints is also available in the Datadog Python Library documentation
Environment Variables
As an alternate method to using the initialize
function with the options
parameters, set the environment variables DATADOG_API_KEY
and DATADOG_APP_KEY
within the context of your application.
If DATADOG_API_KEY
or DATADOG_APP_KEY
are not set, the library attempts to fall back to Datadog's APM environment variable prefixes: DD_API_KEY
and DD_APP_KEY
.
from datadog import initialize, api
# Assuming you've set `DD_API_KEY` and `DD_APP_KEY` in your env,
# initialize() will pick it up automatically
initialize()
title = "Something big happened!"
text = "And let me tell you all about it here!"
tags = ["version:1", "application:web"]
api.Event.create(title=title, text=text, tags=tags)
In development, you can disable any statsd
metric collection using DD_DOGSTATSD_DISABLE=True
(or any not-empty value).
DogStatsD
In order to use DogStatsD metrics, the Agent must be running and available.
Instantiate the DogStatsD client with UDP
Once the Datadog Python Library is installed, instantiate the StatsD client using UDP in your code:
from datadog import initialize, statsd
options = {
"statsd_host": "127.0.0.1",
"statsd_port": 8125,
}
initialize(**options)
See the full list of available DogStatsD client instantiation parameters.
Instantiate the DogStatsd client with UDS
Once the Datadog Python Library is installed, instantiate the StatsD client using UDS in your code:
from datadog import initialize, statsd
options = {
"statsd_socket_path": PATH_TO_SOCKET,
}
initialize(**options)
Origin detection over UDP and UDS
Origin detection is a method to detect which pod DogStatsD
packets are coming from in order to add the pod's tags to the tag list.
The DogStatsD
client attaches an internal tag, entity_id
. The value of this tag is the content of the DD_ENTITY_ID
environment variable if found, which is the pod's UID. The Datadog Agent uses this tag to add container tags to the metrics. To avoid overwriting this global tag, make sure to only append
to the constant_tags
list.
To enable origin detection over UDP, add the following lines to your application manifest
env:
- name: DD_ENTITY_ID
valueFrom:
fieldRef:
fieldPath: metadata.uid
Usage
Metrics
After the client is created, you can start sending custom metrics to Datadog. See the dedicated Metric Submission: DogStatsD documentation to see how to submit all supported metric types to Datadog with working code examples:
- Submit a COUNT metric.
- Submit a GAUGE metric.
- Submit a SET metric
- Submit a HISTOGRAM metric
- Submit a TIMER metric
- Submit a DISTRIBUTION metric
Some options are supported when submitting metrics, like applying a Sample Rate to your metrics or tagging your metrics with your custom tags.
Events
After the client is created, you can start sending events to your Datadog Event Stream. See the dedicated Event Submission: DogStatsD documentation to see how to submit an event to your Datadog Event Stream.
Service Checks
After the client is created, you can start sending Service Checks to Datadog. See the dedicated Service Check Submission: DogStatsD documentation to see how to submit a Service Check to Datadog.
Monitoring this client
This client automatically injects telemetry about itself in the DogStatsD stream.
Those metrics will not be counted as custom and will not be billed. This feature can be disabled using the statsd.disable_telemetry()
method.
See Telemetry documentation to learn more about it.
Benchmarks
Note: You will need to install psutil
package before running the benchmarks.
If you would like to get an approximate idea on the throughput that your DogStatsD library can handle on your system, you can run the included local benchmark code:
$ # Python 2 Example
$ python2 -m unittest -vvv tests.performance.test_statsd_throughput
$ # Python 3 Example
$ python3 -m unittest -vvv tests.performance.test_statsd_throughput
You can also add set BENCHMARK_*
to customize the runs:
$ # Example #1
$ BENCHMARK_NUM_RUNS=10 BENCHMARK_NUM_THREADS=1 BENCHMARK_NUM_DATAPOINTS=5000 BENCHMARK_TRANSPORT="UDP" python2 -m unittest -vvv tests.performance.test_statsd_throughput
$ # Example #2
$ BENCHMARK_NUM_THREADS=10 BENCHMARK_TRANSPORT="UDS" python3 -m unittest -vvv tests.performance.test_statsd_throughput
Maximum packets size in high-throughput scenarios
In order to have the most efficient use of this library in high-throughput scenarios,
default values for the maximum packets size have already been set for both UDS (8192 bytes)
and UDP (1432 bytes) in order to have the best usage of the underlying network.
However, if you perfectly know your network and you know that a different value for the maximum packets
size should be used, you can set it with the parameter max_buffer_len
. Example:
from datadog import initialize
options = {
"api_key": "<YOUR_API_KEY>",
"app_key": "<YOUR_APP_KEY>",
"max_buffer_len": 4096,
}
initialize(**options)
Thread Safety
DogStatsD
and ThreadStats
are thread-safe.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file datadog-0.44.0.tar.gz
.
File metadata
- Download URL: datadog-0.44.0.tar.gz
- Upload date:
- Size: 340.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 071170f0c7ef22511dbf7f9bd76c4be500ee2d3d52072900a5c87b5495d2c733 |
|
MD5 | 1ab2f2248b7a36ca42fdb716b776b4bf |
|
BLAKE2b-256 | fc752d572e944f3e93f5873e8953afee003fdfa602a0b7ee8a3e32d1cec6591b |
File details
Details for the file datadog-0.44.0-py2.py3-none-any.whl
.
File metadata
- Download URL: datadog-0.44.0-py2.py3-none-any.whl
- Upload date:
- Size: 111.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57c4878d3a8351f652792cdba78050274789dcc44313adec096e87f9d3ca5992 |
|
MD5 | 1b339f333791e25cec1114ffc3abef60 |
|
BLAKE2b-256 | 28c59699efaf6e530b879fdb5ec2373c5959f396c29a00d4a05a9c4cf9f29292 |