Skip to content

Commit 526cbd4

Browse files
authored
Fix missed collection event to use collection interval (DataDog#21766)
* Fix missed collection event to use collection interval * Avoid divide by zero * Changelog
1 parent f6edc0a commit 526cbd4

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix missed collection event to use correct collection interval.

datadog_checks_base/datadog_checks/base/utils/db/utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class ConstantRateLimiter:
110110

111111
def __init__(self, rate_limit_s, max_sleep_chunk_s=5):
112112
"""
113-
:param rate_limit_s: rate limit in seconds
113+
:param rate_limit_s: rate limit in executions per second
114114
:param max_sleep_chunk_s: maximum size of each sleep chunk while waiting for the next period
115115
"""
116116
self.rate_limit_s = max(rate_limit_s, 0)
@@ -305,7 +305,9 @@ def __init__(
305305
):
306306
self._check = check
307307
self._config_host = config_host
308+
# The min_collection_interval is the expected collection interval for the main check
308309
self._min_collection_interval = min_collection_interval
310+
self._expected_collection_interval = 1 / rate_limit if rate_limit > 0 else 0
309311
# map[dbname -> psycopg connection]
310312
self._log = get_check_logger()
311313
self._job_loop_future = None
@@ -358,12 +360,12 @@ def run_job_loop(self, tags):
358360
if (
359361
hasattr(self._check, 'health')
360362
and self._enable_missed_collection_event
361-
and self._min_collection_interval >= 1
363+
and self._expected_collection_interval >= 1
362364
and self._last_run_start
363365
):
364366
# Assume a collection interval of less than 1 second is an attempt to run the job in a loop
365367
elapsed_time = time.time() - self._last_run_start
366-
if elapsed_time > self._min_collection_interval:
368+
if elapsed_time > self._expected_collection_interval:
367369
# Missed a collection interval, submit a health event for each feature that depends on this job
368370
for feature in self._features:
369371
self._check.health.submit_health_event(
@@ -377,8 +379,10 @@ def run_job_loop(self, tags):
377379
data={
378380
"dbms": self._dbms,
379381
"job_name": self._job_name,
380-
"last_run_start": self._last_run_start,
381-
"elapsed_time": (time.time() - self._last_run_start) * 1000,
382+
# send in ms for consistency with other metrics
383+
"last_run_start": self._last_run_start * 1000,
384+
"elapsed_time": elapsed_time * 1000,
385+
"expected_collection_interval": self._expected_collection_interval * 1000,
382386
"feature": feature,
383387
},
384388
)

datadog_checks_base/tests/base/utils/db/test_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_dbm_async_job_missed_collection_interval(aggregator):
128128
check = AgentCheck()
129129
health = Health(check)
130130
check.health = health
131-
job = JobForTesting(check, min_collection_interval=1, job_execution_time=3)
131+
job = JobForTesting(check, job_execution_time=3, rate_limit=1 / 1)
132132
job.run_job_loop([])
133133
# Sleep longer than the target collection interval
134134
time.sleep(1.5)

0 commit comments

Comments
 (0)