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