Skip to content

Commit

Permalink
[mysql] Allow connection_timeout to be set (#2729)
Browse files Browse the repository at this point in the history
* Allow connection_timeout to be set for pymysql instances

Defaults to the original 'None' behaviour: falling back to OS socket defaults on sock.settimeout()

* Pass in connect_timeout when using default file too
  • Loading branch information
scottgeary authored and degemer committed Aug 8, 2016
1 parent 7d2bf9a commit 4cba70a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
24 changes: 16 additions & 8 deletions checks.d/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def check(self, instance):
procfs_path = self.agentConfig.get('procfs_path', '/proc').rstrip('/')
psutil.PROCFS_PATH = procfs_path

host, port, user, password, mysql_sock, defaults_file, tags, options, queries, ssl = \
host, port, user, password, mysql_sock, defaults_file, tags, options, queries, ssl, connect_timeout = \
self._get_config(instance)

self._set_qcache_stats()
Expand All @@ -303,7 +303,7 @@ def check(self, instance):
raise Exception("Mysql host and user are needed.")

with self._connect(host, port, mysql_sock, user,
password, defaults_file, ssl) as db:
password, defaults_file, ssl, connect_timeout) as db:
try:
# Metadata collection
self._collect_metadata(db, host)
Expand All @@ -330,9 +330,10 @@ def _get_config(self, instance):
options = instance.get('options', {})
queries = instance.get('queries', [])
ssl = instance.get('ssl', {})
connect_timeout = instance.get('connect_timeout', None)

return (self.host, self.port, user, password, self.mysql_sock,
self.defaults_file, tags, options, queries, ssl)
self.defaults_file, tags, options, queries, ssl, connect_timeout)

def _set_qcache_stats(self):
host_key = self._get_host_key()
Expand Down Expand Up @@ -363,7 +364,7 @@ def _get_host_key(self):
return hostkey

@contextmanager
def _connect(self, host, port, mysql_sock, user, password, defaults_file, ssl):
def _connect(self, host, port, mysql_sock, user, password, defaults_file, ssl, connect_timeout):
self.service_check_tags = [
'server:%s' % (mysql_sock if mysql_sock != '' else host),
'port:%s' % ('unix_socket' if port == 0 else port)
Expand All @@ -374,7 +375,11 @@ def _connect(self, host, port, mysql_sock, user, password, defaults_file, ssl):
ssl = dict(ssl) if ssl else None

if defaults_file != '':
db = pymysql.connect(read_default_file=defaults_file, ssl=ssl)
db = pymysql.connect(
read_default_file=defaults_file,
ssl=ssl,
connect_timeout=connect_timeout
)
elif mysql_sock != '':
self.service_check_tags = [
'server:{0}'.format(mysql_sock),
Expand All @@ -383,22 +388,25 @@ def _connect(self, host, port, mysql_sock, user, password, defaults_file, ssl):
db = pymysql.connect(
unix_socket=mysql_sock,
user=user,
passwd=password
passwd=password,
connect_timeout=connect_timeout
)
elif port:
db = pymysql.connect(
host=host,
port=port,
user=user,
passwd=password,
ssl=ssl
ssl=ssl,
connect_timeout=connect_timeout
)
else:
db = pymysql.connect(
host=host,
user=user,
passwd=password,
ssl=ssl
ssl=ssl,
connect_timeout=connect_timeout
)
self.log.debug("Connected to MySQL")
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,
Expand Down
1 change: 1 addition & 0 deletions conf.d/mysql.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ instances:
# port: 3306 # Optional
# sock: /path/to/sock # Connect via Unix Socket
# defaults_file: my.cnf # Alternate configuration mechanism
# connect_timeout: None # Optional integer seconds
# tags: # Optional
# - optional_tag1
# - optional_tag2
Expand Down

0 comments on commit 4cba70a

Please sign in to comment.