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

Process query rows one at a time to reduce memory footprint #15268

Merged
merged 2 commits into from
Jul 17, 2023
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
9 changes: 5 additions & 4 deletions snowflake/datadog_checks/snowflake/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ def execute_query_raw(self, query):

if cursor.rowcount is None or cursor.rowcount < 1:
self.log.debug("Failed to fetch records from query: `%s`", query)
return []
return cursor.fetchall()
return
# Iterating on the cursor provides one row at a time without loading all of them at once
yield from cursor

def connect(self):
self.log.debug(
Expand Down Expand Up @@ -209,8 +210,8 @@ def connect(self):
@AgentCheck.metadata_entrypoint
def _collect_version(self):
try:
raw_version = self.execute_query_raw("select current_version();")
version = raw_version[0][0]
raw_version = next(self.execute_query_raw("select current_version();"))
version = raw_version[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

why is the second [0] not required here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because of the next right above. When using fetchall(), raw_version was a list of tuples, and thus we were accessing the first element of the first item in the list. With fetchone(), execute_query_raw is returning an iterator, for which we're taking the first element already with next. Thus, in the new version of the code, raw_version is a tuple from which we're only interested in the first element.

except Exception as e:
self.log.error("Error collecting version for Snowflake: %s", e)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re

import requests
from snowflake.connector.cursor import SnowflakeCursor

from . import tables

Expand Down Expand Up @@ -49,14 +50,20 @@ def execute(self, query):
if self.schema == 'ORGANIZATION_USAGE':
table_prefix = 'ORGANIZATION_'
table_attr = "{}{}".format(table_prefix, table_name)
self.__data = getattr(tables, table_attr, [])
self.__data = list(getattr(tables, table_attr, []))
elif query == 'select current_version();':
self.__data = [('4.30.2',)]
else:
self.__data = []

def fetchall(self):
return self.__data
def fetchone(self):
try:
return self.__data.pop(0)
except IndexError:
return None

def __iter__(self):
return SnowflakeCursor.__iter__(self)

def close(self):
pass
self.__data = []
2 changes: 1 addition & 1 deletion snowflake/tests/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_version_metadata(dd_run_check, instance, datadog_agent):
'version.raw': '4.30.2',
'version.scheme': 'semver',
}
with mock.patch('datadog_checks.snowflake.SnowflakeCheck.execute_query_raw', return_value=expected_version):
with mock.patch('datadog_checks.snowflake.SnowflakeCheck.execute_query_raw', return_value=iter(expected_version)):
check = SnowflakeCheck(CHECK_NAME, {}, [instance])
check.check_id = 'test:123'
check._conn = mock.MagicMock()
Expand Down