# HG changeset patch # User RhodeCode Admin # Date 2023-02-02 22:47:08 # Node ID ae398e6123db583269b7b51607cd1dfc83affea8 # Parent fa4221419326cc37be77e618d876060216369a84 vcs-client: report more data about the call for better request tracking diff --git a/rhodecode/lib/vcs/__init__.py b/rhodecode/lib/vcs/__init__.py --- a/rhodecode/lib/vcs/__init__.py +++ b/rhodecode/lib/vcs/__init__.py @@ -124,7 +124,7 @@ class CurlSession(object): # TODO: johbo: I did test with 7.19 of libcurl. This version has # trouble with 100 - continue being set in the expect header. This # can lead to massive performance drops, switching it off here. - curl.setopt(curl.HTTPHEADER, ["Expect:"]) + curl.setopt(curl.TCP_NODELAY, True) curl.setopt(curl.PROTOCOLS, curl.PROTO_HTTP) curl.setopt(curl.USERAGENT, 'RhodeCode HTTP {}'.format(rhodecode.__version__)) @@ -132,7 +132,10 @@ class CurlSession(object): curl.setopt(curl.SSL_VERIFYHOST, 0) self._curl = curl - def post(self, url, data, allow_redirects=False): + def post(self, url, data, allow_redirects=False, headers=None): + headers = headers or {} + # format is ['header_name1: header_value1', 'header_name2: header_value2']) + headers_list = ["Expect:"] + ['{}: {}'.format(k, v) for k, v in headers.items()] response_buffer = StringIO() curl = self._curl @@ -141,6 +144,7 @@ class CurlSession(object): curl.setopt(curl.POSTFIELDS, data) curl.setopt(curl.FOLLOWLOCATION, allow_redirects) curl.setopt(curl.WRITEDATA, response_buffer) + curl.setopt(curl.HTTPHEADER, headers_list) curl.perform() status_code = curl.getinfo(pycurl.HTTP_CODE) diff --git a/rhodecode/lib/vcs/client_http.py b/rhodecode/lib/vcs/client_http.py --- a/rhodecode/lib/vcs/client_http.py +++ b/rhodecode/lib/vcs/client_http.py @@ -56,7 +56,11 @@ EXCEPTIONS_MAP = { def _remote_call(url, payload, exceptions_map, session): try: - response = session.post(url, data=msgpack.packb(payload)) + headers = { + 'X-RC-Method': payload.get('method'), + 'X-RC-Repo-Name': payload.get('_repo_name') + } + response = session.post(url, data=msgpack.packb(payload), headers=headers) except pycurl.error as e: msg = '{}. \npycurl traceback: {}'.format(e, traceback.format_exc()) raise exceptions.HttpVCSCommunicationError(msg) @@ -188,13 +192,16 @@ class RemoteRepo(object): self._session = remote_maker._session_factory() cache_repo_id = self._repo_id_sanitizer(repo_id) + _repo_name = self._get_repo_name(config, path) self._cache_region, self._cache_namespace = \ remote_maker.init_cache_region(cache_repo_id) with_wire = with_wire or {} repo_state_uid = with_wire.get('repo_state_uid') or 'state' + self._wire = { + "_repo_name": _repo_name, "path": path, # repo path "repo_id": repo_id, "cache_repo_id": cache_repo_id, @@ -213,6 +220,10 @@ class RemoteRepo(object): self.cert_dir = get_cert_path(rhodecode.CONFIG.get('__file__')) + def _get_repo_name(self, config, path): + repo_store = config.get('paths', '/') + return path.split(repo_store)[-1].lstrip('/') + def _repo_id_sanitizer(self, repo_id): pathless = repo_id.replace('/', '__').replace('-', '_') return ''.join(char if ord(char) < 128 else '_{}_'.format(ord(char)) for char in pathless) @@ -238,6 +249,7 @@ class RemoteRepo(object): payload = { 'id': str(uuid.uuid4()), 'method': name, + "_repo_name": wire['_repo_name'], 'params': {'wire': wire, 'args': args, 'kwargs': kwargs} } diff --git a/rhodecode/tests/vcs/test_client_http.py b/rhodecode/tests/vcs/test_client_http.py --- a/rhodecode/tests/vcs/test_client_http.py +++ b/rhodecode/tests/vcs/test_client_http.py @@ -100,7 +100,8 @@ def test_repo_maker_uses_session_for_cla 'server_and_port', 'endpoint', 'test_dummy_scm', stub_session_factory) repo_maker.example_call() stub_session_factory().post.assert_called_with( - 'http://server_and_port/endpoint', data=mock.ANY) + 'http://server_and_port/endpoint', data=mock.ANY, + headers={'X-RC-Method': 'example_call', 'X-RC-Repo-Name': None}) def test_repo_maker_uses_session_for_instance_methods( @@ -110,7 +111,8 @@ def test_repo_maker_uses_session_for_ins repo = repo_maker('stub_path', 'stub_repo_id', config) repo.example_call() stub_session_factory().post.assert_called_with( - 'http://server_and_port/endpoint', data=mock.ANY) + 'http://server_and_port/endpoint', data=mock.ANY, + headers={'X-RC-Method': 'example_call', 'X-RC-Repo-Name': 'stub_path'}) @mock.patch('rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory')