Show More
@@ -1,123 +1,123 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-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 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 | import base64 |
|
17 | import base64 | |
18 | import logging |
|
18 | import logging | |
19 | import time |
|
19 | import time | |
20 |
|
20 | |||
21 | import msgpack |
|
21 | import msgpack | |
22 |
|
22 | |||
23 | import vcsserver |
|
23 | import vcsserver | |
24 | from vcsserver.lib.str_utils import safe_str |
|
24 | from vcsserver.lib.str_utils import safe_str | |
25 |
|
25 | |||
26 | log = logging.getLogger(__name__) |
|
26 | log = logging.getLogger(__name__) | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | def get_access_path(environ): |
|
29 | def get_access_path(environ): | |
30 | path = environ.get('PATH_INFO') |
|
30 | path = environ.get('PATH_INFO') | |
31 | return path |
|
31 | return path | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | def get_user_agent(environ): |
|
34 | def get_user_agent(environ): | |
35 | return environ.get('HTTP_USER_AGENT') |
|
35 | return environ.get('HTTP_USER_AGENT') | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | def get_call_context(request) -> dict: |
|
38 | def get_call_context(request) -> dict: | |
39 | cc = {} |
|
39 | cc = {} | |
40 | registry = request.registry |
|
40 | registry = request.registry | |
41 | if hasattr(registry, 'vcs_call_context'): |
|
41 | if hasattr(registry, 'vcs_call_context'): | |
42 | cc.update({ |
|
42 | cc.update({ | |
43 | 'X-RC-Method': registry.vcs_call_context.get('method'), |
|
43 | 'X-RC-Method': registry.vcs_call_context.get('method'), | |
44 | 'X-RC-Repo-Name': registry.vcs_call_context.get('repo_name') |
|
44 | 'X-RC-Repo-Name': registry.vcs_call_context.get('repo_name') | |
45 | }) |
|
45 | }) | |
46 |
|
46 | |||
47 | return cc |
|
47 | return cc | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | def get_headers_call_context(environ, strict=True): |
|
50 | def get_headers_call_context(environ, strict=True): | |
51 | if 'HTTP_X_RC_VCS_STREAM_CALL_CONTEXT' in environ: |
|
51 | if 'HTTP_X_RC_VCS_STREAM_CALL_CONTEXT' in environ: | |
52 | packed_cc = base64.b64decode(environ['HTTP_X_RC_VCS_STREAM_CALL_CONTEXT']) |
|
52 | packed_cc = base64.b64decode(environ['HTTP_X_RC_VCS_STREAM_CALL_CONTEXT']) | |
53 | return msgpack.unpackb(packed_cc) |
|
53 | return msgpack.unpackb(packed_cc) | |
54 | elif strict: |
|
54 | elif strict: | |
55 | raise ValueError('Expected header HTTP_X_RC_VCS_STREAM_CALL_CONTEXT not found') |
|
55 | raise ValueError('Expected header HTTP_X_RC_VCS_STREAM_CALL_CONTEXT not found') | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | class RequestWrapperTween: |
|
58 | class RequestWrapperTween: | |
59 | def __init__(self, handler, registry): |
|
59 | def __init__(self, handler, registry): | |
60 | self.handler = handler |
|
60 | self.handler = handler | |
61 | self.registry = registry |
|
61 | self.registry = registry | |
62 |
|
62 | |||
63 | # one-time configuration code goes here |
|
63 | # one-time configuration code goes here | |
64 |
|
64 | |||
65 | def __call__(self, request): |
|
65 | def __call__(self, request): | |
66 | start = time.time() |
|
66 | start = time.time() | |
67 |
log.debug('Starting request |
|
67 | log.debug('Starting request processing') | |
68 | response = None |
|
68 | response = None | |
69 |
|
69 | |||
70 | try: |
|
70 | try: | |
71 | response = self.handler(request) |
|
71 | response = self.handler(request) | |
72 | finally: |
|
72 | finally: | |
73 | ua = get_user_agent(request.environ) |
|
73 | ua = get_user_agent(request.environ) | |
74 | call_context = get_call_context(request) |
|
74 | call_context = get_call_context(request) | |
75 | vcs_method = call_context.get('X-RC-Method', '_NO_VCS_METHOD') |
|
75 | vcs_method = call_context.get('X-RC-Method', '_NO_VCS_METHOD') | |
76 | repo_name = call_context.get('X-RC-Repo-Name', '') |
|
76 | repo_name = call_context.get('X-RC-Repo-Name', '') | |
77 |
|
77 | |||
78 | count = request.request_count() |
|
78 | count = request.request_count() | |
79 | _ver_ = vcsserver.get_version() |
|
79 | _ver_ = vcsserver.get_version() | |
80 | _path = safe_str(get_access_path(request.environ)) |
|
80 | _path = safe_str(get_access_path(request.environ)) | |
81 |
|
81 | |||
82 | ip = '127.0.0.1' |
|
82 | ip = '127.0.0.1' | |
83 | match_route = request.matched_route.name if request.matched_route else "NOT_FOUND" |
|
83 | match_route = request.matched_route.name if request.matched_route else "NOT_FOUND" | |
84 | resp_code = getattr(response, 'status_code', 'UNDEFINED') |
|
84 | resp_code = getattr(response, 'status_code', 'UNDEFINED') | |
85 |
|
85 | |||
86 | _view_path = f"{repo_name}@{_path}/{vcs_method}" |
|
86 | _view_path = f"{repo_name}@{_path}/{vcs_method}" | |
87 |
|
87 | |||
88 | total = time.time() - start |
|
88 | total = time.time() - start | |
89 |
|
89 | |||
90 | log.info( |
|
90 | log.info( | |
91 |
' |
|
91 | 'Finished request processing: reqq[%4s] IP: %s %s Request to %s time: %.4fs [%s], VCSServer %s', | |
92 | count, ip, request.environ.get('REQUEST_METHOD'), |
|
92 | count, ip, request.environ.get('REQUEST_METHOD'), | |
93 | _view_path, total, ua, _ver_, |
|
93 | _view_path, total, ua, _ver_, | |
94 | extra={"time": total, "ver": _ver_, "code": resp_code, |
|
94 | extra={"time": total, "ver": _ver_, "code": resp_code, | |
95 | "path": _path, "view_name": match_route, "user_agent": ua, |
|
95 | "path": _path, "view_name": match_route, "user_agent": ua, | |
96 | "vcs_method": vcs_method, "repo_name": repo_name} |
|
96 | "vcs_method": vcs_method, "repo_name": repo_name} | |
97 | ) |
|
97 | ) | |
98 |
|
98 | |||
99 | statsd = request.registry.statsd |
|
99 | statsd = request.registry.statsd | |
100 | if statsd: |
|
100 | if statsd: | |
101 | match_route = request.matched_route.name if request.matched_route else _path |
|
101 | match_route = request.matched_route.name if request.matched_route else _path | |
102 | elapsed_time_ms = round(1000.0 * total) # use ms only |
|
102 | elapsed_time_ms = round(1000.0 * total) # use ms only | |
103 | statsd.timing( |
|
103 | statsd.timing( | |
104 | "vcsserver_req_timing.histogram", elapsed_time_ms, |
|
104 | "vcsserver_req_timing.histogram", elapsed_time_ms, | |
105 | tags=[ |
|
105 | tags=[ | |
106 | f"view_name:{match_route}", |
|
106 | f"view_name:{match_route}", | |
107 | f"code:{resp_code}" |
|
107 | f"code:{resp_code}" | |
108 | ], |
|
108 | ], | |
109 | use_decimals=False |
|
109 | use_decimals=False | |
110 | ) |
|
110 | ) | |
111 | statsd.incr( |
|
111 | statsd.incr( | |
112 | "vcsserver_req_total", tags=[ |
|
112 | "vcsserver_req_total", tags=[ | |
113 | f"view_name:{match_route}", |
|
113 | f"view_name:{match_route}", | |
114 | f"code:{resp_code}" |
|
114 | f"code:{resp_code}" | |
115 | ]) |
|
115 | ]) | |
116 |
|
116 | |||
117 | return response |
|
117 | return response | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | def includeme(config): |
|
120 | def includeme(config): | |
121 | config.add_tween( |
|
121 | config.add_tween( | |
122 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', |
|
122 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', | |
123 | ) |
|
123 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now