forked from zodb/perfmetrics
/
__init__.py
302 lines (231 loc) · 8.46 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""perfmetrics is a library for sending software performance metrics to statsd.
"""
from perfmetrics.clientstack import ClientStack
from perfmetrics.clientstack import client_stack
from perfmetrics.statsd import StatsdClient
from perfmetrics.statsd import StatsdClientMod
from perfmetrics.statsd import null_client
from time import time
import functools
import os
import random
import types
try: # pragma no cover
# Python 3
from urllib.parse import urlsplit
from urllib.parse import parse_qsl
from urllib.parse import uses_query
basestring = str # @ReservedAssignment
except ImportError: # pragma no cover
# Python 2
from urlparse import urlsplit
from urlparse import parse_qsl
from urlparse import uses_query
__all__ = ['metric',
'Metric',
'set_statsd_client',
'statsd_client',
'statsd_client_from_uri',
'statsd_client_stack',
]
statsd_client_stack = client_stack
def statsd_client():
"""Return the current StatsdClient for the thread.
Defaults to the global client set by ``set_statsd_client``.
"""
return statsd_client_stack.get()
def set_statsd_client(client_or_uri):
"""Set the global StatsdClient.
Accepts either a StatsdClient, a Statsd URI, or None (to clear the
global client).
"""
if isinstance(client_or_uri, basestring):
client = statsd_client_from_uri(client_or_uri)
else:
client = client_or_uri
ClientStack.default = client
if 'statsd' not in uses_query: # pragma: no cover
uses_query.append('statsd')
def statsd_client_from_uri(uri):
"""Create and return StatsdClient.
A typical URI is ``statsd://localhost:8125``. An optional
query parameter is ``prefix``. The default prefix is an empty string.
"""
parts = urlsplit(uri)
if parts.scheme == 'statsd':
kw = {}
if parts.query:
kw.update(parse_qsl(parts.query))
return StatsdClient(parts.hostname, parts.port, **kw)
else:
raise ValueError("URI scheme not supported: %s" % uri)
class Metric(object):
"""Make metric decorator/context managers.
Examples:
@Metric('some.func')
def somefunc():
...
@metric
def somefunc():
...
"""
def __init__(self, stat=None, rate=1, method=False,
count=True, timing=True, timing_format='%s.t',
random=random.random): # testing hook
self.stat = stat
self.rate = rate
self.method = method
self.count = count
self.timing = timing
self.timing_format = timing_format
self.random = random
def __call__(self, f):
"""Decorate a function or method so it can send statistics to statsd.
"""
func_name = f.__name__
func_full_name = '%s.%s' % (f.__module__, func_name)
instance_stat = self.stat
rate = self.rate
method = self.method
count = self.count
timing = self.timing
timing_format = self.timing_format
random = self.random
def call_with_metric(*args, **kw):
if rate < 1 and random() >= rate:
# Ignore this sample.
return f(*args, **kw)
stack = statsd_client_stack.stack
client = stack[-1] if stack else client_stack.default
if client is None:
# No statsd client has been configured.
return f(*args, **kw)
if instance_stat:
stat = instance_stat
elif method:
if isinstance(args[0], (type, types.ClassType)):
cls = args[0]
else:
cls = args[0].__class__
stat = '%s.%s.%s' % (cls.__module__, cls.__name__, func_name)
else:
stat = func_full_name
if timing:
if count:
buf = []
client.incr(stat, 1, rate, buf=buf, rate_applied=True)
else:
buf = None
start = time()
try:
return f(*args, **kw)
finally:
elapsed_ms = int((time() - start) * 1000.0)
client.timing(timing_format % stat, elapsed_ms,
rate, buf=buf, rate_applied=True)
if buf:
client.sendbuf(buf)
else:
if count:
client.incr(stat, 1, rate, rate_applied=True)
return f(*args, **kw)
return functools.update_wrapper(call_with_metric, f)
# Metric can also be used as a context manager.
def __enter__(self):
self.start = time()
def __exit__(self, _typ, _value, _tb):
rate = self.rate
if rate < 1 and self.random() >= rate:
# Ignore this sample.
return
stack = statsd_client_stack.stack
client = stack[-1] if stack else client_stack.default
if client is not None:
buf = []
stat = self.stat
if stat:
if self.count:
client.incr(stat, rate=rate, buf=buf, rate_applied=True)
if self.timing:
elapsed = int((time() - self.start) * 1000.0)
client.timing(self.timing_format % stat, elapsed,
rate=rate, buf=buf, rate_applied=True)
if buf:
client.sendbuf(buf)
# 'metric' is a function decorator with default options.
metric = Metric()
# 'metricmethod' is a method decorator with default options.
metricmethod = Metric(method=True)
class MetricMod(object):
"""Decorator/context manager that modifies the name of metrics in context.
format is a format string such as 'XYZ.%s'.
"""
def __init__(self, format):
self.format = format
def __call__(self, f):
"""Decorate a function or method to add a metric prefix in context.
"""
def call_with_mod(*args, **kw):
stack = statsd_client_stack.stack
client = stack[-1] if stack else client_stack.default
if client is None:
# Statsd is not configured.
return f(*args, **kw)
stack.append(StatsdClientMod(client, self.format))
try:
return f(*args, **kw)
finally:
stack.pop()
return functools.update_wrapper(call_with_mod, f)
def __enter__(self):
stack = statsd_client_stack.stack
client = stack[-1] if stack else client_stack.default
if client is None:
stack.append(null_client)
else:
stack.append(StatsdClientMod(client, self.format))
def __exit__(self, _typ, _value, _tb):
stack = statsd_client_stack.stack
stack.pop()
_uri = os.environ.get('STATSD_URI')
if _uri:
set_statsd_client(_uri) # pragma no cover
#==============================================================================
# Optional Pyramid integration
#==============================================================================
def includeme(config):
"""Pyramid configuration hook: activate the perfmetrics tween.
A statsd_uri should be in the settings.
"""
if config.registry.settings.get('statsd_uri'):
config.add_tween('perfmetrics.tween')
def tween(handler, registry):
"""Pyramid tween that sets up a Statsd client for each request.
"""
uri = registry.settings['statsd_uri']
client = statsd_client_from_uri(uri)
def handle(request):
statsd_client_stack.push(client)
try:
with Metric('perfmetrics.tween'):
return handler(request)
finally:
statsd_client_stack.pop()
return handle
#==============================================================================
# Optional WSGI integration
#==============================================================================
def make_statsd_app(nextapp, _globals=None, statsd_uri=''):
"""Create a WSGI filter app that sets up Statsd for each request."""
if not statsd_uri:
# Disabled.
return nextapp
client = statsd_client_from_uri(statsd_uri)
def app(environ, start_response):
statsd_client_stack.push(client)
try:
with Metric('perfmetrics.wsgi'):
return nextapp(environ, start_response)
finally:
statsd_client_stack.pop()
return app