##// END OF EJS Templates
logging: update request wrapper to expose additional data points
super-admin -
r1017:a6b2d02d default
parent child Browse files
Show More
@@ -1,85 +1,90 b''
1 1 # RhodeCode VCSServer provides access to different vcs backends via network.
2 2 # Copyright (C) 2014-2020 RhodeCode GmbH
3 3 #
4 4 # This program is free software; you can redistribute it and/or modify
5 5 # it under the terms of the GNU General Public License as published by
6 6 # the Free Software Foundation; either version 3 of the License, or
7 7 # (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software Foundation,
16 16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 17
18 18 import time
19 19 import logging
20 20
21 21 import vcsserver
22 22 from vcsserver.utils import safe_str
23 23
24 24
25 25 log = logging.getLogger(__name__)
26 26
27 27
28 28 def get_access_path(environ):
29 29 path = environ.get('PATH_INFO')
30 30 return path
31 31
32 32
33 33 def get_user_agent(environ):
34 34 return environ.get('HTTP_USER_AGENT')
35 35
36 36
37 37 class RequestWrapperTween(object):
38 38 def __init__(self, handler, registry):
39 39 self.handler = handler
40 40 self.registry = registry
41 41
42 42 # one-time configuration code goes here
43 43
44 44 def __call__(self, request):
45 45 start = time.time()
46 46 log.debug('Starting request time measurement')
47 response = None
47 48 try:
48 49 response = self.handler(request)
49 50 finally:
50 51 count = request.request_count()
51 52 _ver_ = vcsserver.__version__
52 53 _path = safe_str(get_access_path(request.environ))
54 ip = '127.0.0.1'
55 match_route = request.matched_route.name if request.matched_route else "NOT_FOUND"
53 56
54 57 total = time.time() - start
55 58 log.info(
56 59 'Req[%4s] IP: %s %s Request to %s time: %.4fs [%s], VCSServer %s',
57 count, '127.0.0.1', request.environ.get('REQUEST_METHOD'),
58 _path, total, get_user_agent(request.environ), _ver_
60 count, ip, request.environ.get('REQUEST_METHOD'),
61 _path, total, get_user_agent(request.environ), _ver_,
62 extra={"time": total, "ver": _ver_,
63 "path": _path, "view_name": match_route}
59 64 )
60 65
61 66 statsd = request.registry.statsd
62 67 if statsd:
63 68 match_route = request.matched_route.name if request.matched_route else _path
64 resp_code = response.status_code
69 resp_code = getattr(response, 'status_code', 'UNDEFINED')
65 70 elapsed_time_ms = round(1000.0 * total) # use ms only
66 71 statsd.timing(
67 72 "vcsserver_req_timing.histogram", elapsed_time_ms,
68 73 tags=[
69 74 "view_name:{}".format(match_route),
70 75 "code:{}".format(resp_code)
71 76 ],
72 77 use_decimals=False
73 78 )
74 79 statsd.incr(
75 80 "vcsserver_req_total", tags=[
76 81 "view_name:{}".format(match_route),
77 82 "code:{}".format(resp_code)
78 83 ])
79 84 return response
80 85
81 86
82 87 def includeme(config):
83 88 config.add_tween(
84 89 'vcsserver.tweens.request_wrapper.RequestWrapperTween',
85 90 )
General Comments 0
You need to be logged in to leave comments. Login now