Skip to content

Commit c9593af

Browse files
authored
fix: usage tracking for bentoml build (#5321)
Signed-off-by: Frost Ming <me@frostming.com>
1 parent e1cc943 commit c9593af

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed
Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import typing as t
1+
from __future__ import annotations
2+
23
from typing import TYPE_CHECKING
34

4-
from ..filesystem import calc_dir_size
55
from .schemas import BentoBuildEvent
66

77
if TYPE_CHECKING:
@@ -11,26 +11,31 @@
1111
def _cli_bentoml_build_event(
1212
cmd_group: str,
1313
cmd_name: str,
14-
return_value: "t.Optional[Bento]",
14+
return_value: Bento | None,
1515
) -> BentoBuildEvent: # pragma: no cover
16-
if return_value is not None:
17-
bento = return_value
18-
return BentoBuildEvent(
19-
cmd_group=cmd_group,
20-
cmd_name=cmd_name,
21-
bento_creation_timestamp=bento.info.creation_time,
22-
bento_size_in_kb=calc_dir_size(bento.path_of("/")) / 1024,
23-
model_size_in_kb=calc_dir_size(bento.path_of("/models")) / 1024,
24-
num_of_models=len(bento.info.all_models),
25-
num_of_runners=len(bento.info.runners),
26-
model_types=[m.module for m in bento.info.all_models],
27-
runnable_types=[r.runnable_type for r in bento.info.runners],
28-
)
16+
from ...bento.bento import BentoInfo
17+
from ...bento.bento import BentoInfoV2
18+
19+
if return_value is None:
20+
return BentoBuildEvent(cmd_group=cmd_group, cmd_name=cmd_name)
21+
bento = return_value
22+
total_size = bento.total_size()
23+
if isinstance(bento.info, BentoInfoV2):
24+
num_of_runners = len(bento.info.services) - 1
25+
elif isinstance(bento.info, BentoInfo):
26+
num_of_runners = len(bento.info.runners)
2927
else:
30-
return BentoBuildEvent(
31-
cmd_group=cmd_group,
32-
cmd_name=cmd_name,
33-
)
28+
num_of_runners = 0
29+
return BentoBuildEvent(
30+
cmd_group=cmd_group,
31+
cmd_name=cmd_name,
32+
bento_creation_timestamp=bento.info.creation_time,
33+
bento_size_in_kb=bento.file_size / 1024,
34+
model_size_in_kb=(total_size - bento.file_size) / 1024,
35+
num_of_models=len(bento.info.all_models),
36+
num_of_runners=num_of_runners,
37+
model_types=[m.module or "" for m in bento.info.all_models],
38+
)
3439

3540

36-
cli_events_map = {"cli": {"build": _cli_bentoml_build_event}}
41+
cli_events_map = {"bentos": {"build": _cli_bentoml_build_event}}

src/bentoml/_internal/utils/analytics/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ class CliEvent(EventMeta):
155155

156156
@attr.define
157157
class BentoBuildEvent(CliEvent):
158+
bentoml_version: str = BENTOML_VERSION
158159
bento_creation_timestamp: t.Optional[datetime] = attr.field(default=None)
159160
bento_size_in_kb: float = attr.field(default=0)
160161
model_size_in_kb: float = attr.field(default=0)
161162

162163
num_of_models: int = attr.field(default=0)
163164
num_of_runners: int = attr.field(default=0)
164165
model_types: t.List[str] = attr.field(factory=list)
165-
runnable_types: t.List[str] = attr.field(factory=list)
166166

167167

168168
@attr.define

0 commit comments

Comments
 (0)