##// END OF EJS Templates
statsd: use safe mode and disable statsd if we can't connect
super-admin -
r1030:e536cb7d default
parent child Browse files
Show More
@@ -1,46 +1,52 b''
1 1 from __future__ import absolute_import, division, unicode_literals
2 2
3 3 import logging
4 4
5 5 from .stream import TCPStatsClient, UnixSocketStatsClient # noqa
6 6 from .udp import StatsClient # noqa
7 7
8 8 HOST = 'localhost'
9 9 PORT = 8125
10 10 IPV6 = False
11 11 PREFIX = None
12 12 MAXUDPSIZE = 512
13 13
14 14 log = logging.getLogger('rhodecode.statsd')
15 15
16 16
17 17 def statsd_config(config, prefix='statsd.'):
18 18 _config = {}
19 19 for key in config.keys():
20 20 if key.startswith(prefix):
21 21 _config[key[len(prefix):]] = config[key]
22 22 return _config
23 23
24 24
25 25 def client_from_config(configuration, prefix='statsd.', **kwargs):
26 26 from pyramid.settings import asbool
27 27
28 28 _config = statsd_config(configuration, prefix)
29 29 statsd_enabled = asbool(_config.pop('enabled', False))
30 30 if not statsd_enabled:
31 31 log.debug('statsd client not enabled by statsd.enabled = flag, skipping...')
32 32 return
33 33
34 34 host = _config.pop('statsd_host', HOST)
35 35 port = _config.pop('statsd_port', PORT)
36 36 prefix = _config.pop('statsd_prefix', PREFIX)
37 37 maxudpsize = _config.pop('statsd_maxudpsize', MAXUDPSIZE)
38 38 ipv6 = asbool(_config.pop('statsd_ipv6', IPV6))
39 39 log.debug('configured statsd client %s:%s', host, port)
40 40
41 return StatsClient(
42 host=host, port=port, prefix=prefix, maxudpsize=maxudpsize, ipv6=ipv6)
41 try:
42 client = StatsClient(
43 host=host, port=port, prefix=prefix, maxudpsize=maxudpsize, ipv6=ipv6)
44 except Exception:
45 log.exception('StatsD is enabled, but failed to connect to statsd server, fallback: disable statsd')
46 client = None
47
48 return client
43 49
44 50
45 51 def get_statsd_client(request):
46 52 return client_from_config(request.registry.settings)
General Comments 0
You need to be logged in to leave comments. Login now