-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Datadog alert destination (#6476)
* Add Datadog alert destination * Fix Datadog Event API response code: 200 -> 202 * Add datadog alert dest test * Sort test_destinations.py imports * Fix test_datadog_notify_calls_requests_post * Fix datadog alert dest: Add aggregation_key
- Loading branch information
1 parent
3ebf163
commit 4a36abc
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import logging | ||
import os | ||
|
||
import requests | ||
|
||
from redash.destinations import BaseDestination, register | ||
from redash.utils import json_dumps | ||
|
||
|
||
class Datadog(BaseDestination): | ||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
"type": "object", | ||
"properties": { | ||
"api_key": {"type": "string", "title": "API Key"}, | ||
"tags": {"type": "string", "title": "Tags"}, | ||
"priority": {"type": "string", "default": "normal", "title": "Priority"}, | ||
# https://docs.datadoghq.com/integrations/faq/list-of-api-source-attribute-value/ | ||
"source_type_name": {"type": "string", "default": "my_apps", "title": "Source Type Name"}, | ||
}, | ||
"secret": ["api_key"], | ||
"required": ["api_key"], | ||
} | ||
|
||
@classmethod | ||
def icon(cls): | ||
return "fa-datadog" | ||
|
||
def notify(self, alert, query, user, new_state, app, host, metadata, options): | ||
# Documentation: https://docs.datadoghq.com/api/latest/events/#post-an-event | ||
if new_state == "triggered": | ||
alert_type = "error" | ||
if alert.custom_subject: | ||
title = alert.custom_subject | ||
else: | ||
title = f"{alert.name} just triggered" | ||
else: | ||
alert_type = "success" | ||
if alert.custom_subject: | ||
title = alert.custom_subject | ||
else: | ||
title = f"{alert.name} went back to normal" | ||
|
||
if alert.custom_body: | ||
text = alert.custom_body | ||
else: | ||
text = f"{alert.name} changed state to {new_state}." | ||
|
||
query_url = f"{host}/queries/{query.id}" | ||
alert_url = f"{host}/alerts/{alert.id}" | ||
text += f"\nQuery: {query_url}\nAlert: {alert_url}" | ||
|
||
headers = { | ||
"Accept": "application/json", | ||
"Content-Type": "application/json", | ||
"DD-API-KEY": options.get("api_key"), | ||
} | ||
|
||
body = { | ||
"title": title, | ||
"text": text, | ||
"alert_type": alert_type, | ||
"priority": options.get("priority"), | ||
"source_type_name": options.get("source_type_name"), | ||
"aggregation_key": f"redash:{alert_url}", | ||
"tags": [], | ||
} | ||
|
||
tags = options.get("tags") | ||
if tags: | ||
body["tags"] = tags.split(",") | ||
body["tags"].extend( | ||
[ | ||
"redash", | ||
f"query_id:{query.id}", | ||
f"alert_id:{alert.id}", | ||
] | ||
) | ||
|
||
dd_host = os.getenv("DATADOG_HOST", "api.datadoghq.com") | ||
url = f"https://{dd_host}/api/v1/events" | ||
|
||
try: | ||
resp = requests.post(url, headers=headers, data=json_dumps(body), timeout=5.0) | ||
logging.warning(resp.text) | ||
if resp.status_code != 202: | ||
logging.error(f"Datadog send ERROR. status_code => {resp.status_code}") | ||
except Exception as e: | ||
logging.exception("Datadog send ERROR: %s", e) | ||
|
||
|
||
register(Datadog) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters