Show More
@@ -1,46 +1,52 b'' | |||||
1 | from __future__ import absolute_import, division, unicode_literals |
|
1 | from __future__ import absolute_import, division, unicode_literals | |
2 |
|
2 | |||
3 | import logging |
|
3 | import logging | |
4 |
|
4 | |||
5 | from .stream import TCPStatsClient, UnixSocketStatsClient # noqa |
|
5 | from .stream import TCPStatsClient, UnixSocketStatsClient # noqa | |
6 | from .udp import StatsClient # noqa |
|
6 | from .udp import StatsClient # noqa | |
7 |
|
7 | |||
8 | HOST = 'localhost' |
|
8 | HOST = 'localhost' | |
9 | PORT = 8125 |
|
9 | PORT = 8125 | |
10 | IPV6 = False |
|
10 | IPV6 = False | |
11 | PREFIX = None |
|
11 | PREFIX = None | |
12 | MAXUDPSIZE = 512 |
|
12 | MAXUDPSIZE = 512 | |
13 |
|
13 | |||
14 | log = logging.getLogger('rhodecode.statsd') |
|
14 | log = logging.getLogger('rhodecode.statsd') | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | def statsd_config(config, prefix='statsd.'): |
|
17 | def statsd_config(config, prefix='statsd.'): | |
18 | _config = {} |
|
18 | _config = {} | |
19 | for key in config.keys(): |
|
19 | for key in config.keys(): | |
20 | if key.startswith(prefix): |
|
20 | if key.startswith(prefix): | |
21 | _config[key[len(prefix):]] = config[key] |
|
21 | _config[key[len(prefix):]] = config[key] | |
22 | return _config |
|
22 | return _config | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | def client_from_config(configuration, prefix='statsd.', **kwargs): |
|
25 | def client_from_config(configuration, prefix='statsd.', **kwargs): | |
26 | from pyramid.settings import asbool |
|
26 | from pyramid.settings import asbool | |
27 |
|
27 | |||
28 | _config = statsd_config(configuration, prefix) |
|
28 | _config = statsd_config(configuration, prefix) | |
29 | statsd_enabled = asbool(_config.pop('enabled', False)) |
|
29 | statsd_enabled = asbool(_config.pop('enabled', False)) | |
30 | if not statsd_enabled: |
|
30 | if not statsd_enabled: | |
31 | log.debug('statsd client not enabled by statsd.enabled = flag, skipping...') |
|
31 | log.debug('statsd client not enabled by statsd.enabled = flag, skipping...') | |
32 | return |
|
32 | return | |
33 |
|
33 | |||
34 | host = _config.pop('statsd_host', HOST) |
|
34 | host = _config.pop('statsd_host', HOST) | |
35 | port = _config.pop('statsd_port', PORT) |
|
35 | port = _config.pop('statsd_port', PORT) | |
36 | prefix = _config.pop('statsd_prefix', PREFIX) |
|
36 | prefix = _config.pop('statsd_prefix', PREFIX) | |
37 | maxudpsize = _config.pop('statsd_maxudpsize', MAXUDPSIZE) |
|
37 | maxudpsize = _config.pop('statsd_maxudpsize', MAXUDPSIZE) | |
38 | ipv6 = asbool(_config.pop('statsd_ipv6', IPV6)) |
|
38 | ipv6 = asbool(_config.pop('statsd_ipv6', IPV6)) | |
39 | log.debug('configured statsd client %s:%s', host, port) |
|
39 | log.debug('configured statsd client %s:%s', host, port) | |
40 |
|
40 | |||
41 | return StatsClient( |
|
41 | try: | |
|
42 | client = StatsClient( | |||
42 | host=host, port=port, prefix=prefix, maxudpsize=maxudpsize, ipv6=ipv6) |
|
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 | def get_statsd_client(request): |
|
51 | def get_statsd_client(request): | |
46 | return client_from_config(request.registry.settings) |
|
52 | return client_from_config(request.registry.settings) |
General Comments 0
You need to be logged in to leave comments.
Login now