77
88import sentry_sdk
99import uvloop
10- from prometheus_sanic import monitor
1110from sanic import Sanic
1211from sanic .log import logger
1312from sanic .worker .loader import AppLoader
1716from renku_data_services .app_config import Config
1817from renku_data_services .authz .admin_sync import sync_admins_from_keycloak
1918from renku_data_services .data_api .app import register_all_handlers
19+ from renku_data_services .data_api .prometheus import collect_system_metrics , setup_app_metrics , setup_prometheus
2020from renku_data_services .errors .errors import (
2121 ForbiddenError ,
2222 MissingResourceError ,
3131 import sentry_sdk ._types
3232
3333
34- async def _send_messages () -> None :
34+ async def _send_messages (app : Sanic ) -> None :
3535 config = Config .from_env ()
3636 while True :
3737 try :
3838 await config .event_repo .send_pending_events ()
39+ # we need to collect metrics for this background process separately from the task we add to the
40+ # server processes
41+ await collect_system_metrics (app , "send_events_worker" )
3942 await asyncio .sleep (1 )
4043 except (asyncio .CancelledError , KeyboardInterrupt ) as e :
4144 logger .warning (f"Exiting: { e } " )
@@ -45,14 +48,15 @@ async def _send_messages() -> None:
4548 raise
4649
4750
48- def send_pending_events () -> None :
51+ def send_pending_events (app_name : str ) -> None :
4952 """Send pending messages in case sending in a handler failed."""
50- _ = Sanic ("send_events" ) # we need a dummy app for logging to work.
53+ app = Sanic (app_name )
54+ setup_app_metrics (app )
5155
5256 logger .info ("running events sending loop." )
5357
5458 asyncio .set_event_loop (uvloop .new_event_loop ())
55- asyncio .run (_send_messages ())
59+ asyncio .run (_send_messages (app ))
5660
5761
5862def create_app () -> Sanic :
@@ -104,9 +108,7 @@ async def setup_sentry(_: Sanic) -> None:
104108 logger .info (f"REAL_IP_HEADER = { app .config .REAL_IP_HEADER } " )
105109
106110 app = register_all_handlers (app , config )
107-
108- # Setup prometheus
109- monitor (app , endpoint_type = "url" , multiprocess_mode = "all" , is_middleware = True ).expose_endpoint ()
111+ setup_prometheus (app )
110112
111113 if environ .get ("CORS_ALLOW_ALL_ORIGINS" , "false" ).lower () == "true" :
112114 from sanic_ext import Extend
@@ -134,7 +136,7 @@ async def setup_rclone_validator(app: Sanic) -> None:
134136 async def ready (app : Sanic ) -> None :
135137 """Application ready event handler."""
136138 logger .info ("starting events background job." )
137- app .manager .manage ("SendEvents" , send_pending_events , {}, transient = True )
139+ app .manager .manage ("SendEvents" , send_pending_events , {"app_name" : config . app_name }, transient = True )
138140
139141 return app
140142
0 commit comments