Join us at the Dash conference! July 16-17, NYC

Python

Library Library

Overview

The Python integration allows you to monitor custom metrics by adding a few lines of code to your Python application. For example, a metric that returns the number of page views or the time of any function call.

Setup

Datadog offers a library to assist you with the implementation of Python application metrics. Learn more about the library on GitHub.

Installation

To install the Datadog Python library from pip:

pip install datadog

Metric Collection

For the Python integration, all metrics are custom metrics. For information on collecting custom metrics see the:

Below is an example of instrumenting your code using the Datadog API:

from datadog import initialize

options = {
    'api_key':'<YOUR_DD_API_KEY>',
    'app_key':'<YOUR_DD_APP_KEY>'
}

initialize(**options)

# Use Datadog REST API client
from datadog import api

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)

Below is an example of instrumenting your code using the DogStatsD client:

# Use Statsd, a Python client for DogStatsd
from datadog import statsd

statsd.increment('whatever')
statsd.gauge('foo', 42)

Below is an example of instrumenting your code using ThreadStats:

# ThreadStats is an alternative tool to collect and flush metrics,using Datadog REST API
from datadog import ThreadStats
stats = ThreadStats()
stats.start()
stats.increment('home.page.hits')

Trace collection

See Datadog’s dedicated documentation for Tracing Python Applications.

Log collection

Available for Agent v6.0+

See Datadog’s dedicated documentation for Python log collection.

Troubleshooting

Need help? Contact Datadog support.

Further Reading

Additional helpful documentation, links, and articles: