##// END OF EJS Templates
chore(logging): optimize loggers for request wrapper to be easier to read
super-admin -
r5512:669e0fe1 default
parent child Browse files
Show More
@@ -1,96 +1,96 b''
1 1
2 2
3 3 # Copyright (C) 2016-2023 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
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 Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import time
22 22 import logging
23 23
24 24 import rhodecode
25 25 from rhodecode.lib.auth import AuthUser
26 26 from rhodecode.lib.base import get_ip_addr, get_user_agent
27 27 from rhodecode.lib.middleware.utils import get_path_info
28 28 from rhodecode.lib.utils2 import safe_str, get_current_rhodecode_user
29 29
30 30
31 31 log = logging.getLogger(__name__)
32 32
33 33
34 34 class RequestWrapperTween(object):
35 35 def __init__(self, handler, registry):
36 36 self.handler = handler
37 37 self.registry = registry
38 38
39 39 # one-time configuration code goes here
40 40
41 41 def _get_user_info(self, request):
42 42 user = get_current_rhodecode_user(request)
43 43 if not user:
44 44 user = AuthUser.repr_user(ip=get_ip_addr(request.environ))
45 45 return user
46 46
47 47 def __call__(self, request):
48 48 start = time.time()
49 log.debug('Starting request time measurement')
49 log.debug('Starting request processing')
50 50 response = None
51 51 request.req_wrapper_start = start
52 52
53 53 try:
54 54 response = self.handler(request)
55 55 finally:
56 56 count = request.request_count()
57 57 _ver_ = rhodecode.__version__
58 58 _path = get_path_info(request.environ)
59 59 _auth_user = self._get_user_info(request)
60 60 ip = get_ip_addr(request.environ)
61 61 match_route = request.matched_route.name if request.matched_route else "NOT_FOUND"
62 62 resp_code = getattr(response, 'status_code', 'UNDEFINED')
63 63
64 64 total = time.time() - start
65 65 log.info(
66 'Req[%4s] %s %s Request to %s time: %.4fs [%s], RhodeCode %s',
66 'Finished request processing: req[%4s] %s %s Request to %s time: %.4fs [%s], RhodeCode %s',
67 67 count, _auth_user, request.environ.get('REQUEST_METHOD'),
68 68 _path, total, get_user_agent(request. environ), _ver_,
69 69 extra={"time": total, "ver": _ver_, "ip": ip,
70 70 "path": _path, "view_name": match_route, "code": resp_code}
71 71 )
72 72
73 73 statsd = request.registry.statsd
74 74 if statsd:
75 75 elapsed_time_ms = round(1000.0 * total) # use ms only
76 76 statsd.timing(
77 77 "rhodecode_req_timing.histogram", elapsed_time_ms,
78 78 tags=[
79 79 "view_name:{}".format(match_route),
80 80 "code:{}".format(resp_code)
81 81 ],
82 82 use_decimals=False
83 83 )
84 84 statsd.incr(
85 85 'rhodecode_req_total', tags=[
86 86 "view_name:{}".format(match_route),
87 87 "code:{}".format(resp_code)
88 88 ])
89 89
90 90 return response
91 91
92 92
93 93 def includeme(config):
94 94 config.add_tween(
95 95 'rhodecode.lib.middleware.request_wrapper.RequestWrapperTween',
96 96 )
General Comments 0
You need to be logged in to leave comments. Login now