Skip to content

Commit

Permalink
Allow back passing a list of Query objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Florimond Manca committed Nov 9, 2020
1 parent b92cdc3 commit e82445b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion datadog_checks_base/datadog_checks/base/utils/db/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def __init__(self, check, executor, queries=None, tags=None, error_handler=None)
self.executor = executor
self.tags = tags or []
self.error_handler = error_handler
self.queries = [Query(payload) for payload in queries or []]
# Accept either a `List[Query]` (< 7.24.0), or a `List[dict]` to be wrapped in `Query` (7.24.0+).
# The latter was introduced to avoid an issue with passing mutable query objects,
# see: https://github.com/DataDog/integrations-core/pull/7750
# But we still accept `List[Query]` for backwards compatibility.
self.queries = [Query(query) if not isinstance(query, Query) else query for query in queries or []]
custom_queries = list(self.check.instance.get('custom_queries', []))
use_global_custom_queries = self.check.instance.get('use_global_custom_queries', True)

Expand Down
19 changes: 18 additions & 1 deletion datadog_checks_base/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from datadog_checks.base import AgentCheck
from datadog_checks.base.stubs.aggregator import AggregatorStub
from datadog_checks.base.utils.db import QueryManager
from datadog_checks.base.utils.db import Query, QueryManager

pytestmark = pytest.mark.db

Expand Down Expand Up @@ -1043,6 +1043,23 @@ class MyCheck(AgentCheck):
query_manager2.queries[0]
), "QueryManager does not copy the queries"

def test_accept_query_objects(self):
class MyCheck(AgentCheck):
pass

check = MyCheck('test', {}, [{}])
dummy_query = {
'name': 'test query',
'query': 'foo',
'columns': [
{'name': 'test.foo', 'type': 'gauge', 'tags': ['override:ok']},
{'name': 'test.baz', 'type': 'gauge', 'raw': True},
],
'tags': ['test:bar'],
}
query_manager = QueryManager(check, mock_executor(), [Query(dummy_query)])
query_manager.compile_queries()

def test_query_execution_error(self, caplog, aggregator):
class Result(object):
def __init__(self, _):
Expand Down

0 comments on commit e82445b

Please sign in to comment.