Flask-Datadog

0.1.4 · maintenance · verified Thu Apr 16

Flask-Datadog is a micro-framework extension for Flask applications, providing access to DogStatsD for custom metrics. Last updated in 2017, it offers a simple wrapper around the `datadog` library's StatsD and API clients. For comprehensive application performance monitoring (APM), tracing, and logging with Flask, Datadog's officially supported `ddtrace` library is the current recommended solution.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes Flask-Datadog for both StatsD metric submission and Datadog API interactions. It demonstrates incrementing a custom metric on a successful route and another on an error route. Ensure `DATADOG_STATSD_HOST`, `DATADOG_STATSD_PORT`, `DATADOG_API_KEY`, and `DATADOG_APP_KEY` environment variables are set for full functionality, though StatsD will default to localhost:8125.

from flask import Flask
from flask_datadog import API, StatsD
import os

app = Flask(__name__)

# Configure Datadog API and StatsD settings
app.config['STATSD_HOST'] = os.environ.get('DATADOG_STATSD_HOST', 'localhost')
app.config['STATSD_PORT'] = int(os.environ.get('DATADOG_STATSD_PORT', '8125'))
app.config['DATADOG_API_KEY'] = os.environ.get('DATADOG_API_KEY', '')
app.config['DATADOG_APP_KEY'] = os.environ.get('DATADOG_APP_KEY', '')

statsd = StatsD(app)
dogapi = API(app)

@app.route('/')
def hello():
    statsd.increment('my_flask_app.requests')
    return 'Hello, Datadog!'

@app.route('/error')
def error_route():
    statsd.increment('my_flask_app.errors')
    # Example of API usage (requires valid API/App keys)
    # if dogapi.api_key and dogapi.app_key:
    #     dogapi.Event.create('An error occurred in Flask app', 
    #                        tags=['env:dev', 'service:my-app'])
    raise Exception('This is a test error!')

if __name__ == '__main__':
    # Ensure the Datadog Agent is running and accessible at STATSD_HOST:STATSD_PORT
    # For API calls, ensure DATADOG_API_KEY and DATADOG_APP_KEY are set.
    app.run(debug=True)

view raw JSON →