Skip to content

Commit

Permalink
feat(metrics): init zero for request count of apis (#5186)
Browse files Browse the repository at this point in the history
* feat(metrics): init zero for request count of apis

* fix: use route instead of api name
  • Loading branch information
Haivilo authored Jan 17, 2025
1 parent 722bdc5 commit 1f1d8d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/_bentoml_impl/worker/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def main(
BentoMLContainer.development_mode.set(development_mode)

server_context.service_name = service.name
server_context.service_routes = [api.route for api in service.apis.values()]
if service.bento is None:
server_context.bento_name = service.name
server_context.bento_version = "not available"
Expand Down
1 change: 1 addition & 0 deletions src/bentoml/_internal/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class _ComponentContext:
service_type: str | None = None
service_name: str | None = None
worker_index: int | None = None
service_routes: list[str] = attr.field(factory=list)

@property
def component_type(self) -> str | None:
Expand Down
17 changes: 17 additions & 0 deletions src/bentoml/_internal/server/http/instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,23 @@ def _setup(
documentation="Total number of bytes sent to websocket",
labelnames=["endpoint", "service_name", "service_version", "runner_name"],
)
# place holder metrics to initialize endpoints with 0 to facilitate rate calculation
# otherwise, rate() can be 0 if the endpoint is hit for the first time
for endpoint in server_context.service_routes:
self.metrics_request_total.labels(
endpoint=endpoint,
service_name=server_context.bento_name,
service_version=server_context.bento_version,
http_response_code=200,
runner_name=server_context.service_name,
)
self.metrics_request_total.labels(
endpoint=endpoint,
service_name=server_context.bento_name,
service_version=server_context.bento_version,
http_response_code=500,
runner_name=server_context.service_name,
)
self._is_setup = True

async def __call__(
Expand Down

0 comments on commit 1f1d8d4

Please sign in to comment.