Skip to content

Commit

Permalink
refactor(backends/datadog): avoid useless call to Datadog API when no…
Browse files Browse the repository at this point in the history
…t in debug mode (#356)
  • Loading branch information
sdenef-adeo authored Nov 22, 2023
1 parent 0283e20 commit d5e03f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 5 additions & 2 deletions slo_generator/backends/datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import datadog

from slo_generator import utils

LOGGER = logging.getLogger(__name__)
logging.getLogger("datadog.api").setLevel(logging.ERROR)

Expand Down Expand Up @@ -137,8 +139,9 @@ def query_slo(self, timestamp, window, slo_config):
"""
slo_id = slo_config["spec"]["service_level_indicator"]["slo_id"]
from_ts = timestamp - window
slo_data = self.client.ServiceLevelObjective.get(id=slo_id)
LOGGER.debug(f"SLO data: {slo_id} | Result: {pprint.pformat(slo_data)}")
if utils.is_debug_enabled():
slo_data = self.client.ServiceLevelObjective.get(id=slo_id)
LOGGER.debug(f"SLO data: {slo_id} | Result: {pprint.pformat(slo_data)}")
data = self.client.ServiceLevelObjective.history(
id=slo_id,
from_ts=from_ts,
Expand Down
7 changes: 6 additions & 1 deletion slo_generator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,14 @@ def replace_env_vars(content, ctx) -> str:
return data


def is_debug_enabled():
"""Check if DEBUG mode is enabled."""
return DEBUG == 1


def setup_logging():
"""Setup logging for the CLI."""
if DEBUG == 1:
if is_debug_enabled():
print(f"DEBUG mode is enabled. DEBUG={DEBUG}")
level = logging.DEBUG
format_str = "%(name)s - %(levelname)s - %(message)s"
Expand Down

0 comments on commit d5e03f0

Please sign in to comment.