11"""HTTP adapter for syncing a schedule reminder's final confirmed state."""
22
33import logging
4+ import time
45from collections .abc import Callable , Coroutine
56from datetime import UTC , datetime
67from typing import Annotated , Any , Literal
2425 AuthenticatedAccount ,
2526 AuthenticatedAccountDependency ,
2627)
28+ from timeflow .gateway .observability .http import record_reminder_state
2729
2830logger = logging .getLogger (__name__ )
2931
@@ -91,9 +93,11 @@ def get_route_handler(
9193 original_handler = super ().get_route_handler ()
9294
9395 async def route_handler (request : Request ) -> Response :
96+ started = time .perf_counter ()
9497 try :
9598 return await original_handler (request )
9699 except RequestValidationError :
100+ record_reminder_state ("invalid" , time .perf_counter () - started )
97101 response = ReminderStateInvalidRequestResponse ()
98102 return JSONResponse (
99103 status_code = 422 ,
@@ -135,6 +139,7 @@ def confirm_reminder_state(
135139 request : ReminderStateRequest ,
136140 account : Annotated [AuthenticatedAccount , Security (trusted_account )],
137141 ) -> Response :
142+ started = time .perf_counter ()
138143 try :
139144 result = confirmer .confirm (
140145 account_id = account .account_id ,
@@ -145,18 +150,23 @@ def confirm_reminder_state(
145150 disposition_state = result .disposition_state ,
146151 updated_at = _as_utc (result .updated_at ),
147152 )
153+ record_reminder_state ("confirmed" , time .perf_counter () - started )
148154 return JSONResponse (status_code = 200 , content = response .model_dump (mode = "json" ))
149155 except ScheduleBusinessError as error :
150156 if error .code is ScheduleErrorCode .SCHEDULE_NOT_FOUND :
157+ record_reminder_state ("not_found" , time .perf_counter () - started )
151158 return _error_response (404 , "SCHEDULE_NOT_FOUND" , "Schedule not found" )
152159 if error .code is ScheduleErrorCode .REMINDER_NOT_CONFIGURED :
160+ record_reminder_state ("conflict" , time .perf_counter () - started )
153161 return _error_response (
154162 409 ,
155163 "REMINDER_NOT_CONFIGURED" ,
156164 "Reminder not configured" ,
157165 )
166+ record_reminder_state ("error" , time .perf_counter () - started )
158167 return _internal_error_response (error )
159168 except Exception as error :
169+ record_reminder_state ("error" , time .perf_counter () - started )
160170 return _internal_error_response (error )
161171
162172 return router
0 commit comments