Skip to content

Commit e8cbf41

Browse files
authored
Wrangle monitor export JSON to be in the right format for us (#17936)
* Wrangle monitor export JSON to be in the right format for us * add changelog * make sure title and description are strings * Expand description seed * drop invalid field * add link to official guidelines * drop id field from definition * add changelog * fix changelog * Move to scripts folder
1 parent 37a3772 commit e8cbf41

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

ddev/changelog.d/17936.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add script to convert monitor export json into the JSON we can use

ddev/src/ddev/cli/meta/scripts/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datadog_checks.dev.tooling.commands.meta.scripts.remove_labels import remove_labels
88

99
from ddev.cli.meta.scripts.generate_metrics import generate_metrics
10+
from ddev.cli.meta.scripts.monitor import monitor
1011
from ddev.cli.meta.scripts.serve_openmetrics_payload import serve_openmetrics_payload
1112
from ddev.cli.meta.scripts.upgrade_python import upgrade_python
1213

@@ -24,3 +25,4 @@ def scripts():
2425
scripts.add_command(remove_labels)
2526
scripts.add_command(serve_openmetrics_payload)
2627
scripts.add_command(upgrade_python)
28+
scripts.add_command(monitor)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import copy
2+
3+
import click
4+
5+
DESCRIPTION_SEED = """\
6+
This monitor will alert you on XXX...
7+
8+
- Define the problem stated by the title.
9+
- Answer why this is an issue worth alerting on.
10+
- Describe the impact of the problem.
11+
12+
Official guidelines:
13+
https://docs.datadoghq.com/developers/integrations/create-an-integration-recommended-monitor/#description
14+
"""
15+
16+
17+
@click.group
18+
def monitor():
19+
"""
20+
Work with monitors.
21+
"""
22+
23+
24+
def _edit(text):
25+
edited = click.edit(text=text, require_save=False)
26+
return "" if edited is None else edited
27+
28+
29+
def _drop_fields(exported):
30+
x = copy.deepcopy(exported)
31+
x.pop('id', None)
32+
x['options'].pop('on_missing_data', None)
33+
return x
34+
35+
36+
@monitor.command
37+
@click.argument("export_json", type=click.File())
38+
def create(export_json):
39+
"""
40+
Create monitor spec from the JSON export of the monitor in the UI.
41+
42+
The exported monitor cannot be committed as-is, we have to rename, add, and drop some fields.
43+
44+
After you've copied the JSON in the UI you can either save it as a file or pipe it to STDIN:
45+
46+
\b
47+
pbpaste | ddev meta monitor create -
48+
"""
49+
import json
50+
from datetime import date
51+
52+
exported = json.load(export_json)
53+
today = date.today().isoformat()
54+
wrangled = {
55+
"version": 2,
56+
"created_at": today,
57+
"last_updated_at": today,
58+
"title": _edit(text=exported["name"]).strip(),
59+
"description": _edit(text=DESCRIPTION_SEED).strip(),
60+
"tags": exported["tags"],
61+
"definition": _drop_fields(exported),
62+
}
63+
click.echo(
64+
json.dumps(
65+
wrangled,
66+
indent=2,
67+
)
68+
)

0 commit comments

Comments
 (0)