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