Skip to content

Commit

Permalink
SNOW-74772: Added log_max_query_length to adjust a query lenght in the
Browse files Browse the repository at this point in the history
log
  • Loading branch information
smtakeda authored and ankit-bhatnagar167 committed Apr 22, 2019
1 parent 42f55ee commit 8230e9b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
40 changes: 27 additions & 13 deletions connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
PARAMETER_CLIENT_STORE_TEMPORARY_CREDENTIAL,
PARAMETER_CLIENT_PREFETCH_THREADS,
)
from .cursor import SnowflakeCursor
from .cursor import SnowflakeCursor, LOG_MAX_QUERY_LENGTH
from .errorcode import (ER_CONNECTION_IS_CLOSED,
ER_NO_ACCOUNT_NAME, ER_OLD_PYTHON, ER_NO_USER,
ER_NO_PASSWORD, ER_INVALID_VALUE,
Expand Down Expand Up @@ -131,7 +131,8 @@ def DefaultConverterClass():
u'timezone': None, # snowflake
u'consent_cache_id_token': True, # snowflake
u'service_name': None, # snowflake,
u'support_negative_year': True # snowflake
u'support_negative_year': True, # snowflake
u'log_max_query_length': LOG_MAX_QUERY_LENGTH, # snowflake
}

APPLICATION_RE = re.compile(r'[\w\d_]+')
Expand Down Expand Up @@ -427,6 +428,10 @@ def service_name(self):
def service_name(self, value):
self._service_name = value

@property
def log_max_query_length(self):
return self._log_max_query_length

def connect(self, **kwargs):
u"""
Connects to the database
Expand Down Expand Up @@ -585,24 +590,30 @@ def __open_connection(self):
self.port)

if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' in os.environ:
logger.debug(u"Custom OCSP Cache Server URL found in environment - %s", os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'])
logger.debug(
u"Custom OCSP Cache Server URL found in environment - %s",
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'])

if self.host.endswith(u".privatelink.snowflakecomputing.com"):
ocsp_cache_server = \
u'http://ocsp{}/ocsp_response_cache.json'.format(
self.host[self.host.index('.'):])
if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' not in os.environ:
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
os.environ[
'SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
else:
if not os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'].\
if not os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL']. \
startswith("http://"):
ocsp_cache_server = "http://{0}/{1}".format(os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
"ocsp_response_cache.json")
ocsp_cache_server = "http://{0}/{1}".format(
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
"ocsp_response_cache.json")
else:
ocsp_cache_server = "{0}/{1}".format(os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
"ocsp_response_cache.json")
ocsp_cache_server = "{0}/{1}".format(
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
"ocsp_response_cache.json")

os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
os.environ[
'SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
logger.debug(u"OCSP Cache Server is updated: %s", ocsp_cache_server)
else:
if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' in os.environ:
Expand Down Expand Up @@ -783,9 +794,7 @@ def cmd_query(self, sql, sequence_counter, request_id,
if logger.getEffectiveLevel() <= logging.DEBUG:
logger.debug(
u'sql=[%s], sequence_id=[%s], is_file_transfer=[%s]',
u' '.join(
line.strip() for line in
data[u'sqlText'].split(u'\n')),
self._format_query_for_log(data[u'sqlText']),
data[u'sequenceId'],
is_file_transfer
)
Expand Down Expand Up @@ -1091,6 +1100,11 @@ def _set_parameters(self, ret, session_parameters):
elif PARAMETER_CLIENT_PREFETCH_THREADS == name:
self.client_prefetch_threads = value

def _format_query_for_log(self, query):
ret = u' '.join(line.strip() for line in query.split(u'\n'))
return (ret if len(ret) < self.log_max_query_length
else ret[0:self.log_max_query_length] + '...')

def __enter__(self):
u"""
context manager
Expand Down
21 changes: 10 additions & 11 deletions cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

logger = getLogger(__name__)

LOG_MAX_QUERY_LENGTH = 80


class SnowflakeCursor(object):
u"""
Expand All @@ -65,8 +67,6 @@ class SnowflakeCursor(object):
u(r'alter\s+session\s+set\s+(.*)=\'?([^\']+)\'?\s*;'),
flags=re.IGNORECASE | re.MULTILINE | re.DOTALL)

LOG_MAX_QUERY_LENGTH = 80

def __init__(self, connection):
self._connection = connection

Expand Down Expand Up @@ -97,6 +97,8 @@ def __init__(self, connection):

self._first_chunk_time = None

self._log_max_query_length = connection.log_max_query_length

self.reset()

def __del__(self):
Expand Down Expand Up @@ -303,9 +305,7 @@ def _execute_helper(

if logger.getEffectiveLevel() <= logging.DEBUG:
logger.debug(
u'running query [%s]',
u' '.join(line.strip() for line in query.split(u'\n')),
)
u'running query [%s]', self._format_query_for_log(query))
if _is_put_get is not None:
# if told the query is PUT or GET, use the information
self._is_file_transfer = _is_put_get
Expand Down Expand Up @@ -441,9 +441,10 @@ def execute(self, command, params=None, timeout=None,
# pyformat/format paramstyle
# client side binding
processed_params = self._connection._process_params(params, self)
logger.debug(u'binding: %s with input=%s, processed=%s',
command,
params, processed_params)
if logger.getEffectiveLevel() <= logging.DEBUG:
logger.debug(u'binding: [%s] with input=[%s], processed=[%s]',
self._format_query_for_log(command),
params, processed_params)
if len(processed_params) > 0:
query = command % processed_params
else:
Expand Down Expand Up @@ -551,9 +552,7 @@ def execute(self, command, params=None, timeout=None,
return self

def _format_query_for_log(self, query):
ret = u' '.join(line.strip() for line in query.split(u'\n'))
return (ret if len(ret) < SnowflakeCursor.LOG_MAX_QUERY_LENGTH
else ret[0:SnowflakeCursor.LOG_MAX_QUERY_LENGTH] + '...')
return self._connection._format_query_for_log(query)

def _is_dml(self, data):
return u'statementTypeId' in data \
Expand Down

0 comments on commit 8230e9b

Please sign in to comment.