Skip to content

Commit b891adc

Browse files
authored
  (#253)
Add blueprint middleware instrumentation and tests.
1 parent a58dcfc commit b891adc

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

newrelic/hooks/framework_sanic.py

+3
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ def instrument_sanic_app(module):
271271
_sanic_app_init)
272272
wrap_function_wrapper(module, 'Sanic.register_middleware',
273273
_nr_sanic_register_middleware_)
274+
if hasattr(module.Sanic, 'register_named_middleware'):
275+
wrap_function_wrapper(module, 'Sanic.register_named_middleware',
276+
_nr_sanic_register_middleware_)
274277

275278

276279
def instrument_sanic_response(module):

tests/framework_sanic/_target_application.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from sanic import Sanic
15+
from sanic import Sanic, Blueprint
1616
from sanic.exceptions import NotFound, SanicException, ServerError
1717
from sanic.handlers import ErrorHandler
1818
from sanic.response import json, stream
1919
from sanic.router import Router
2020
from sanic.views import HTTPMethodView
21-
from sanic.websocket import WebSocketProtocol
2221

2322

2423
class MethodView(HTTPMethodView):
@@ -83,7 +82,7 @@ def get(self, *args):
8382
router = CustomRouter()
8483
app = Sanic(name="test app", error_handler=CustomErrorHandler(), router=router)
8584
router.app = app
86-
85+
blueprint = Blueprint("test_bp")
8786

8887
@app.route('/')
8988
async def index(request):
@@ -116,6 +115,10 @@ async def request_middleware(request):
116115
return None
117116

118117

118+
@blueprint.middleware('request')
119+
async def blueprint_middleware(request):
120+
return None
121+
119122
# register the middleware a second time, testing that the `request_middleware`
120123
# function is not getting double wrapped
121124
app.register_middleware(request_middleware)
@@ -179,6 +182,14 @@ async def async_error(request):
179182
raise CustomExceptionAsync('something went wrong')
180183

181184

185+
@blueprint.route('/blueprint')
186+
async def blueprint_route(request):
187+
async def streaming_fn(response):
188+
response.write('foo')
189+
return stream(streaming_fn)
190+
191+
192+
app.blueprint(blueprint)
182193
app.add_route(MethodView.as_view(), '/method_view')
183194

184195
if not getattr(router, "finalized", True):

tests/framework_sanic/test_application.py

+17
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,23 @@ def _test():
338338
app.app.response_middleware = original_response_middleware
339339

340340

341+
BLUEPRINT_METRICS = [
342+
("Function/_target_application:blueprint_middleware", 1),
343+
("Function/_target_application:blueprint_route", 1),
344+
]
345+
346+
347+
@validate_transaction_metrics(
348+
"_target_application:blueprint_route",
349+
scoped_metrics=BLUEPRINT_METRICS,
350+
rollup_metrics=BLUEPRINT_METRICS + FRAMEWORK_METRICS,
351+
)
352+
@validate_transaction_errors(errors=[])
353+
def test_blueprint_middleware(app):
354+
response = app.fetch('get', '/blueprint')
355+
assert response.status == 200
356+
357+
341358
def test_unknown_route(app):
342359
import sanic
343360
sanic_version = [int(x) for x in sanic.__version__.split(".")]

0 commit comments

Comments
 (0)