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.
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.
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).
Alternatively, the environment variable DD_DOGSTATSD_URL can be used to define a UDS connection:
DD_DOGSTATSD_URL=unix:///var/run/datadog/dsd.socket
As with the UDP variant, manually supplying a statsd_socket_path takes precedence over this environment variable.
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
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:
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.
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.
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:
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:
datadog-cdk-constructs-v2Pythie’s Index: 93CDK Construct Library to automatically instrument Python and Node Lambda functions with Datadog using AWS CDK v2283.4K downloads/mo