-
Notifications
You must be signed in to change notification settings - Fork 31
/
__init__.py
298 lines (256 loc) · 9.89 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
# Copyright (c) 2013-2018 Steve Milner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# (1) Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# (2) Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# (3)The name of the author may not be used to
# endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""
Basic metrics tracking with Flask.
"""
import datetime
import json
import time
from six.moves.urllib_parse import quote_plus
from six.moves.urllib.request import urlopen
import six
from flask import _request_ctx_stack, g
__version__ = '2.0.0'
__author__ = 'Steve Milner'
__license__ = 'MBSD'
class TrackUsage(object):
"""
Tracks basic usage of Flask applications.
"""
def __init__(self, app=None, storage=None, _fake_time=None):
"""
Create the instance.
:Parameters:
- `app`: Optional app to use.
- `storage`: If app is set, required list of storage callables.
"""
#
# `_fake_time` is to force the time stamp of the request for testing
# purposes. It is not normally used by end users. Must be a native
# datetime object.
#
self._exclude_views = set()
self._include_views = set()
if callable(storage):
storage = [storage]
self._fake_time = _fake_time
if app is not None and storage is not None:
self.init_app(app, storage)
def init_app(self, app, storage):
"""
Initialize the instance with the app.
:Parameters:
- `app`: Application to work with.
- `storage`: The storage callable which will store result.
"""
self.app = app
self._storages = storage
self._use_freegeoip = app.config.get(
'TRACK_USAGE_USE_FREEGEOIP', False)
self._freegeoip_endpoint = app.config.get(
'TRACK_USAGE_FREEGEOIP_ENDPOINT',
"http://extreme-ip-lookup.com/json/{ip}"
)
self._type = app.config.get(
'TRACK_USAGE_INCLUDE_OR_EXCLUDE_VIEWS', 'exclude')
if self._type not in ('include', 'exclude'):
raise NotImplementedError(
'You must set include or exclude type.')
app.before_request(self.before_request)
app.after_request(self.after_request)
def before_request(self):
"""
Done before every request that is in scope.
"""
ctx = _request_ctx_stack.top
view_func = self.app.view_functions.get(ctx.request.endpoint)
if self._type == 'exclude':
if view_func in self._exclude_views:
return
elif self._type == 'include':
if view_func not in self._include_views:
return
else:
raise NotImplementedError(
'You must set include or exclude type.')
g.start_time = datetime.datetime.utcnow()
if not hasattr(g, "track_var"):
g.track_var = {}
def after_request(self, response):
"""
The heavy lifter. This method collects the majority of data
and passes it off for storage.
:Parameters:
- `response`: The response on it's way to the client.
"""
ctx = _request_ctx_stack.top
view_func = self.app.view_functions.get(ctx.request.endpoint)
if self._type == 'exclude':
if view_func in self._exclude_views:
return response
elif self._type == 'include':
if view_func not in self._include_views:
return response
else:
raise NotImplementedError(
'You must set include or exclude type.')
now = datetime.datetime.utcnow()
speed = None
try:
speed = (now - g.start_time).total_seconds()
except:
# Older python versions don't have total_seconds()
speed_result = (now - g.start_time)
speed = float("%s.%s" % (
speed_result.seconds, speed_result.microseconds))
if self._fake_time:
current_time = self._fake_time
else:
current_time = now
data = {
'url': ctx.request.url,
'user_agent': ctx.request.user_agent,
'server_name': ctx.app.name,
'blueprint': ctx.request.blueprint,
'view_args': ctx.request.view_args,
'status': response.status_code,
'remote_addr': ctx.request.remote_addr,
'xforwardedfor': ctx.request.headers.get(
'X-Forwarded-For', None),
'authorization': bool(ctx.request.authorization),
'ip_info': None,
'path': ctx.request.path,
'speed': float(speed),
'date': int(time.mktime(current_time.timetuple())),
'content_length': response.content_length,
'request': "{} {} {}".format(
ctx.request.method,
ctx.request.url,
ctx.request.environ.get('SERVER_PROTOCOL')
),
'url_args': dict(
[(k, ctx.request.args[k]) for k in ctx.request.args]
),
'username': None,
'track_var': g.track_var
}
if ctx.request.authorization:
data['username'] = str(ctx.request.authorization.username)
if self._use_freegeoip:
clean_ip = quote_plus(str(ctx.request.remote_addr))
if '{ip}' in self._freegeoip_endpoint:
url = self._freegeoip_endpoint.format(ip=clean_ip)
else:
url = self._freegeoip_endpoint + clean_ip
# seperate capture and conversion to aid in debugging
text = urlopen(url).read()
ip_info = json.loads(text)
if url.startswith("http://extreme-ip-lookup.com/"):
del ip_info["businessWebsite"]
del ip_info["status"]
data['ip_info'] = ip_info
for storage in self._storages:
storage(data)
return response
def exclude(self, view):
"""
Excludes a view from tracking if we are in exclude mode.
:Parameters:
- `view`: The view to exclude.
"""
self._exclude_views.add(view)
def include(self, view):
"""
Includes a view from tracking if we are in include mode.
:Parameters:
- `view`: The view to include.
"""
self._include_views.add(view)
def _modify_blueprint(self, blueprint, include_type):
"""
Modifies a blueprint to include or exclude views.
:Parameters:
- `blueprint`: Blueprint instance to include.
- `include_type`: include or exlude.
"""
blueprint.before_request = self.before_request
blueprint.after_request = self.after_request
# Hack to grab views from blueprints since view_functions
# is always empty
class DeferredViewGrabber(object):
views = []
def add_url_rule(self, rule, endpoint, view_func, **options):
self.views.append(view_func)
dvg = DeferredViewGrabber()
[defered(dvg) for defered in blueprint.deferred_functions]
for view in dvg.views:
if include_type.lower() == 'include':
self._include_views.add(view)
elif include_type.lower() == 'exclude':
self._exclude_views.add(view)
else:
raise NotImplementedError(
'You must set include or exclude type for the blueprint.')
return blueprint
def include_blueprint(self, blueprint):
"""
Includes an entire blueprint.
:Parameters:
- `blueprint`: Blueprint instance to include.
"""
self._modify_blueprint(blueprint, 'include')
def exclude_blueprint(self, blueprint):
"""
Excludes an entire blueprint.
:Parameters:
- `blueprint`: Blueprint instance to exclude.
"""
self._modify_blueprint(blueprint, 'exclude')
if __name__ == '__main__':
# Example
from flask import Flask
app = Flask(__name__)
# Set the configuration items manually for the example
app.config['TRACK_USAGE_USE_FREEGEOIP'] = False
app.config['TRACK_USAGE_INCLUDE_OR_EXCLUDE_VIEWS'] = 'include'
# We will just print out the data for the example
from flask_track_usage.storage.printer import PrintWriter
from flask_track_usage.storage.output import OutputWriter
# Make an instance of the extension and put two writers
t = TrackUsage(app, [PrintWriter(), OutputWriter(
transform=lambda s: "OUTPUT: " + str(s))])
# Include the view in the metrics
@t.include
@app.route('/')
def index():
return "Hello"
# Run the application!
app.run(debug=True)