Skip to content

Commit ffe5a09

Browse files
authored
refactor replica _handle_errors_and_metrics (#51644)
Signed-off-by: Abrar Sheikh <abrar@anyscale.com>
1 parent e662459 commit ffe5a09

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

python/ray/serve/_private/replica.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,20 +483,36 @@ def _status_code_callback(s: str):
483483
self._metrics_manager.dec_num_ongoing_requests()
484484

485485
latency_ms = (time.time() - start_time) * 1000
486+
self._record_errors_and_metrics(
487+
user_exception, status_code, latency_ms, request_metadata, request_args
488+
)
489+
490+
if user_exception is not None:
491+
raise user_exception from None
492+
493+
def _record_errors_and_metrics(
494+
self,
495+
user_exception: Optional[BaseException],
496+
status_code: Optional[str],
497+
latency_ms: float,
498+
request_metadata: RequestMetadata,
499+
request_args: Tuple[Any],
500+
):
501+
http_method = self._maybe_get_http_method(request_metadata, request_args)
502+
http_route = request_metadata.route
503+
call_method = request_metadata.call_method
486504
if user_exception is None:
487505
status_str = "OK"
488506
elif isinstance(user_exception, asyncio.CancelledError):
489507
status_str = "CANCELLED"
490508
else:
491509
status_str = "ERROR"
492510

493-
http_method = self._maybe_get_http_method(request_metadata, request_args)
494-
http_route = request_metadata.route
495511
# Set in _wrap_user_method_call.
496512
logger.info(
497513
access_log_msg(
498514
method=http_method or "CALL",
499-
route=http_route or request_metadata.call_method,
515+
route=http_route or call_method,
500516
# Prefer the HTTP status code if it was populated.
501517
status=status_code or status_str,
502518
latency_ms=latency_ms,
@@ -509,9 +525,6 @@ def _status_code_callback(s: str):
509525
was_error=user_exception is not None,
510526
)
511527

512-
if user_exception is not None:
513-
raise user_exception from None
514-
515528
async def _call_user_generator(
516529
self,
517530
request_metadata: RequestMetadata,

0 commit comments

Comments
 (0)