Show More
@@ -1,90 +1,107 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2020 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2020 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
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 General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import time |
|
18 | import time | |
19 | import logging |
|
19 | import logging | |
20 |
|
20 | |||
21 | import vcsserver |
|
21 | import vcsserver | |
22 | from vcsserver.utils import safe_str |
|
22 | from vcsserver.utils import safe_str | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | log = logging.getLogger(__name__) |
|
25 | log = logging.getLogger(__name__) | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | def get_access_path(environ): |
|
28 | def get_access_path(environ): | |
29 | path = environ.get('PATH_INFO') |
|
29 | path = environ.get('PATH_INFO') | |
30 | return path |
|
30 | return path | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | def get_user_agent(environ): |
|
33 | def get_user_agent(environ): | |
34 | return environ.get('HTTP_USER_AGENT') |
|
34 | return environ.get('HTTP_USER_AGENT') | |
35 |
|
35 | |||
36 |
|
36 | |||
|
37 | def get_vcs_method(environ): | |||
|
38 | return environ.get('HTTP_X_RC_METHOD') | |||
|
39 | ||||
|
40 | ||||
|
41 | def get_vcs_repo(environ): | |||
|
42 | return environ.get('HTTP_X_RC_REPO_NAME') | |||
|
43 | ||||
|
44 | ||||
37 | class RequestWrapperTween(object): |
|
45 | class RequestWrapperTween(object): | |
38 | def __init__(self, handler, registry): |
|
46 | def __init__(self, handler, registry): | |
39 | self.handler = handler |
|
47 | self.handler = handler | |
40 | self.registry = registry |
|
48 | self.registry = registry | |
41 |
|
49 | |||
42 | # one-time configuration code goes here |
|
50 | # one-time configuration code goes here | |
43 |
|
51 | |||
44 | def __call__(self, request): |
|
52 | def __call__(self, request): | |
45 | start = time.time() |
|
53 | start = time.time() | |
46 | log.debug('Starting request time measurement') |
|
54 | log.debug('Starting request time measurement') | |
47 | response = None |
|
55 | response = None | |
|
56 | ||||
|
57 | ua = get_user_agent(request.environ) | |||
|
58 | vcs_method = get_vcs_method(request.environ) | |||
|
59 | repo_name = get_vcs_repo(request.environ) | |||
|
60 | ||||
48 | try: |
|
61 | try: | |
49 | response = self.handler(request) |
|
62 | response = self.handler(request) | |
50 | finally: |
|
63 | finally: | |
51 | count = request.request_count() |
|
64 | count = request.request_count() | |
52 | _ver_ = vcsserver.__version__ |
|
65 | _ver_ = vcsserver.__version__ | |
53 | _path = safe_str(get_access_path(request.environ)) |
|
66 | _path = safe_str(get_access_path(request.environ)) | |
54 | ip = '127.0.0.1' |
|
67 | ip = '127.0.0.1' | |
55 | match_route = request.matched_route.name if request.matched_route else "NOT_FOUND" |
|
68 | match_route = request.matched_route.name if request.matched_route else "NOT_FOUND" | |
56 | resp_code = getattr(response, 'status_code', 'UNDEFINED') |
|
69 | resp_code = getattr(response, 'status_code', 'UNDEFINED') | |
57 |
|
70 | |||
58 | total = time.time() - start |
|
71 | total = time.time() - start | |
|
72 | ||||
|
73 | _view_path = "{}/{}@{}".format(_path, vcs_method, repo_name) | |||
59 | log.info( |
|
74 | log.info( | |
60 | 'Req[%4s] IP: %s %s Request to %s time: %.4fs [%s], VCSServer %s', |
|
75 | 'Req[%4s] IP: %s %s Request to %s time: %.4fs [%s], VCSServer %s', | |
61 | count, ip, request.environ.get('REQUEST_METHOD'), |
|
76 | count, ip, request.environ.get('REQUEST_METHOD'), | |
62 |
_path, total, |
|
77 | _view_path, total, ua, _ver_, | |
63 | extra={"time": total, "ver": _ver_, "code": resp_code, |
|
78 | extra={"time": total, "ver": _ver_, "code": resp_code, | |
64 |
"path": _path, "view_name": match_route |
|
79 | "path": _path, "view_name": match_route, "user_agent": ua, | |
|
80 | "vcs_method": vcs_method, "repo_name": repo_name} | |||
65 | ) |
|
81 | ) | |
66 |
|
82 | |||
67 | statsd = request.registry.statsd |
|
83 | statsd = request.registry.statsd | |
68 | if statsd: |
|
84 | if statsd: | |
69 | match_route = request.matched_route.name if request.matched_route else _path |
|
85 | match_route = request.matched_route.name if request.matched_route else _path | |
70 | elapsed_time_ms = round(1000.0 * total) # use ms only |
|
86 | elapsed_time_ms = round(1000.0 * total) # use ms only | |
71 | statsd.timing( |
|
87 | statsd.timing( | |
72 | "vcsserver_req_timing.histogram", elapsed_time_ms, |
|
88 | "vcsserver_req_timing.histogram", elapsed_time_ms, | |
73 | tags=[ |
|
89 | tags=[ | |
74 | "view_name:{}".format(match_route), |
|
90 | "view_name:{}".format(match_route), | |
75 | "code:{}".format(resp_code) |
|
91 | "code:{}".format(resp_code) | |
76 | ], |
|
92 | ], | |
77 | use_decimals=False |
|
93 | use_decimals=False | |
78 | ) |
|
94 | ) | |
79 | statsd.incr( |
|
95 | statsd.incr( | |
80 | "vcsserver_req_total", tags=[ |
|
96 | "vcsserver_req_total", tags=[ | |
81 | "view_name:{}".format(match_route), |
|
97 | "view_name:{}".format(match_route), | |
82 | "code:{}".format(resp_code) |
|
98 | "code:{}".format(resp_code) | |
83 | ]) |
|
99 | ]) | |
|
100 | ||||
84 | return response |
|
101 | return response | |
85 |
|
102 | |||
86 |
|
103 | |||
87 | def includeme(config): |
|
104 | def includeme(config): | |
88 | config.add_tween( |
|
105 | config.add_tween( | |
89 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', |
|
106 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', | |
90 | ) |
|
107 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now