File tree Expand file tree Collapse file tree 2 files changed +14
-6
lines changed 
packages/service-library/src/servicelib/fastapi 
services/api-server/src/simcore_service_api_server/core Expand file tree Collapse file tree 2 files changed +14
-6
lines changed Original file line number Diff line number Diff line change 11# pylint: disable=protected-access 
22
3+ import  logging 
34from  collections .abc  import  AsyncIterator 
45from  typing  import  Final 
56
67import  prometheus_client 
7- from  fastapi  import  FastAPI , Request , Response 
8+ from  fastapi  import  FastAPI , Request , Response ,  status 
89from  fastapi_lifespan_manager  import  State 
910from  prometheus_client  import  CollectorRegistry 
1011from  servicelib .prometheus_metrics  import  (
2122    X_SIMCORE_USER_AGENT ,
2223)
2324
25+ _logger  =  logging .getLogger (__name__ )
2426kPROMETHEUS_METRICS  =  "prometheus_metrics" 
2527
2628
@@ -45,13 +47,19 @@ async def dispatch(
4547                user_agent = user_agent ,
4648            ):
4749                response  =  await  call_next (request )
50+                 status_code  =  response .status_code 
51+         except  Exception :  # pylint: disable=broad-except 
52+             # NOTE: The prometheus metrics middleware should be "outside" exception handling 
53+             # middleware so at this point starlette should turn an exception into a 500 
54+             status_code  =  status .HTTP_500_INTERNAL_SERVER_ERROR 
55+             raise 
4856        finally :
4957            record_response_metrics (
5058                metrics = self .metrics ,
5159                method = request .method ,
5260                endpoint = canonical_endpoint ,
5361                user_agent = user_agent ,
54-                 http_status = response . status_code ,
62+                 http_status = status_code ,
5563            )
5664
5765        return  response 
Original file line number Diff line number Diff line change @@ -115,16 +115,16 @@ def init_app(settings: ApplicationSettings | None = None) -> FastAPI:
115115    app .add_event_handler ("startup" , on_startup )
116116    app .add_event_handler ("shutdown" , on_shutdown )
117117
118-     exceptions .setup_exception_handlers (
119-         app , is_debug = settings .SC_BOOT_MODE  ==  BootModeEnum .DEBUG 
120-     )
121- 
122118    if  settings .API_SERVER_PROFILING :
123119        initialize_profiler (app )
124120
125121    if  app .state .settings .API_SERVER_PROMETHEUS_INSTRUMENTATION_ENABLED :
126122        setup_prometheus_instrumentation (app )
127123
124+     exceptions .setup_exception_handlers (
125+         app , is_debug = settings .SC_BOOT_MODE  ==  BootModeEnum .DEBUG 
126+     )
127+ 
128128    # routing 
129129
130130    # healthcheck at / and at /VTAG/ 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments