Show More
@@ -135,10 +135,10 b' def make_pyramid_app(global_config, **se' | |||||
135 | log.info('Pyramid app `%s` created and configured in %.2fs', |
|
135 | log.info('Pyramid app `%s` created and configured in %.2fs', | |
136 | pyramid_app.func_name, total_time) |
|
136 | pyramid_app.func_name, total_time) | |
137 | if statsd: |
|
137 | if statsd: | |
138 | elapsed_time_ms = 1000.0 * total_time |
|
138 | elapsed_time_ms = round(1000.0 * total_time) # use ms only rounded time | |
139 | statsd.timing('rhodecode_app_bootstrap_timing', elapsed_time_ms, tags=[ |
|
139 | statsd.timing('rhodecode_app_bootstrap_timing', elapsed_time_ms, tags=[ | |
140 | "pyramid_app:{}".format(pyramid_app.func_name) |
|
140 | "pyramid_app:{}".format(pyramid_app.func_name) | |
141 | ]) |
|
141 | ], use_decimals=False) | |
142 | return pyramid_app |
|
142 | return pyramid_app | |
143 |
|
143 | |||
144 |
|
144 |
@@ -14,6 +14,12 b' TAG_INVALID_CHARS_RE = re.compile(' | |||||
14 | ) |
|
14 | ) | |
15 | TAG_INVALID_CHARS_SUBS = "_" |
|
15 | TAG_INVALID_CHARS_SUBS = "_" | |
16 |
|
16 | |||
|
17 | # we save and expose methods called by statsd for discovery | |||
|
18 | stat_dict = { | |||
|
19 | ||||
|
20 | } | |||
|
21 | ||||
|
22 | ||||
17 |
|
23 | |||
18 | @lru_cache(maxsize=500) |
|
24 | @lru_cache(maxsize=500) | |
19 | def _normalize_tags_with_cache(tag_list): |
|
25 | def _normalize_tags_with_cache(tag_list): | |
@@ -42,7 +48,7 b' class StatsClientBase(object):' | |||||
42 | def timer(self, stat, rate=1, tags=None): |
|
48 | def timer(self, stat, rate=1, tags=None): | |
43 | return Timer(self, stat, rate, tags) |
|
49 | return Timer(self, stat, rate, tags) | |
44 |
|
50 | |||
45 | def timing(self, stat, delta, rate=1, tags=None): |
|
51 | def timing(self, stat, delta, rate=1, tags=None, use_decimals=True): | |
46 | """ |
|
52 | """ | |
47 | Send new timing information. |
|
53 | Send new timing information. | |
48 |
|
54 | |||
@@ -51,7 +57,11 b' class StatsClientBase(object):' | |||||
51 | if isinstance(delta, timedelta): |
|
57 | if isinstance(delta, timedelta): | |
52 | # Convert timedelta to number of milliseconds. |
|
58 | # Convert timedelta to number of milliseconds. | |
53 | delta = delta.total_seconds() * 1000. |
|
59 | delta = delta.total_seconds() * 1000. | |
54 | self._send_stat(stat, '%0.6f|ms' % delta, rate, tags) |
|
60 | if use_decimals: | |
|
61 | fmt = '%0.6f|ms' | |||
|
62 | else: | |||
|
63 | fmt = '%s|ms' | |||
|
64 | self._send_stat(stat, fmt % delta, rate, tags) | |||
55 |
|
65 | |||
56 | def incr(self, stat, count=1, rate=1, tags=None): |
|
66 | def incr(self, stat, count=1, rate=1, tags=None): | |
57 | """Increment a stat by `count`.""" |
|
67 | """Increment a stat by `count`.""" | |
@@ -78,10 +88,17 b' class StatsClientBase(object):' | |||||
78 | """Set a set value.""" |
|
88 | """Set a set value.""" | |
79 | self._send_stat(stat, '%s|s' % value, rate) |
|
89 | self._send_stat(stat, '%s|s' % value, rate) | |
80 |
|
90 | |||
|
91 | def histogram(self, stat, value, rate=1, tags=None): | |||
|
92 | """Set a histogram""" | |||
|
93 | self._send_stat(stat, '%s|h' % value, rate, tags) | |||
|
94 | ||||
81 | def _send_stat(self, stat, value, rate, tags=None): |
|
95 | def _send_stat(self, stat, value, rate, tags=None): | |
82 | self._after(self._prepare(stat, value, rate, tags)) |
|
96 | self._after(self._prepare(stat, value, rate, tags)) | |
83 |
|
97 | |||
84 | def _prepare(self, stat, value, rate, tags=None): |
|
98 | def _prepare(self, stat, value, rate, tags=None): | |
|
99 | global stat_dict | |||
|
100 | stat_dict[stat] = 1 | |||
|
101 | ||||
85 | if rate < 1: |
|
102 | if rate < 1: | |
86 | if random.random() > rate: |
|
103 | if random.random() > rate: | |
87 | return |
|
104 | return |
@@ -21,7 +21,7 b' def safe_wraps(wrapper, *args, **kwargs)' | |||||
21 | class Timer(object): |
|
21 | class Timer(object): | |
22 | """A context manager/decorator for statsd.timing().""" |
|
22 | """A context manager/decorator for statsd.timing().""" | |
23 |
|
23 | |||
24 | def __init__(self, client, stat, rate=1, tags=None): |
|
24 | def __init__(self, client, stat, rate=1, tags=None, use_decimals=True): | |
25 | self.client = client |
|
25 | self.client = client | |
26 | self.stat = stat |
|
26 | self.stat = stat | |
27 | self.rate = rate |
|
27 | self.rate = rate | |
@@ -29,6 +29,7 b' class Timer(object):' | |||||
29 | self.ms = None |
|
29 | self.ms = None | |
30 | self._sent = False |
|
30 | self._sent = False | |
31 | self._start_time = None |
|
31 | self._start_time = None | |
|
32 | self.use_decimals = use_decimals | |||
32 |
|
33 | |||
33 | def __call__(self, f): |
|
34 | def __call__(self, f): | |
34 | """Thread-safe timing function decorator.""" |
|
35 | """Thread-safe timing function decorator.""" | |
@@ -39,7 +40,7 b' class Timer(object):' | |||||
39 | return f(*args, **kwargs) |
|
40 | return f(*args, **kwargs) | |
40 | finally: |
|
41 | finally: | |
41 | elapsed_time_ms = 1000.0 * (time_now() - start_time) |
|
42 | elapsed_time_ms = 1000.0 * (time_now() - start_time) | |
42 | self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags) |
|
43 | self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals) | |
43 | return _wrapped |
|
44 | return _wrapped | |
44 |
|
45 | |||
45 | def __enter__(self): |
|
46 | def __enter__(self): | |
@@ -69,4 +70,4 b' class Timer(object):' | |||||
69 | if self._sent: |
|
70 | if self._sent: | |
70 | raise RuntimeError('Already sent data.') |
|
71 | raise RuntimeError('Already sent data.') | |
71 | self._sent = True |
|
72 | self._sent = True | |
72 | self.client.timing(self.stat, self.ms, self.rate) |
|
73 | self.client.timing(self.stat, self.ms, self.rate, self.tags, self.use_decimals) |
@@ -65,14 +65,15 b' class RequestWrapperTween(object):' | |||||
65 | if statsd: |
|
65 | if statsd: | |
66 | match_route = request.matched_route.name if request.matched_route else _path |
|
66 | match_route = request.matched_route.name if request.matched_route else _path | |
67 | resp_code = response.status_code |
|
67 | resp_code = response.status_code | |
68 | elapsed_time_ms = 1000.0 * total |
|
68 | elapsed_time_ms = round(1000.0 * total) # use ms only | |
69 | statsd.timing( |
|
69 | statsd.timing( | |
70 | 'rhodecode_req_timing', elapsed_time_ms, |
|
70 | 'rhodecode_req_timing', elapsed_time_ms, | |
71 | tags=[ |
|
71 | tags=[ | |
72 | "view_name:{}".format(match_route), |
|
72 | "view_name:{}".format(match_route), | |
73 | #"user:{}".format(user_id), |
|
73 | #"user:{}".format(user_id), | |
74 | "code:{}".format(resp_code) |
|
74 | "code:{}".format(resp_code) | |
75 | ] |
|
75 | ], | |
|
76 | use_decimals=False | |||
76 | ) |
|
77 | ) | |
77 | statsd.incr( |
|
78 | statsd.incr( | |
78 | 'rhodecode_req_total', tags=[ |
|
79 | 'rhodecode_req_total', tags=[ |
General Comments 0
You need to be logged in to leave comments.
Login now