Skip to content
The Datadog Python library
Python Ruby
Branch: master
Clone or download

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.azure-pipelines Initial support for Python type hints and Mypy type checking (#565) Jun 4, 2020
.github Add codeowners per subproject (#520) Jan 28, 2020
datadog Initial support for Python type hints and Mypy type checking (#565) Jun 4, 2020
doc add license infos and more (#503) Jan 6, 2020
tests Fix decode attribute error with options for py3 (#555) Apr 7, 2020
.gitignore Initial support for Python type hints and Mypy type checking (#565) Jun 4, 2020
.pylintrc Initial commit Mar 11, 2015
CHANGELOG.md Release v0.36.0 (#560) May 5, 2020
CONTRIBUTING.md Updating Contributing doc (#516) Jan 18, 2020
DEVELOPMENT.md Release v0.36.0 (#560) May 5, 2020
LICENSE add license infos and more (#503) Jan 6, 2020
LICENSE-3rdparty.csv Add 3rd party licenses file and update doc (#522) Jan 30, 2020
MANIFEST.in Initial support for Python type hints and Mypy type checking (#565) Jun 4, 2020
README.md [Documentation Update] Add UDS Configuration (#556) Apr 14, 2020
RELEASING.md Update RELEASING.md May 5, 2020
Rakefile.rb Make sure we distribute wheels in addition to sdists Sep 13, 2019
SUPPORT.md add license infos and more (#503) Jan 6, 2020
mypy.ini Initial support for Python type hints and Mypy type checking (#565) Jun 4, 2020
setup.py Initial support for Python type hints and Mypy type checking (#565) Jun 4, 2020
tox.ini Initial support for Python type hints and Mypy type checking (#565) Jun 4, 2020

README.md

The Datadog Python library

Unit Tests Integration Tests Documentation Status PyPI - Version PyPI - Downloads

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.

See CHANGELOG.md for changes.

Installation

To install from pip:

pip install datadog

To install from source:

python setup.py install

Datadog API

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 environmnent 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)

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 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 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:

Some options are suppported 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.

Thread Safety

DogStatsD and ThreadStats are thread-safe.

You can’t perform that action at this time.