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

Allow back passing a list of Query objects #7979

Closed
wants to merge 1 commit into from
Closed
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
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