Create and manage DataDog monitors using declarative configuration
309
dogwhistle is a tool for managing DataDog resources using declarative configuration files. This allows for the ability to store configuration in source control for better accountability and management.
This project is largely based on DogPush, however it has some fundamental design differences, and is also a total rewrite in order to clean up code structure and update to Python 3.
dogwhistle can be installed on all platforms via Docker or pip.
dogwhistle can be installed via either Docker or pip.
On Windows, it is recommended that you use the ActivePython distribution of Python 3, as it comes with Pip.
On MacOS, Homebrew is recommended.
Once Python 3 and Pip are installed, run the following command to install dogwhistle from PyPi:
pip install dogwhistle
To install dogwhistle from source, run the following command from the cloned repository:
pip install .
DogWhistle is available as a Docker image on Docker Hub.
Example run:
docker run --rm -it adammillerio/dogwhistle:latest init
To build the Docker container from source, run the following command from the cloned repository:
docker build -t dogwhistle:latest .
At the very least, DogWhistle requires a minimum configuration of API and Application keys from DataDog.
DogWhistle configuration can be provided in multiple ways.
The first option is a config file. DogWhistle's configuration file is located by default in ~/.dogwhistlerc, however, you can manually specify a config location with the --config flag
Alternatively, configuration can be provided via command line flag or environment variable.
| Config | Flag | Environment | Type | Default | Description |
|---|---|---|---|---|---|
| api_key | --api-key | DOGWHISTLE_API_KEY | String | N/A | API key for DataDog |
| app_key | --app-key | DOGWHISTLE_APP_KEY | String | N/A | Application key for DataDog |
| config | --config | DOGWHISTLE_CONFIG | String | ~/.dogwhistlerc | Path to DogWhistle config file |
| verbose | --verbose | DOGWHISTLE_VERSBOSE | Boolean | false | Enable verbose output |
| dry_run | --dry-run | DOGWHISTLE_DRY_RUN | Boolean | false | "Dry run" Print actions without applying |
| force | -f, --force | DOGWHISTLE_FORCE | Boolean | false | Don't ask for confirmation prior to applying |
A spec file in DogWhistle is a set of resources as they are to be represented in the DataDog API. Here is an example of a spec file with a single monitor retrieved via the init subcommand:
monitors:
- name: '[team] my-stream IteratorAge is over 4 hours'
type: metric alert
query: avg(last_4h):avg:aws.kinesis.get_records_iterator_age_milliseconds{streamname:my-stream} > 3600000
message: '@[email protected]'
multi: false
options:
escalation_message: ''
evaluation_delay: 900
include_tags: true
new_host_delay: 300
no_data_timeframe: null
notify_audit: true
notify_no_data: false
renotify_interval: 30
require_full_window: true
thresholds:
critical: 3600000.0
timeout_h: 0
The main thing to know is that the monitor represented in the spec file above is a direct representation of a Monitor object in the DataDog Python API. This makes it easy to reference the DataDog API documentation to learn what the various values mean.
You can view information about building Monitors here.
DogWhistle is also able to generate yaml spec files via jinja2 templating. For example, we could make the spec file above take two parameters, priority and stream and use these in a template file called kinesis.yaml.j2.
Note: DogWhistle detects template files via the .j2 file extension.
monitors:
- name: '[team] {{ stream }} IteratorAge is over 4 hours'
type: metric alert
query: avg(last_4h):avg:aws.kinesis.get_records_iterator_age_milliseconds{streamname:{{ stream }}} > 3600000
{% if priority == 'high' %}
message: '@pagerduty'
{% else %}
message: '@[email protected]'
{% endif %}
multi: false
options:
escalation_message: ''
evaluation_delay: 900
include_tags: true
new_host_delay: 300
no_data_timeframe: null
notify_audit: true
notify_no_data: false
renotify_interval: 30
require_full_window: true
thresholds:
critical: 3600000.0
timeout_h: 0
Now, we can invoke dogwhistle with -p priority=high -p stream=my-stream and it will substitute in the stream name, and also dynamically set the monitor to notify the PagerDuty integration since it is high priority.
Parameters can also be stored in key value format in a json file and loaded via --params-file params.json:
{
"priority": "high",
"stream": "my-stream"
}
There is a wide range of options that are available when using the jinja2 templating library that allow for dynamic configuration of DataDog resources.
All resources that are created by DogWhistle have a special tag applied to them, dogwhistle.managed:true. This tag is transparently used to allow the tool to filter out resources that it does not manage.
DogWhistle uses spec files to define DataDog resources. These spec files are essentially direct representations of the DataDog API, with a few small changes and the ability to use jinja2 templating. If you already have an initial set of monitors deployed in DataDog through other means, you can retrieve them with the init command:
dogwhistle init --prefix '[team]'
This will retrieve all resources in a given account that start with [team] and output a spec file representation of them that can be used with other DogWhistle commands. This output can then be redirected to a file to be edited:
dogwhistle init --prefix '[team]' > team-spec.yaml
Diff will take a set of provided spec files and check them against what is deployed in DataDog. It will then output a diff with two sections:
dogwhistle diff kinesis-spec.yaml.j2
Push will take a set of provided spec files and check them against what is deployed in DataDog. If the force option is not provided, it will then print a diff for the user and ask them to manually approve the changes. Then, it will create/update resources in DataDog as necessary.
Note: DogWhistle does not delete resources.
dogwhistle push kinesis-spec.yaml.j2
Mute will take a set of provided spec files and mute the associated monitors in DataDog, disabling all notification targets. This is useful for running deployments.
Note: Currently rules for muting are not implemented, use the --forever flag to mute indiscriminately.
dogwhistle mute kinesis-spec.yaml.j2
Unmute will take a set of provided spec files and unmute the associated monitors in DataDog.
dogwhistle unmute kinesis-spec.yaml.j2
To develop on DogWhistle, it is recommended to install it in development mode with pip:
pip install -e .
This will install dogwhistle so that it points to the current working directory, allowing for iteration without having to reinstall.
This repository has MyPy type hints for type enforcement.
To use these, first install mypy:
pip install mypy
Then, run the following command from the root of the repository:
mypy dogwhistle
Content type
Image
Digest
Size
37.2 MB
Last updated
almost 7 years ago
docker pull adammillerio/dogwhistle