Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a view deriver to actually time views themselves (#5043)
- Loading branch information
Showing
4 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# 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 pytest | ||
import pretend | ||
|
||
from warehouse.metrics import views | ||
|
||
|
||
class TestTimingView: | ||
@pytest.mark.parametrize("route", [None, "foo"]) | ||
def test_unknown_view(self, pyramid_services, metrics, route): | ||
response = pretend.stub() | ||
view = pretend.call_recorder(lambda request, context: response) | ||
view_info = pretend.stub(original_view=pretend.stub()) | ||
|
||
derived = views.timing_view(view, view_info) | ||
|
||
request = pretend.stub( | ||
matched_route=pretend.stub(name=route) if route else None, | ||
find_service=pyramid_services.find_service, | ||
) | ||
context = pretend.stub() | ||
|
||
route_tag = "route:null" if route is None else f"route:{route}" | ||
|
||
assert derived(context, request) is response | ||
assert view.calls == [pretend.call(context, request)] | ||
assert metrics.timed.calls == [ | ||
pretend.call("pyramid.view.duration", tags=[route_tag, "view:unknown"]) | ||
] | ||
|
||
@pytest.mark.parametrize("route", [None, "foo"]) | ||
def test_qualname_view(self, pyramid_services, metrics, route): | ||
response = pretend.stub() | ||
view = pretend.call_recorder(lambda request, context: response) | ||
view_info = pretend.stub( | ||
original_view=pretend.stub( | ||
__module__="foo", __qualname__="bar", __name__="other" | ||
) | ||
) | ||
|
||
derived = views.timing_view(view, view_info) | ||
|
||
request = pretend.stub( | ||
matched_route=pretend.stub(name=route) if route else None, | ||
find_service=pyramid_services.find_service, | ||
) | ||
context = pretend.stub() | ||
|
||
route_tag = "route:null" if route is None else f"route:{route}" | ||
|
||
assert derived(context, request) is response | ||
assert view.calls == [pretend.call(context, request)] | ||
assert metrics.timed.calls == [ | ||
pretend.call("pyramid.view.duration", tags=[route_tag, "view:foo.bar"]) | ||
] | ||
|
||
@pytest.mark.parametrize("route", [None, "foo"]) | ||
def test_name_view(self, pyramid_services, metrics, route): | ||
response = pretend.stub() | ||
view = pretend.call_recorder(lambda request, context: response) | ||
view_info = pretend.stub( | ||
original_view=pretend.stub(__module__="foo", __name__="other") | ||
) | ||
|
||
derived = views.timing_view(view, view_info) | ||
|
||
request = pretend.stub( | ||
matched_route=pretend.stub(name=route) if route else None, | ||
find_service=pyramid_services.find_service, | ||
) | ||
context = pretend.stub() | ||
|
||
route_tag = "route:null" if route is None else f"route:{route}" | ||
|
||
assert derived(context, request) is response | ||
assert view.calls == [pretend.call(context, request)] | ||
assert metrics.timed.calls == [ | ||
pretend.call("pyramid.view.duration", tags=[route_tag, "view:foo.other"]) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 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. | ||
|
||
from warehouse.metrics.interfaces import IMetricsService | ||
|
||
|
||
def timing_view(view, info): | ||
def wrapper_view(context, request): | ||
metrics = request.find_service(IMetricsService, context=None) | ||
|
||
route_tag = "route:null" | ||
if request.matched_route: | ||
route_tag = f"route:{request.matched_route.name}" | ||
|
||
original_view = info.original_view | ||
if hasattr(original_view, "__qualname__"): | ||
view_name = f"{original_view.__module__}.{original_view.__qualname__}" | ||
elif hasattr(original_view, "__name__"): | ||
view_name = f"{original_view.__module__}.{original_view.__name__}" | ||
else: | ||
view_name = "unknown" | ||
view_tag = f"view:{view_name}" | ||
|
||
with metrics.timed("pyramid.view.duration", tags=[route_tag, view_tag]): | ||
return view(context, request) | ||
|
||
return wrapper_view |