From f34ab8fb4a1ee906346c29d1ed250470484768f0 Mon Sep 17 00:00:00 2001 From: xianml Date: Mon, 27 May 2024 15:54:29 +0800 Subject: [PATCH] fix: metrics did not include 4xx --- src/_bentoml_impl/server/app.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/_bentoml_impl/server/app.py b/src/_bentoml_impl/server/app.py index d08367d02c4..a3b96006d43 100644 --- a/src/_bentoml_impl/server/app.py +++ b/src/_bentoml_impl/server/app.py @@ -203,9 +203,19 @@ def middlewares(self) -> list[Middleware]: from bentoml._internal.container import BentoMLContainer - middlewares = super().middlewares + [ - Middleware(ContextMiddleware, context=self.service.context) - ] + middlewares: list[Middleware] = [] + # TrafficMetrics middleware should be the first middleware + if self.enable_metrics: + from bentoml._internal.server.http.instruments import ( + RunnerTrafficMetricsMiddleware, + ) + + middlewares.append( + Middleware(RunnerTrafficMetricsMiddleware, namespace="bentoml_service") + ) + + middlewares.extend(super().middlewares) + middlewares.append(Middleware(ContextMiddleware, context=self.service.context)) for middleware_cls, options in self.service.middlewares: middlewares.append(Middleware(middleware_cls, **options)) @@ -238,15 +248,6 @@ def server_request_hook(span: Span | None, _scope: dict[str, t.Any]) -> None: ) ) - if self.enable_metrics: - from bentoml._internal.server.http.instruments import ( - RunnerTrafficMetricsMiddleware, - ) - - middlewares.append( - Middleware(RunnerTrafficMetricsMiddleware, namespace="bentoml_service") - ) - access_log_config = BentoMLContainer.api_server_config.logging.access if access_log_config.enabled.get(): from bentoml._internal.server.http.access import AccessLogMiddleware