Skip to content

Commit

Permalink
feat: track serve update for start subcommands (bentoml#2976)
Browse files Browse the repository at this point in the history
## What does this PR address?
<!--
Thanks for sending a pull request!

Congrats for making it this far! Here's a 🍱 for you. There are still a
few steps ahead.

Please make sure to read the contribution guidelines, then fill out the
blanks below before requesting a code review.

Name your Pull Request with one of the following prefixes, e.g. "feat:
add support for PyTorch", to indicate the type of changes proposed. This
is based on the [Conventional Commits
specification](https://www.conventionalcommits.org/en/v1.0.0/#summary).
  - feat: (new feature for the user, not a new feature for build script)
  - fix: (bug fix for the user, not a fix to a build script)
  - docs: (changes to the documentation)
- style: (formatting, missing semicolons, etc; no production code
change)
  - refactor: (refactoring production code, eg. renaming a variable)
  - perf: (code changes that improve performance)
- test: (adding missing tests, refactoring tests; no production code
change)
  - chore: (updating grunt tasks etc; no production code change)
- build: (changes that affect the build system or external dependencies)
  - ci: (changes to configuration files and scripts)
  - revert: (reverts a previous commit)

Describe your changes in detail. Attach screenshots here if appropriate.

Once you're done with this, someone from BentoML team or community
member will help review your PR (see "Who can help review?" section for
potential reviewers.). If no one has reviewed your PR after a week have
passed, don't hesitate to post a new comment and ping @-the same person.
Notifications sometimes get lost 🥲.
-->

<!-- Remove if not applicable -->
Fixes #(issue)

## Before submitting:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
<!--- If you plan to update documentation or tests in follow-up, please
note -->
- [ ] Does the Pull Request follow [Conventional Commits
specification](https://www.conventionalcommits.org/en/v1.0.0/#summary)
naming? Here are [GitHub's

guide](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request)
on how to create a pull request.
- [ ] Does the code follow BentoML's code style, both `make format` and
`make lint` script have passed
([instructions](https://github.com/bentoml/BentoML/blob/main/DEVELOPMENT.md#style-check-auto-formatting-type-checking))?
- [ ] Did you read through [contribution
guidelines](https://github.com/bentoml/BentoML/blob/main/CONTRIBUTING.md#ways-to-contribute)
and follow [development
guidelines](https://github.com/bentoml/BentoML/blob/main/DEVELOPMENT.md#start-developing)?
- [ ] Did your changes require updates to the documentation? Have you
updated
those accordingly? Here are [documentation
guidelines](https://github.com/bentoml/BentoML/tree/main/docs) and [tips
on writting
docs](https://github.com/bentoml/BentoML/tree/main/docs#writing-documentation).
- [ ] Did you write tests to cover your changes?

## Who can help review?

Feel free to tag members/contributors who can help review your PR.
<!--
Feel free to ping any of the BentoML members for help on your issue, but
don't ping more than three people 😊.
If you know how to use git blame, that is probably the easiest way.

Team members that you can ping:
- @parano
- @yubozhao
- @bojiang
- @ssheng
- @aarnphm
- @sauyon
- @larme
- @yetone
- @jjmachan
-->
  • Loading branch information
ssheng authored and jiewpeng committed Sep 12, 2022
1 parent f0ec881 commit 098daa2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
1 change: 1 addition & 0 deletions bentoml/_internal/utils/analytics/schemas.py
Expand Up @@ -199,6 +199,7 @@ class ServeInitEvent(EventMeta):
class ServeUpdateEvent(EventMeta):
serve_id: str
production: bool
component: str
triggered_at: datetime
duration_in_seconds: int
metrics: t.List[t.Any] = attr.field(factory=list)
Expand Down
2 changes: 2 additions & 0 deletions bentoml/_internal/utils/analytics/usage_stats.py
Expand Up @@ -196,6 +196,7 @@ def get_metrics_report(
def track_serve(
svc: Service,
production: bool,
component: str = "standalone",
metrics_client: PrometheusClient = Provide[BentoMLContainer.metrics_client],
serve_info: ServeInfo = Provide[BentoMLContainer.serve_info],
) -> t.Generator[None, None, None]:
Expand All @@ -220,6 +221,7 @@ def loop() -> t.NoReturn: # type: ignore
event_properties = ServeUpdateEvent(
serve_id=serve_info.serve_id,
production=production,
component=component,
triggered_at=now,
duration_in_seconds=int((now - last_tracked_timestamp).total_seconds()),
metrics=get_metrics_report(metrics_client),
Expand Down
54 changes: 30 additions & 24 deletions bentoml/start.py
Expand Up @@ -19,6 +19,7 @@
from ._internal.utils import reserve_free_port
from ._internal.resource import CpuResource
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve
from ._internal.configuration.containers import BentoMLContainer

logger = logging.getLogger(__name__)
Expand All @@ -27,6 +28,9 @@
SCRIPT_API_SERVER = "bentoml_cli.worker.http_api_server"
SCRIPT_DEV_API_SERVER = "bentoml_cli.worker.http_dev_api_server"

API_SERVER = "api_server"
RUNNER = "runner"


@inject
def ensure_prometheus_dir(
Expand Down Expand Up @@ -114,7 +118,7 @@ def start_runner_server(

watchers.append(
Watcher(
name=f"runner_{runner.name}",
name=f"{RUNNER}_{runner.name}",
cmd=sys.executable,
args=[
"-m",
Expand Down Expand Up @@ -149,18 +153,19 @@ def start_runner_server(
sockets=list(circus_socket_map.values()),
)

try:
arbiter.start(
cb=lambda _: logger.info( # type: ignore
'Starting RunnerServer from "%s"\n running on http://%s:%s (Press CTRL+C to quit)',
bento_identifier,
host,
port,
),
)
finally:
if uds_path is not None:
shutil.rmtree(uds_path)
with track_serve(svc, production=True, component=RUNNER):
try:
arbiter.start(
cb=lambda _: logger.info( # type: ignore
'Starting RunnerServer from "%s"\n running on http://%s:%s (Press CTRL+C to quit)',
bento_identifier,
host,
port,
),
)
finally:
if uds_path is not None:
shutil.rmtree(uds_path)


@inject
Expand Down Expand Up @@ -248,7 +253,7 @@ def start_http_server(

watchers.append(
Watcher(
name="api_server",
name=API_SERVER,
cmd=sys.executable,
args=args,
copy_env=True,
Expand All @@ -264,13 +269,14 @@ def start_http_server(
sockets=list(circus_socket_map.values()),
)

try:
arbiter.start(
cb=lambda _: logger.info( # type: ignore
f'Starting bare Bento API server from "{bento_identifier}" '
f"running on http://{host}:{port} (Press CTRL+C to quit)"
),
)
finally:
if uds_path is not None:
shutil.rmtree(uds_path)
with track_serve(svc, production=True, component=API_SERVER):
try:
arbiter.start(
cb=lambda _: logger.info( # type: ignore
f'Starting bare Bento API server from "{bento_identifier}" '
f"running on http://{host}:{port} (Press CTRL+C to quit)"
),
)
finally:
if uds_path is not None:
shutil.rmtree(uds_path)

0 comments on commit 098daa2

Please sign in to comment.