Show More
@@ -1,93 +1,93 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2016-2020 RhodeCode GmbH |
|
3 | # Copyright (C) 2016-2020 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_access_path, get_user_agent |
|
26 | from rhodecode.lib.base import get_ip_addr, get_access_path, get_user_agent | |
27 | from rhodecode.lib.utils2 import safe_str, get_current_rhodecode_user |
|
27 | from rhodecode.lib.utils2 import safe_str, get_current_rhodecode_user | |
28 |
|
28 | |||
29 |
|
29 | |||
30 | log = logging.getLogger(__name__) |
|
30 | log = logging.getLogger(__name__) | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | class RequestWrapperTween(object): |
|
33 | class RequestWrapperTween(object): | |
34 | def __init__(self, handler, registry): |
|
34 | def __init__(self, handler, registry): | |
35 | self.handler = handler |
|
35 | self.handler = handler | |
36 | self.registry = registry |
|
36 | self.registry = registry | |
37 |
|
37 | |||
38 | # one-time configuration code goes here |
|
38 | # one-time configuration code goes here | |
39 |
|
39 | |||
40 | def _get_user_info(self, request): |
|
40 | def _get_user_info(self, request): | |
41 | user = get_current_rhodecode_user(request) |
|
41 | user = get_current_rhodecode_user(request) | |
42 | if not user: |
|
42 | if not user: | |
43 | user = AuthUser.repr_user(ip=get_ip_addr(request.environ)) |
|
43 | user = AuthUser.repr_user(ip=get_ip_addr(request.environ)) | |
44 | return user |
|
44 | return user | |
45 |
|
45 | |||
46 | def __call__(self, request): |
|
46 | def __call__(self, request): | |
47 | start = time.time() |
|
47 | start = time.time() | |
48 | log.debug('Starting request time measurement') |
|
48 | log.debug('Starting request time measurement') | |
49 | response = None |
|
49 | response = None | |
50 | try: |
|
50 | try: | |
51 | response = self.handler(request) |
|
51 | response = self.handler(request) | |
52 | finally: |
|
52 | finally: | |
53 | count = request.request_count() |
|
53 | count = request.request_count() | |
54 | _ver_ = rhodecode.__version__ |
|
54 | _ver_ = rhodecode.__version__ | |
55 | _path = safe_str(get_access_path(request.environ)) |
|
55 | _path = safe_str(get_access_path(request.environ)) | |
56 | _auth_user = self._get_user_info(request) |
|
56 | _auth_user = self._get_user_info(request) | |
57 | ip = get_ip_addr(request.environ) |
|
57 | ip = get_ip_addr(request.environ) | |
58 | match_route = request.matched_route.name if request.matched_route else "NOT_FOUND" |
|
58 | match_route = request.matched_route.name if request.matched_route else "NOT_FOUND" | |
|
59 | resp_code = getattr(response, 'status_code', 'UNDEFINED') | |||
59 |
|
60 | |||
60 | total = time.time() - start |
|
61 | total = time.time() - start | |
61 | log.info( |
|
62 | log.info( | |
62 | 'Req[%4s] %s %s Request to %s time: %.4fs [%s], RhodeCode %s', |
|
63 | 'Req[%4s] %s %s Request to %s time: %.4fs [%s], RhodeCode %s', | |
63 | count, _auth_user, request.environ.get('REQUEST_METHOD'), |
|
64 | count, _auth_user, request.environ.get('REQUEST_METHOD'), | |
64 | _path, total, get_user_agent(request. environ), _ver_, |
|
65 | _path, total, get_user_agent(request. environ), _ver_, | |
65 | extra={"time": total, "ver": _ver_, "ip": ip, |
|
66 | extra={"time": total, "ver": _ver_, "ip": ip, | |
66 | "path": _path, "view_name": match_route} |
|
67 | "path": _path, "view_name": match_route, "code": resp_code} | |
67 | ) |
|
68 | ) | |
68 |
|
69 | |||
69 | statsd = request.registry.statsd |
|
70 | statsd = request.registry.statsd | |
70 | if statsd: |
|
71 | if statsd: | |
71 | resp_code = getattr(response, 'status_code', 'UNDEFINED') |
|
|||
72 | elapsed_time_ms = round(1000.0 * total) # use ms only |
|
72 | elapsed_time_ms = round(1000.0 * total) # use ms only | |
73 | statsd.timing( |
|
73 | statsd.timing( | |
74 | "rhodecode_req_timing.histogram", elapsed_time_ms, |
|
74 | "rhodecode_req_timing.histogram", elapsed_time_ms, | |
75 | tags=[ |
|
75 | tags=[ | |
76 | "view_name:{}".format(match_route), |
|
76 | "view_name:{}".format(match_route), | |
77 | "code:{}".format(resp_code) |
|
77 | "code:{}".format(resp_code) | |
78 | ], |
|
78 | ], | |
79 | use_decimals=False |
|
79 | use_decimals=False | |
80 | ) |
|
80 | ) | |
81 | statsd.incr( |
|
81 | statsd.incr( | |
82 | 'rhodecode_req_total', tags=[ |
|
82 | 'rhodecode_req_total', tags=[ | |
83 | "view_name:{}".format(match_route), |
|
83 | "view_name:{}".format(match_route), | |
84 | "code:{}".format(resp_code) |
|
84 | "code:{}".format(resp_code) | |
85 | ]) |
|
85 | ]) | |
86 |
|
86 | |||
87 | return response |
|
87 | return response | |
88 |
|
88 | |||
89 |
|
89 | |||
90 | def includeme(config): |
|
90 | def includeme(config): | |
91 | config.add_tween( |
|
91 | config.add_tween( | |
92 | 'rhodecode.lib.middleware.request_wrapper.RequestWrapperTween', |
|
92 | 'rhodecode.lib.middleware.request_wrapper.RequestWrapperTween', | |
93 | ) |
|
93 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now