Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mechanism to execute setup steps before the first check run #4713

Merged
merged 2 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import re
import traceback
import unicodedata
from collections import defaultdict
from collections import defaultdict, deque
from os.path import basename

import yaml
Expand Down Expand Up @@ -212,6 +212,9 @@ class except the :py:meth:`check` method but sometimes it might be useful for a
if metric_limit > 0:
self.metric_limiter = Limiter(self.name, 'metrics', metric_limit, self.warning)

# Functions that will be called exactly once (if successful) before the first check run
self.check_initializations = deque()

@staticmethod
def load_config(yaml_str):
"""
Expand Down Expand Up @@ -600,6 +603,14 @@ def check(self, instance):

def run(self):
try:
while self.check_initializations:
initialization = self.check_initializations.popleft()
try:
initialization()
except Exception:
self.check_initializations.appendleft(initialization)
raise
Copy link
Member

@AlexandreYang AlexandreYang Oct 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems this would have a similar effect, if I'm not missing something.

            while self.check_initializations:
                self.check_initializations[0]()
                self.check_initializations.pop(0)


instance = copy.deepcopy(self.instances[0])

if 'set_breakpoint' in self.init_config:
Expand Down
48 changes: 48 additions & 0 deletions datadog_checks_base/tests/test_agent_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,51 @@ def test_metric_limit_instance_config_zero(self, aggregator):
check.gauge("metric", 0)
assert len(check.get_warnings()) == 1 # get_warnings resets the array
assert len(aggregator.metrics("metric")) == 10


class TestCheckInitializations:
def test_success_only_once(self):
class TestCheck(AgentCheck):
def __init__(self, *args, **kwargs):
super(TestCheck, self).__init__(*args, **kwargs)
self.state = 1
self.initialize = mock.MagicMock(side_effect=self._initialize)
self.check_initializations.append(self.initialize)

def _initialize(self):
self.state += 1
if self.state % 2:
raise Exception('is odd')

def check(self, _):
pass

check = TestCheck('test', {}, [{}])
check.run()
check.run()
check.run()

assert check.initialize.call_count == 1

def test_error_retry(self):
class TestCheck(AgentCheck):
def __init__(self, *args, **kwargs):
super(TestCheck, self).__init__(*args, **kwargs)
self.state = 0
self.initialize = mock.MagicMock(side_effect=self._initialize)
self.check_initializations.append(self.initialize)

def _initialize(self):
self.state += 1
if self.state % 2:
raise Exception('is odd')

def check(self, _):
pass

check = TestCheck('test', {}, [{}])
check.run()
check.run()
check.run()

assert check.initialize.call_count == 2