Skip to content

Commit e458894

Browse files
committed
Use a response callback instead
1 parent d59763f commit e458894

File tree

6 files changed

+52
-88
lines changed

6 files changed

+52
-88
lines changed

tests/unit/legacy/api/xmlrpc/test_init.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

tests/unit/legacy/api/xmlrpc/test_xmlrpc.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,18 @@ def test_multicall(self, monkeypatch):
812812
body = pretend.stub()
813813
response = pretend.stub(body=body)
814814

815-
invoke_subrequest = pretend.call_recorder(lambda *a, **kw: response)
816-
request = pretend.stub(invoke_subrequest=invoke_subrequest)
815+
request = pretend.stub(
816+
invoke_subrequest=pretend.call_recorder(lambda *a, **kw: response),
817+
add_response_callback=pretend.call_recorder(
818+
lambda *a, **kw: response),
819+
)
820+
821+
callback = pretend.stub()
822+
monkeypatch.setattr(
823+
xmlrpc,
824+
'measure_response_content_length',
825+
pretend.call_recorder(lambda metric_name: callback)
826+
)
817827

818828
args = [
819829
{'methodName': 'search', 'params': [{'name': 'foo'}]},
@@ -827,10 +837,13 @@ def test_multicall(self, monkeypatch):
827837
pretend.call('/RPC2', headers={'Content-Type': 'text/xml'}),
828838
pretend.call('/RPC2', headers={'Content-Type': 'text/xml'}),
829839
]
830-
assert invoke_subrequest.calls == [
840+
assert request.invoke_subrequest.calls == [
831841
pretend.call(subreq, use_tweens=True),
832842
pretend.call(subreq, use_tweens=True),
833843
]
844+
assert request.add_response_callback.calls == [
845+
pretend.call(callback),
846+
]
834847
assert dumps.calls == [
835848
pretend.call(({'name': 'foo'},), methodname='search'),
836849
pretend.call(({'classifiers': ['bar']},), methodname='browse'),
@@ -869,3 +882,22 @@ def test_too_many_multicalls_method(self):
869882
assert exc.value.faultString == (
870883
'ValueError: Multicall limit is 20 calls'
871884
)
885+
886+
def test_measure_response_content_length(self):
887+
metric_name = 'some_metric_name'
888+
callback = xmlrpc.measure_response_content_length(metric_name)
889+
890+
request = pretend.stub(
891+
registry=pretend.stub(
892+
datadog=pretend.stub(
893+
histogram=pretend.call_recorder(lambda *a: None)
894+
)
895+
)
896+
)
897+
response = pretend.stub(content_length=pretend.stub())
898+
899+
callback(request, response)
900+
901+
assert request.registry.datadog.histogram.calls == [
902+
pretend.call(metric_name, response.content_length),
903+
]

tests/unit/test_config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ def __init__(self):
402402
pretend.call("pyramid_retry"),
403403
pretend.call("pyramid_tm"),
404404
pretend.call("pyramid_services"),
405-
pretend.call(".legacy.api.xmlrpc"),
406405
pretend.call(".legacy.api.xmlrpc.cache"),
407406
pretend.call("pyramid_rpc.xmlrpc"),
408407
pretend.call(".legacy.action_routing"),

warehouse/config.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,6 @@ def configure(settings=None):
377377
# Register support for services
378378
config.include("pyramid_services")
379379

380-
# Register our XMLRPC API
381-
config.include(".legacy.api.xmlrpc")
382-
383380
# Register our XMLRPC cache
384381
config.include(".legacy.api.xmlrpc.cache")
385382

warehouse/legacy/api/xmlrpc/__init__.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,4 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13-
from pyramid.events import NewResponse
14-
1513
import warehouse.legacy.api.xmlrpc.views # noqa
16-
17-
18-
def on_new_response(new_response_event):
19-
request = new_response_event.request
20-
if not hasattr(request, 'content_length_metric_name'):
21-
return
22-
23-
request.registry.datadog.histogram(
24-
request.content_length_metric_name,
25-
new_response_event.response.content_length
26-
)
27-
28-
29-
def includeme(config):
30-
config.add_subscriber(on_new_response, NewResponse)

warehouse/legacy/api/xmlrpc/views.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,18 @@ def browse(request, classifiers):
448448
return [(r.name, r.version) for r in releases]
449449

450450

451+
def measure_response_content_length(metric_name):
452+
453+
def _callback(request, response):
454+
request.registry.datadog.histogram(
455+
metric_name, response.content_length
456+
)
457+
458+
return _callback
459+
460+
451461
@xmlrpc_method(method='system.multicall')
452462
def multicall(request, args):
453-
request.content_length_metric_name = (
454-
'warehouse.xmlrpc.system.multicall.content_length'
455-
)
456-
457463
if any(arg.get('methodName') == 'system.multicall' for arg in args):
458464
raise XMLRPCWrappedError(
459465
ValueError('Cannot use system.multicall inside a multicall')
@@ -478,4 +484,11 @@ def multicall(request, args):
478484
).encode()
479485
response = request.invoke_subrequest(subreq, use_tweens=True)
480486
responses.append(xmlrpc.client.loads(response.body))
487+
488+
request.add_response_callback(
489+
measure_response_content_length(
490+
'warehouse.xmlrpc.system.multicall.content_length'
491+
)
492+
)
493+
481494
return responses

0 commit comments

Comments
 (0)