From 5890920096617631876ec8407aae38f08d1e4a79 Mon Sep 17 00:00:00 2001 From: Timothy Pansino <11214426+TimPansino@users.noreply.github.com> Date: Tue, 26 Apr 2022 11:04:00 -0700 Subject: [PATCH] Fix crash in route wrappers for Starlette 0.19.1 (#527) * Fix crash in route wrappers for starlette 0.19.1 Co-authored-by: Lalleh Rafeei Co-authored-by: Uma Annamalai * [Mega-Linter] Apply linters fixes * Bump Tests Co-authored-by: Lalleh Rafeei Co-authored-by: Uma Annamalai Co-authored-by: TimPansino --- newrelic/hooks/framework_starlette.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/newrelic/hooks/framework_starlette.py b/newrelic/hooks/framework_starlette.py index 9bf9c91bb5..498abeb29f 100644 --- a/newrelic/hooks/framework_starlette.py +++ b/newrelic/hooks/framework_starlette.py @@ -61,7 +61,20 @@ def bind_add_exception_handler(exc_class_or_status_code, handler, *args, **kwarg def wrap_route(wrapped, instance, args, kwargs): path, endpoint, args, kwargs = bind_endpoint(*args, **kwargs) - endpoint = route_naming_wrapper(FunctionTraceWrapper(endpoint)) + endpoint = FunctionTraceWrapper(endpoint) + + # Starlette name detection gets a bit confused with our wrappers + # so the get_name function should be called and the result should + # be cached on the wrapper. + try: + if not hasattr(endpoint, "__name__"): + from starlette.routing import get_name + + endpoint.__name__ = get_name(endpoint.__wrapped__) + except Exception: + pass + + endpoint = route_naming_wrapper(endpoint) return wrapped(path, endpoint, *args, **kwargs)