From b891adc63a0904a357cd87cad3580d13326948fe Mon Sep 17 00:00:00 2001 From: Uma Annamalai Date: Tue, 22 Jun 2021 13:09:18 -0700 Subject: [PATCH] =?UTF-8?q?=C2=A0=20(#253)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add blueprint middleware instrumentation and tests. --- newrelic/hooks/framework_sanic.py | 3 +++ tests/framework_sanic/_target_application.py | 17 ++++++++++++++--- tests/framework_sanic/test_application.py | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/newrelic/hooks/framework_sanic.py b/newrelic/hooks/framework_sanic.py index 3982548b46..3f9bee9995 100644 --- a/newrelic/hooks/framework_sanic.py +++ b/newrelic/hooks/framework_sanic.py @@ -271,6 +271,9 @@ def instrument_sanic_app(module): _sanic_app_init) wrap_function_wrapper(module, 'Sanic.register_middleware', _nr_sanic_register_middleware_) + if hasattr(module.Sanic, 'register_named_middleware'): + wrap_function_wrapper(module, 'Sanic.register_named_middleware', + _nr_sanic_register_middleware_) def instrument_sanic_response(module): diff --git a/tests/framework_sanic/_target_application.py b/tests/framework_sanic/_target_application.py index 4e80780196..c94dcc5cc9 100644 --- a/tests/framework_sanic/_target_application.py +++ b/tests/framework_sanic/_target_application.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -from sanic import Sanic +from sanic import Sanic, Blueprint from sanic.exceptions import NotFound, SanicException, ServerError from sanic.handlers import ErrorHandler from sanic.response import json, stream from sanic.router import Router from sanic.views import HTTPMethodView -from sanic.websocket import WebSocketProtocol class MethodView(HTTPMethodView): @@ -83,7 +82,7 @@ def get(self, *args): router = CustomRouter() app = Sanic(name="test app", error_handler=CustomErrorHandler(), router=router) router.app = app - +blueprint = Blueprint("test_bp") @app.route('/') async def index(request): @@ -116,6 +115,10 @@ async def request_middleware(request): return None +@blueprint.middleware('request') +async def blueprint_middleware(request): + return None + # register the middleware a second time, testing that the `request_middleware` # function is not getting double wrapped app.register_middleware(request_middleware) @@ -179,6 +182,14 @@ async def async_error(request): raise CustomExceptionAsync('something went wrong') +@blueprint.route('/blueprint') +async def blueprint_route(request): + async def streaming_fn(response): + response.write('foo') + return stream(streaming_fn) + + +app.blueprint(blueprint) app.add_route(MethodView.as_view(), '/method_view') if not getattr(router, "finalized", True): diff --git a/tests/framework_sanic/test_application.py b/tests/framework_sanic/test_application.py index 3f54687259..664e434c95 100644 --- a/tests/framework_sanic/test_application.py +++ b/tests/framework_sanic/test_application.py @@ -338,6 +338,23 @@ def _test(): app.app.response_middleware = original_response_middleware +BLUEPRINT_METRICS = [ + ("Function/_target_application:blueprint_middleware", 1), + ("Function/_target_application:blueprint_route", 1), +] + + +@validate_transaction_metrics( + "_target_application:blueprint_route", + scoped_metrics=BLUEPRINT_METRICS, + rollup_metrics=BLUEPRINT_METRICS + FRAMEWORK_METRICS, +) +@validate_transaction_errors(errors=[]) +def test_blueprint_middleware(app): + response = app.fetch('get', '/blueprint') + assert response.status == 200 + + def test_unknown_route(app): import sanic sanic_version = [int(x) for x in sanic.__version__.split(".")]