-
Notifications
You must be signed in to change notification settings - Fork 94
/
flask_profiler.py
281 lines (220 loc) · 7.9 KB
/
flask_profiler.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
# -*- coding: utf8 -*-
import functools
import re
import time
from pprint import pprint as pp
import logging
from flask import Blueprint
from flask import jsonify
from flask import request
from flask_httpauth import HTTPBasicAuth
from . import storage
CONF = {}
collection = None
auth = HTTPBasicAuth()
logger = logging.getLogger("flask-profiler")
_is_initialized = lambda: True if CONF else False
@auth.verify_password
def verify_password(username, password):
if "basicAuth" not in CONF or not CONF["basicAuth"]["enabled"]:
return True
c = CONF["basicAuth"]
if username == c["username"] and password == c["password"]:
return True
logging.warn("flask-profiler authentication failed")
return False
class Measurement(object):
"""represents an endpoint measurement"""
DECIMAL_PLACES = 6
def __init__(self, name, args, kwargs, method, context=None):
super(Measurement, self).__init__()
self.context = context
self.name = name
self.method = method
self.args = args
self.kwargs = kwargs
self.startedAt = 0
self.endedAt = 0
self.elapsed = 0
def __json__(self):
return {
"name": self.name,
"args": self.args,
"kwargs": self.kwargs,
"method": self.method,
"startedAt": self.startedAt,
"endedAt": self.endedAt,
"elapsed": self.elapsed,
"context": self.context
}
def __str__(self):
return str(self.__json__())
def start(self):
# we use default_timer to get the best clock available.
# see: http://stackoverflow.com/a/25823885/672798
self.startedAt = time.time()
def stop(self):
self.endedAt = time.time()
self.elapsed = round(
self.endedAt - self.startedAt, self.DECIMAL_PLACES)
def is_ignored(name, conf):
ignore_patterns = conf.get("ignore", [])
for pattern in ignore_patterns:
if re.search(pattern, name):
return True
return False
def measure(f, name, method, context=None):
logger.debug("{0} is being processed.".format(name))
if is_ignored(name, CONF):
logger.debug("{0} is ignored.".format(name))
return f
@functools.wraps(f)
def wrapper(*args, **kwargs):
if 'sampling_function' in CONF and not callable(CONF['sampling_function']):
raise Exception(
"if sampling_function is provided to flask-profiler via config, "
"it must be callable, refer to: "
"https://github.com/muatik/flask-profiler#sampling")
if 'sampling_function' in CONF and not CONF['sampling_function']():
return f(*args, **kwargs)
measurement = Measurement(name, args, kwargs, method, context)
measurement.start()
try:
returnVal = f(*args, **kwargs)
except:
raise
finally:
measurement.stop()
if CONF.get("verbose", False):
pp(measurement.__json__())
collection.insert(measurement.__json__())
return returnVal
return wrapper
def wrapHttpEndpoint(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
context = {
"url": request.base_url,
"args": dict(request.args.items()),
"form": dict(request.form.items()),
"body": request.data.decode("utf-8", "strict"),
"headers": dict(request.headers.items()),
"func": request.endpoint,
"ip": request.remote_addr
}
endpoint_name = str(request.url_rule)
wrapped = measure(f, endpoint_name, request.method, context)
return wrapped(*args, **kwargs)
return wrapper
def wrapAppEndpoints(app):
"""
wraps all endpoints defined in the given flask app to measure how long time
each endpoints takes while being executed. This wrapping process is
supposed not to change endpoint behaviour.
:param app: Flask application instance
:return:
"""
for endpoint, func in app.view_functions.items():
app.view_functions[endpoint] = wrapHttpEndpoint(func)
def profile(*args, **kwargs):
"""
http endpoint decorator
"""
if _is_initialized():
def wrapper(f):
return wrapHttpEndpoint(f)
return wrapper
raise Exception(
"before measuring anything, you need to call init_app()")
def registerInternalRouters(app):
"""
These are the endpoints which are used to display measurements in the
flask-profiler dashboard.
Note: these should be defined after wrapping user defined endpoints
via wrapAppEndpoints()
:param app: Flask application instance
:return:
"""
urlPath = CONF.get("endpointRoot", "flask-profiler")
fp = Blueprint(
'flask-profiler', __name__,
url_prefix="/" + urlPath,
static_folder="static/dist/", static_url_path='/static/dist')
@fp.route("/".format(urlPath))
@auth.login_required
def index():
return fp.send_static_file("index.html")
@fp.route("/api/measurements/".format(urlPath))
@auth.login_required
def filterMeasurements():
args = dict(request.args.items())
measurements = collection.filter(args)
return jsonify({"measurements": list(measurements)})
@fp.route("/api/measurements/grouped".format(urlPath))
@auth.login_required
def getMeasurementsSummary():
args = dict(request.args.items())
measurements = collection.getSummary(args)
return jsonify({"measurements": list(measurements)})
@fp.route("/api/measurements/<measurementId>".format(urlPath))
@auth.login_required
def getContext(measurementId):
return jsonify(collection.get(measurementId))
@fp.route("/api/measurements/timeseries/".format(urlPath))
@auth.login_required
def getRequestsTimeseries():
args = dict(request.args.items())
return jsonify({"series": collection.getTimeseries(args)})
@fp.route("/api/measurements/methodDistribution/".format(urlPath))
@auth.login_required
def getMethodDistribution():
args = dict(request.args.items())
return jsonify({
"distribution": collection.getMethodDistribution(args)})
@fp.route("/db/dumpDatabase")
@auth.login_required
def dumpDatabase():
response = jsonify({
"summary": collection.getSummary()})
response.headers["Content-Disposition"] = "attachment; filename=dump.json"
return response
@fp.route("/db/deleteDatabase")
@auth.login_required
def deleteDatabase():
response = jsonify({
"status": collection.truncate()})
return response
@fp.after_request
def x_robots_tag_header(response):
response.headers['X-Robots-Tag'] = 'noindex, nofollow'
return response
app.register_blueprint(fp)
def init_app(app):
global collection, CONF
try:
CONF = app.config["flask_profiler"]
except:
try:
CONF = app.config["FLASK_PROFILER"]
except:
raise Exception(
"to init flask-profiler, provide "
"required config through flask app's config. please refer: "
"https://github.com/muatik/flask-profiler")
if not CONF.get("enabled", False):
return
collection = storage.getCollection(CONF.get("storage", {}))
wrapAppEndpoints(app)
registerInternalRouters(app)
basicAuth = CONF.get("basicAuth", None)
if not basicAuth or not basicAuth["enabled"]:
logging.warn(" * CAUTION: flask-profiler is working without basic auth!")
class Profiler(object):
""" Wrapper for extension. """
def __init__(self, app=None):
self._init_app = init_app
if app is not None:
self.init_app(app)
def init_app(self, app):
init = functools.partial(self._init_app, app)
app.before_first_request(init)