Skip to content

Commit

Permalink
Add methods for the persistent cache Agent interface (#6819)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored Jun 4, 2020
1 parent 3669349 commit 3f4d637
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,18 @@ def entrypoint(self, *args, **kwargs):

return entrypoint

def _persistent_cache_id(self, key):
# type: (str) -> str
return '{}_{}'.format(self.check_id, key)

def read_persistent_cache(self, key):
# type: (str) -> str
return datadog_agent.read_persistent_cache(self._persistent_cache_id(key))

def write_persistent_cache(self, key, value):
# type: (str, str) -> None
datadog_agent.write_persistent_cache(self._persistent_cache_id(key), value)

def set_external_tags(self, external_tags):
# type: (Sequence[ExternalTagType]) -> None
# Example of external_tags format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class DatadogAgentStub(object):

def __init__(self):
self._metadata = {}
self._cache = {}
self._config = self.get_default_config()

def get_default_config(self):
return {'enable_metadata_collection': True}

def reset(self):
self._metadata.clear()
self._cache.clear()
self._config = self.get_default_config()

def assert_metadata(self, check_id, data):
Expand Down Expand Up @@ -56,6 +58,12 @@ def set_external_tags(self, *args, **kwargs):
def tracemalloc_enabled(self, *args, **kwargs):
return False

def write_persistent_cache(self, key, value):
self._cache[key] = value

def read_persistent_cache(self, key):
return self._cache.get(key, '')


# Use the stub as a singleton
datadog_agent = DatadogAgentStub()
10 changes: 10 additions & 0 deletions datadog_checks_base/tests/test_agent_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ def test_load_config():
assert AgentCheck.load_config("raw_foo: bar") == {'raw_foo': 'bar'}


def test_persistent_cache(datadog_agent):
check = AgentCheck()
check.check_id = 'test'

check.write_persistent_cache('foo', 'bar')

assert datadog_agent.read_persistent_cache('test_foo') == 'bar'
assert check.read_persistent_cache('foo') == 'bar'


@pytest.mark.parametrize(
'enable_metadata_collection, expected_is_metadata_collection_enabled',
[(None, False), ('true', True), ('false', False)],
Expand Down

0 comments on commit 3f4d637

Please sign in to comment.