From b88a852c2c6efdca7b48a57a104af57721973ba9 Mon Sep 17 00:00:00 2001 From: Sherzod Karimov Date: Mon, 10 Jul 2023 11:52:47 -0400 Subject: [PATCH 1/3] add support for syncing slo monitors --- datadog_sync/model/monitors.py | 20 ++++++++++++++++++-- datadog_sync/utils/resources_manager.py | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/datadog_sync/model/monitors.py b/datadog_sync/model/monitors.py index 07a9bcbd..b56ea6f7 100644 --- a/datadog_sync/model/monitors.py +++ b/datadog_sync/model/monitors.py @@ -6,6 +6,7 @@ from __future__ import annotations import re from typing import TYPE_CHECKING, Optional, List, Dict, cast +import inspect from datadog_sync.utils.base_resource import BaseResource, ResourceConfig @@ -16,7 +17,11 @@ class Monitors(BaseResource): resource_type = "monitors" resource_config = ResourceConfig( - resource_connections={"monitors": ["query"], "roles": ["restricted_roles"], "synthetics_tests": []}, + resource_connections={ + "monitors": ["query"], + "roles": ["restricted_roles"], + "service_level_objectives": ["query"], + }, base_path="/api/v1/monitor", excluded_attributes=[ "id", @@ -44,7 +49,7 @@ def import_resource(self, _id: Optional[str] = None, resource: Optional[Dict] = resource = source_client.get(self.resource_config.base_path + f"/{_id}").json() resource = cast(dict, resource) - if resource["type"] in ("synthetics alert", "slo alert"): + if resource["type"] == "synthetics alert": return self.resource_config.source_resources[str(resource["id"])] = resource @@ -80,6 +85,7 @@ def delete_resource(self, _id: str) -> None: def connect_id(self, key: str, r_obj: Dict, resource_to_connect: str) -> Optional[List[str]]: monitors = self.config.resources[resource_to_connect].resource_config.destination_resources synthetics_tests = self.config.resources["synthetics_tests"].resource_config.destination_resources + slos = self.config.resources["service_level_objectives"].resource_config.destination_resources if r_obj.get("type") == "composite" and key == "query": failed_connections = [] @@ -100,6 +106,16 @@ def connect_id(self, key: str, r_obj: Dict, resource_to_connect: str) -> Optiona failed_connections.append(_id) r_obj[key] = (r_obj[key].replace("#", "")).strip() return failed_connections + elif resource_to_connect == "service_level_objectives" and r_obj.get("type") == "slo alert" and key == "query": + failed_connections = [] + ids = re.search('error_budget\("(.*)"\)\.', r_obj[key]) + if ids: + for _id in ids.groups(): + if _id in slos: + r_obj[key] = re.sub(_id, slos[_id]["id"], r_obj[key]) + else: + failed_connections.append(_id) + return failed_connections elif key == "query": return None else: diff --git a/datadog_sync/utils/resources_manager.py b/datadog_sync/utils/resources_manager.py index f91686ca..64dc7ce5 100644 --- a/datadog_sync/utils/resources_manager.py +++ b/datadog_sync/utils/resources_manager.py @@ -21,7 +21,7 @@ def __init__(self, config: Configuration) -> None: self.all_resources: Dict[str, str] = {} # mapping of all resources to its resource_type self.all_cleanup_resources: Dict[str, str] = {} # mapping of all resources to cleanup self.dependencies_graph: Dict[str, Set[str]] = {} # dependency graph - self.all_missing_resources: Dict[str, str] = {} # mapping of all missing resources imported + self.all_missing_resources: Dict[str, str] = {} # mapping of all missing resources imported self.missing_resources_queue: deque = deque() # queue for missing resources for resource_type in config.resources_arg: From 938076ba660492cce3b5ef1d99bc41a60da1729d Mon Sep 17 00:00:00 2001 From: Sherzod Karimov Date: Mon, 10 Jul 2023 11:53:30 -0400 Subject: [PATCH 2/3] remove inspect import --- datadog_sync/model/monitors.py | 1 - 1 file changed, 1 deletion(-) diff --git a/datadog_sync/model/monitors.py b/datadog_sync/model/monitors.py index b56ea6f7..ca9b7979 100644 --- a/datadog_sync/model/monitors.py +++ b/datadog_sync/model/monitors.py @@ -6,7 +6,6 @@ from __future__ import annotations import re from typing import TYPE_CHECKING, Optional, List, Dict, cast -import inspect from datadog_sync.utils.base_resource import BaseResource, ResourceConfig From 8e283bb695e6fd002e52b24e780303504e5c37af Mon Sep 17 00:00:00 2001 From: Sherzod Karimov Date: Mon, 10 Jul 2023 12:04:55 -0400 Subject: [PATCH 3/3] cleanup --- datadog_sync/model/monitors.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/datadog_sync/model/monitors.py b/datadog_sync/model/monitors.py index ca9b7979..aa26d852 100644 --- a/datadog_sync/model/monitors.py +++ b/datadog_sync/model/monitors.py @@ -107,13 +107,12 @@ def connect_id(self, key: str, r_obj: Dict, resource_to_connect: str) -> Optiona return failed_connections elif resource_to_connect == "service_level_objectives" and r_obj.get("type") == "slo alert" and key == "query": failed_connections = [] - ids = re.search('error_budget\("(.*)"\)\.', r_obj[key]) - if ids: - for _id in ids.groups(): - if _id in slos: - r_obj[key] = re.sub(_id, slos[_id]["id"], r_obj[key]) - else: - failed_connections.append(_id) + if res := re.search('error_budget\("(.*)"\)\.', r_obj[key]): + _id = res.group(1) + if _id in slos: + r_obj[key] = re.sub(_id, slos[_id]["id"], r_obj[key]) + else: + failed_connections.append(_id) return failed_connections elif key == "query": return None