2525from newrelic .common .object_names import callable_name
2626from testing_support .validators .validate_code_level_metrics import validate_code_level_metrics
2727
28+ starlette_version = tuple (int (x ) for x in starlette .__version__ .split ("." ))
2829
2930@pytest .fixture (scope = "session" )
3031def target_application ():
@@ -34,10 +35,18 @@ def target_application():
3435
3536
3637FRAMEWORK_METRIC = ("Python/Framework/Starlette/%s" % starlette .__version__ , 1 )
37- DEFAULT_MIDDLEWARE_METRICS = [
38- ("Function/starlette.middleware.errors:ServerErrorMiddleware.__call__" , 1 ),
39- ("Function/starlette.exceptions:ExceptionMiddleware.__call__" , 1 ),
40- ]
38+
39+ if starlette_version >= (0 , 20 , 1 ):
40+ DEFAULT_MIDDLEWARE_METRICS = [
41+ ("Function/starlette.middleware.errors:ServerErrorMiddleware.__call__" , 1 ),
42+ ("Function/starlette.middleware.exceptions:ExceptionMiddleware.__call__" , 1 ),
43+ ]
44+ else :
45+ DEFAULT_MIDDLEWARE_METRICS = [
46+ ("Function/starlette.middleware.errors:ServerErrorMiddleware.__call__" , 1 ),
47+ ("Function/starlette.exceptions:ExceptionMiddleware.__call__" , 1 ),
48+ ]
49+
4150MIDDLEWARE_METRICS = [
4251 ("Function/_test_application:middleware_factory.<locals>.middleware" , 2 ),
4352 ("Function/_test_application:middleware_decorator" , 1 ),
@@ -71,17 +80,27 @@ def test_application_non_async(target_application, app_name):
7180 response = app .get ("/non_async" )
7281 assert response .status == 200
7382
83+ # Starting in Starlette v0.20.1, the ExceptionMiddleware class
84+ # has been moved to the starlette.middleware.exceptions from
85+ # starlette.exceptions
86+ version_tweak_string = ".middleware" if starlette_version >= (0 , 20 , 1 ) else ""
7487
75- @pytest .mark .parametrize (
76- "app_name, transaction_name" ,
88+ DEFAULT_MIDDLEWARE_METRICS = [
89+ ("Function/starlette.middleware.errors:ServerErrorMiddleware.__call__" , 1 ),
90+ ("Function/starlette%s.exceptions:ExceptionMiddleware.__call__" % version_tweak_string , 1 ),
91+ ]
92+
93+ middleware_test = (
94+ ("no_error_handler" , "starlette%s.exceptions:ExceptionMiddleware.__call__" % version_tweak_string ),
7795 (
78- ("no_error_handler" , "starlette.exceptions:ExceptionMiddleware.__call__" ),
79- (
80- "non_async_error_handler_no_middleware" ,
81- "starlette.exceptions:ExceptionMiddleware.__call__" ,
82- ),
96+ "non_async_error_handler_no_middleware" ,
97+ "starlette%s.exceptions:ExceptionMiddleware.__call__" % version_tweak_string ,
8398 ),
8499)
100+
101+ @pytest .mark .parametrize (
102+ "app_name, transaction_name" , middleware_test ,
103+ )
85104def test_application_nonexistent_route (target_application , app_name , transaction_name ):
86105 @validate_transaction_metrics (
87106 transaction_name ,
@@ -244,19 +263,20 @@ def _test():
244263 _test ()
245264
246265
247- @pytest .mark .parametrize (
248- "app_name,scoped_metrics" ,
266+ middleware_test_exception = (
249267 (
250- (
251- "no_middleware" ,
252- [("Function/starlette.exceptions:ExceptionMiddleware.http_exception" , 1 )],
253- ),
254- (
255- "teapot_exception_handler_no_middleware" ,
256- [("Function/_test_application:teapot_handler" , 1 )],
257- ),
268+ "no_middleware" ,
269+ [("Function/starlette%s.exceptions:ExceptionMiddleware.http_exception" % version_tweak_string , 1 )],
270+ ),
271+ (
272+ "teapot_exception_handler_no_middleware" ,
273+ [("Function/_test_application:teapot_handler" , 1 )],
258274 ),
259275)
276+
277+ @pytest .mark .parametrize (
278+ "app_name,scoped_metrics" , middleware_test_exception
279+ )
260280def test_starlette_http_exception (target_application , app_name , scoped_metrics ):
261281 @validate_transaction_errors (errors = ["starlette.exceptions:HTTPException" ])
262282 @validate_transaction_metrics (
0 commit comments