diff --git a/tests/unit/legacy/api/xmlrpc/test_init.py b/tests/unit/legacy/api/xmlrpc/test_init.py deleted file mode 100644 index 3598598680c9..000000000000 --- a/tests/unit/legacy/api/xmlrpc/test_init.py +++ /dev/null @@ -1,60 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import pretend -from pyramid.events import NewResponse - -from warehouse.legacy.api.xmlrpc import on_new_response, includeme - - -def test_on_new_response_enabled(): - metric_name = pretend.stub() - histogram = pretend.call_recorder(lambda metric_name, value: None) - content_length = pretend.stub() - new_response_event = pretend.stub( - request=pretend.stub( - content_length_metric_name=metric_name, - registry=pretend.stub(datadog=pretend.stub(histogram=histogram)), - ), - response=pretend.stub(content_length=content_length) - ) - - on_new_response(new_response_event) - - assert histogram.calls == [ - pretend.call(metric_name, content_length), - ] - - -def test_on_new_response_disabled(): - histogram = pretend.call_recorder(lambda metric_name, value: None) - new_response_event = pretend.stub( - request=pretend.stub( - registry=pretend.stub(datadog=pretend.stub(histogram=histogram)), - ), - ) - - on_new_response(new_response_event) - - assert histogram.calls == [] - - -def test_includeme(): - config = pretend.stub( - add_subscriber=pretend.call_recorder(lambda subscriber, iface: None), - ) - - includeme(config) - - assert config.add_subscriber.calls == [ - pretend.call(on_new_response, NewResponse), - ] diff --git a/tests/unit/legacy/api/xmlrpc/test_xmlrpc.py b/tests/unit/legacy/api/xmlrpc/test_xmlrpc.py index 5b5cfb13ccb2..6593a833dfae 100644 --- a/tests/unit/legacy/api/xmlrpc/test_xmlrpc.py +++ b/tests/unit/legacy/api/xmlrpc/test_xmlrpc.py @@ -812,8 +812,18 @@ def test_multicall(self, monkeypatch): body = pretend.stub() response = pretend.stub(body=body) - invoke_subrequest = pretend.call_recorder(lambda *a, **kw: response) - request = pretend.stub(invoke_subrequest=invoke_subrequest) + request = pretend.stub( + invoke_subrequest=pretend.call_recorder(lambda *a, **kw: response), + add_response_callback=pretend.call_recorder( + lambda *a, **kw: response), + ) + + callback = pretend.stub() + monkeypatch.setattr( + xmlrpc, + 'measure_response_content_length', + pretend.call_recorder(lambda metric_name: callback) + ) args = [ {'methodName': 'search', 'params': [{'name': 'foo'}]}, @@ -827,10 +837,13 @@ def test_multicall(self, monkeypatch): pretend.call('/RPC2', headers={'Content-Type': 'text/xml'}), pretend.call('/RPC2', headers={'Content-Type': 'text/xml'}), ] - assert invoke_subrequest.calls == [ + assert request.invoke_subrequest.calls == [ pretend.call(subreq, use_tweens=True), pretend.call(subreq, use_tweens=True), ] + assert request.add_response_callback.calls == [ + pretend.call(callback), + ] assert dumps.calls == [ pretend.call(({'name': 'foo'},), methodname='search'), pretend.call(({'classifiers': ['bar']},), methodname='browse'), @@ -869,3 +882,22 @@ def test_too_many_multicalls_method(self): assert exc.value.faultString == ( 'ValueError: Multicall limit is 20 calls' ) + + def test_measure_response_content_length(self): + metric_name = 'some_metric_name' + callback = xmlrpc.measure_response_content_length(metric_name) + + request = pretend.stub( + registry=pretend.stub( + datadog=pretend.stub( + histogram=pretend.call_recorder(lambda *a: None) + ) + ) + ) + response = pretend.stub(content_length=pretend.stub()) + + callback(request, response) + + assert request.registry.datadog.histogram.calls == [ + pretend.call(metric_name, response.content_length), + ] diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 89b6e70cd441..a46fc2283e96 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -402,7 +402,6 @@ def __init__(self): pretend.call("pyramid_retry"), pretend.call("pyramid_tm"), pretend.call("pyramid_services"), - pretend.call(".legacy.api.xmlrpc"), pretend.call(".legacy.api.xmlrpc.cache"), pretend.call("pyramid_rpc.xmlrpc"), pretend.call(".legacy.action_routing"), diff --git a/warehouse/config.py b/warehouse/config.py index 77c908b3e21a..91a56e772cdb 100644 --- a/warehouse/config.py +++ b/warehouse/config.py @@ -377,9 +377,6 @@ def configure(settings=None): # Register support for services config.include("pyramid_services") - # Register our XMLRPC API - config.include(".legacy.api.xmlrpc") - # Register our XMLRPC cache config.include(".legacy.api.xmlrpc.cache") diff --git a/warehouse/legacy/api/xmlrpc/__init__.py b/warehouse/legacy/api/xmlrpc/__init__.py index c14f7835049d..36ab810bdee3 100644 --- a/warehouse/legacy/api/xmlrpc/__init__.py +++ b/warehouse/legacy/api/xmlrpc/__init__.py @@ -10,21 +10,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -from pyramid.events import NewResponse - import warehouse.legacy.api.xmlrpc.views # noqa - - -def on_new_response(new_response_event): - request = new_response_event.request - if not hasattr(request, 'content_length_metric_name'): - return - - request.registry.datadog.histogram( - request.content_length_metric_name, - new_response_event.response.content_length - ) - - -def includeme(config): - config.add_subscriber(on_new_response, NewResponse) diff --git a/warehouse/legacy/api/xmlrpc/views.py b/warehouse/legacy/api/xmlrpc/views.py index 1528d6ca7ac1..99ac8e5c8a61 100644 --- a/warehouse/legacy/api/xmlrpc/views.py +++ b/warehouse/legacy/api/xmlrpc/views.py @@ -448,12 +448,18 @@ def browse(request, classifiers): return [(r.name, r.version) for r in releases] +def measure_response_content_length(metric_name): + + def _callback(request, response): + request.registry.datadog.histogram( + metric_name, response.content_length + ) + + return _callback + + @xmlrpc_method(method='system.multicall') def multicall(request, args): - request.content_length_metric_name = ( - 'warehouse.xmlrpc.system.multicall.content_length' - ) - if any(arg.get('methodName') == 'system.multicall' for arg in args): raise XMLRPCWrappedError( ValueError('Cannot use system.multicall inside a multicall') @@ -478,4 +484,11 @@ def multicall(request, args): ).encode() response = request.invoke_subrequest(subreq, use_tweens=True) responses.append(xmlrpc.client.loads(response.body)) + + request.add_response_callback( + measure_response_content_length( + 'warehouse.xmlrpc.system.multicall.content_length' + ) + ) + return responses