Skip to content

Commit

Permalink
  (#253)
Browse files Browse the repository at this point in the history
Add blueprint middleware instrumentation and tests.
  • Loading branch information
umaannamalai authored Jun 22, 2021
1 parent a58dcfc commit b891adc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions newrelic/hooks/framework_sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 14 additions & 3 deletions tests/framework_sanic/_target_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
17 changes: 17 additions & 0 deletions tests/framework_sanic/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")]
Expand Down

0 comments on commit b891adc

Please sign in to comment.