Skip to content

Commit

Permalink
Add metrics for multicall response size
Browse files Browse the repository at this point in the history
  • Loading branch information
di committed Apr 23, 2018
1 parent f38ae14 commit bc95bc7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/unit/legacy/api/xmlrpc/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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),
]
1 change: 1 addition & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ 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"),
Expand Down
3 changes: 3 additions & 0 deletions warehouse/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ 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")

Expand Down
17 changes: 17 additions & 0 deletions warehouse/legacy/api/xmlrpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,21 @@
# 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)
4 changes: 4 additions & 0 deletions warehouse/legacy/api/xmlrpc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ def browse(request, classifiers):

@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')
Expand Down

0 comments on commit bc95bc7

Please sign in to comment.