Skip to content

Commit

Permalink
Wrangle monitor export JSON to be in the right format for us (DataDog…
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
iliakur authored Dec 10, 2024
1 parent 37a3772 commit e8cbf41
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions ddev/changelog.d/17936.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add script to convert monitor export json into the JSON we can use
2 changes: 2 additions & 0 deletions ddev/src/ddev/cli/meta/scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datadog_checks.dev.tooling.commands.meta.scripts.remove_labels import remove_labels

from ddev.cli.meta.scripts.generate_metrics import generate_metrics
from ddev.cli.meta.scripts.monitor import monitor
from ddev.cli.meta.scripts.serve_openmetrics_payload import serve_openmetrics_payload
from ddev.cli.meta.scripts.upgrade_python import upgrade_python

Expand All @@ -24,3 +25,4 @@ def scripts():
scripts.add_command(remove_labels)
scripts.add_command(serve_openmetrics_payload)
scripts.add_command(upgrade_python)
scripts.add_command(monitor)
68 changes: 68 additions & 0 deletions ddev/src/ddev/cli/meta/scripts/monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import copy

import click

DESCRIPTION_SEED = """\
This monitor will alert you on XXX...
- Define the problem stated by the title.
- Answer why this is an issue worth alerting on.
- Describe the impact of the problem.
Official guidelines:
https://docs.datadoghq.com/developers/integrations/create-an-integration-recommended-monitor/#description
"""


@click.group
def monitor():
"""
Work with monitors.
"""


def _edit(text):
edited = click.edit(text=text, require_save=False)
return "" if edited is None else edited


def _drop_fields(exported):
x = copy.deepcopy(exported)
x.pop('id', None)
x['options'].pop('on_missing_data', None)
return x


@monitor.command
@click.argument("export_json", type=click.File())
def create(export_json):
"""
Create monitor spec from the JSON export of the monitor in the UI.
The exported monitor cannot be committed as-is, we have to rename, add, and drop some fields.
After you've copied the JSON in the UI you can either save it as a file or pipe it to STDIN:
\b
pbpaste | ddev meta monitor create -
"""
import json
from datetime import date

exported = json.load(export_json)
today = date.today().isoformat()
wrangled = {
"version": 2,
"created_at": today,
"last_updated_at": today,
"title": _edit(text=exported["name"]).strip(),
"description": _edit(text=DESCRIPTION_SEED).strip(),
"tags": exported["tags"],
"definition": _drop_fields(exported),
}
click.echo(
json.dumps(
wrangled,
indent=2,
)
)

0 comments on commit e8cbf41

Please sign in to comment.