Skip to content

Commit

Permalink
fixup! feat: add log package and performance module
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Sep 23, 2024
1 parent b2dfedc commit 9f89d20
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions cloudinit/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ def __exit__(self, exc_type, exc_val, exc_tb):
suffix = f"took {self.delta:.3f} seconds"
if "always" == self.log_mode:
LOG.debug("%s %s", self.msg, suffix)
elif "threshold" == self.log_mode and self.delta > self.threshold:
LOG.debug("%s %s", self.msg, suffix)
self.output = f"{self.msg} {suffix}"


def timed(msg: str, /, *, threshold: float = 0.01, log_mode: str = ""):
elif "skip" == self.log_mode:
return
elif "threshold" == self.log_mode:
if self.delta > self.threshold:
LOG.debug("%s %s", self.msg, suffix)
self.output = f"{self.msg} {suffix}"
else:
raise ValueError(
f"Invalid Timed log_mode value: '{self.log_mode}'."
)


def timed(msg: str, /, *, threshold: float = 0.01, log_mode: str = "threshold"):
"""
A decorator which measures and optionally logs context run time.
Expand Down
2 changes: 1 addition & 1 deletion cloudinit/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def clear_cached_attrs(self, attr_defaults=()):
if not attr_defaults:
self._dirty_cache = False

@performance.timed("Getting metadata")
@performance.timed("Getting metadata", log_mode="always")
def get_data(self) -> bool:
"""Datasources implement _get_data to setup metadata and userdata_raw.
Expand Down

0 comments on commit 9f89d20

Please sign in to comment.