Skip to content

Commit 4cba70a

Browse files
scottgearydegemer
authored andcommitted
[mysql] Allow connection_timeout to be set (#2729)
* 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
1 parent 7d2bf9a commit 4cba70a

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

checks.d/mysql.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def check(self, instance):
294294
procfs_path = self.agentConfig.get('procfs_path', '/proc').rstrip('/')
295295
psutil.PROCFS_PATH = procfs_path
296296

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

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

305305
with self._connect(host, port, mysql_sock, user,
306-
password, defaults_file, ssl) as db:
306+
password, defaults_file, ssl, connect_timeout) as db:
307307
try:
308308
# Metadata collection
309309
self._collect_metadata(db, host)
@@ -330,9 +330,10 @@ def _get_config(self, instance):
330330
options = instance.get('options', {})
331331
queries = instance.get('queries', [])
332332
ssl = instance.get('ssl', {})
333+
connect_timeout = instance.get('connect_timeout', None)
333334

334335
return (self.host, self.port, user, password, self.mysql_sock,
335-
self.defaults_file, tags, options, queries, ssl)
336+
self.defaults_file, tags, options, queries, ssl, connect_timeout)
336337

337338
def _set_qcache_stats(self):
338339
host_key = self._get_host_key()
@@ -363,7 +364,7 @@ def _get_host_key(self):
363364
return hostkey
364365

365366
@contextmanager
366-
def _connect(self, host, port, mysql_sock, user, password, defaults_file, ssl):
367+
def _connect(self, host, port, mysql_sock, user, password, defaults_file, ssl, connect_timeout):
367368
self.service_check_tags = [
368369
'server:%s' % (mysql_sock if mysql_sock != '' else host),
369370
'port:%s' % ('unix_socket' if port == 0 else port)
@@ -374,7 +375,11 @@ def _connect(self, host, port, mysql_sock, user, password, defaults_file, ssl):
374375
ssl = dict(ssl) if ssl else None
375376

376377
if defaults_file != '':
377-
db = pymysql.connect(read_default_file=defaults_file, ssl=ssl)
378+
db = pymysql.connect(
379+
read_default_file=defaults_file,
380+
ssl=ssl,
381+
connect_timeout=connect_timeout
382+
)
378383
elif mysql_sock != '':
379384
self.service_check_tags = [
380385
'server:{0}'.format(mysql_sock),
@@ -383,22 +388,25 @@ def _connect(self, host, port, mysql_sock, user, password, defaults_file, ssl):
383388
db = pymysql.connect(
384389
unix_socket=mysql_sock,
385390
user=user,
386-
passwd=password
391+
passwd=password,
392+
connect_timeout=connect_timeout
387393
)
388394
elif port:
389395
db = pymysql.connect(
390396
host=host,
391397
port=port,
392398
user=user,
393399
passwd=password,
394-
ssl=ssl
400+
ssl=ssl,
401+
connect_timeout=connect_timeout
395402
)
396403
else:
397404
db = pymysql.connect(
398405
host=host,
399406
user=user,
400407
passwd=password,
401-
ssl=ssl
408+
ssl=ssl,
409+
connect_timeout=connect_timeout
402410
)
403411
self.log.debug("Connected to MySQL")
404412
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,

conf.d/mysql.yaml.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ instances:
77
# port: 3306 # Optional
88
# sock: /path/to/sock # Connect via Unix Socket
99
# defaults_file: my.cnf # Alternate configuration mechanism
10+
# connect_timeout: None # Optional integer seconds
1011
# tags: # Optional
1112
# - optional_tag1
1213
# - optional_tag2

0 commit comments

Comments
 (0)