Skip to content

Commit

Permalink
chore: wip [skip ci]
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Oct 28, 2022
1 parent 09163e1 commit 484d32e
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 91 deletions.
1 change: 0 additions & 1 deletion requirements/tests-requirements.txt
Expand Up @@ -17,4 +17,3 @@ imageio==2.22.1
pyarrow==9.0.0
build[virtualenv]==0.8.0
protobuf==3.19.6
grpcio-tools>=1.41.0,<1.49.0,!=1.48.2
11 changes: 7 additions & 4 deletions src/bentoml/metrics.py
Expand Up @@ -65,7 +65,9 @@ def __getattr__(item: t.Any):
class _LazyMetric:
__slots__ = ("_attr", "_proxy", "_initialized", "_args", "_kwargs")

def __init__(self, attr: t.Any):
def __init__(self, attr: str):
if attr in _NOT_SUPPORTED:
raise NotImplementedError(f"{attr} is not yet supported.")
self._attr = attr
self._proxy = None
self._initialized = False
Expand All @@ -81,16 +83,17 @@ def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
*args: Arguments to pass to the metrics object.
**kwargs: Keyword arguments to pass to the metrics object.
"""
# This is where we lazy load the proxy object.
if "registry" in kwargs:
raise ValueError(
f"'registry' should not be passed when using '{__name__}.{self._attr}'. See https://docs.bentoml.org/en/latest/reference/metrics.html."
)
warn_experimental("%s.%s" % (__name__, self._attr))
self._args = args
self._kwargs = kwargs
if self._attr in _INTERNAL_IMPL:
# first-class function implementation from BentoML Prometheus client.
# In this case, the function will be called directly.
return self._load_proxy()
if self._attr in _NOT_SUPPORTED:
raise NotImplementedError(f"{self._attr} is not supported, yet.")
return self

def __getattr__(self, item: t.Any) -> t.Any:
Expand Down
9 changes: 5 additions & 4 deletions src/bentoml/serve.py
Expand Up @@ -190,10 +190,10 @@ def serve_http_development(
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve

prometheus_dir = ensure_prometheus_dir()
working_dir = os.path.realpath(os.path.expanduser(working_dir))
svc = load(bento_identifier, working_dir=working_dir)

prometheus_dir = ensure_prometheus_dir()
watchers: list[Watcher] = []
circus_sockets: list[CircusSocket] = [
CircusSocket(name=API_SERVER_NAME, host=host, port=port, backlog=backlog)
Expand Down Expand Up @@ -310,13 +310,13 @@ def serve_http_production(
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve

prometheus_dir = ensure_prometheus_dir()
working_dir = os.path.realpath(os.path.expanduser(working_dir))
svc = load(bento_identifier, working_dir=working_dir, standalone_load=True)
watchers: t.List[Watcher] = []
circus_socket_map: t.Dict[str, CircusSocket] = {}
runner_bind_map: t.Dict[str, str] = {}
uds_path = None
prometheus_dir = ensure_prometheus_dir()

if psutil.POSIX:
# use AF_UNIX sockets for Circus
Expand Down Expand Up @@ -505,10 +505,10 @@ def serve_grpc_development(
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve

prometheus_dir = ensure_prometheus_dir()
working_dir = os.path.realpath(os.path.expanduser(working_dir))
svc = load(bento_identifier, working_dir=working_dir)

prometheus_dir = ensure_prometheus_dir()
watchers: list[Watcher] = []
circus_sockets: list[CircusSocket] = []

Expand Down Expand Up @@ -681,7 +681,6 @@ def serve_grpc_production(
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve

prometheus_dir = ensure_prometheus_dir()
working_dir = os.path.realpath(os.path.expanduser(working_dir))
svc = load(bento_identifier, working_dir=working_dir, standalone_load=True)

Expand All @@ -692,6 +691,8 @@ def serve_grpc_production(
runner_bind_map: dict[str, str] = {}
uds_path = None

prometheus_dir = ensure_prometheus_dir()

# Check whether users are running --grpc on windows
# also raising warning if users running on MacOS or FreeBSD
if psutil.WINDOWS:
Expand Down
9 changes: 4 additions & 5 deletions src/bentoml/start.py
Expand Up @@ -42,7 +42,6 @@ def start_runner_server(
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve

prometheus_dir = ensure_prometheus_dir()
working_dir = os.path.realpath(os.path.expanduser(working_dir))
svc = load(bento_identifier, working_dir=working_dir, standalone_load=True)

Expand All @@ -52,6 +51,8 @@ def start_runner_server(
watchers: t.List[Watcher] = []
circus_socket_map: t.Dict[str, CircusSocket] = {}

ensure_prometheus_dir()

with contextlib.ExitStack() as port_stack:
for runner in svc.runners:
if runner.name == runner_name:
Expand Down Expand Up @@ -83,8 +84,6 @@ def start_runner_server(
"--no-access-log",
"--worker-id",
"$(circus.wid)",
"--prometheus-dir",
prometheus_dir,
],
copy_env=True,
stop_children=True,
Expand Down Expand Up @@ -144,7 +143,6 @@ def start_http_server(
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve

prometheus_dir = ensure_prometheus_dir()
working_dir = os.path.realpath(os.path.expanduser(working_dir))
svc = load(bento_identifier, working_dir=working_dir, standalone_load=True)
runner_requirements = {runner.name for runner in svc.runners}
Expand All @@ -154,6 +152,7 @@ def start_http_server(
)
watchers: t.List[Watcher] = []
circus_socket_map: t.Dict[str, CircusSocket] = {}
prometheus_dir = ensure_prometheus_dir()
logger.debug("Runner map: %s", runner_map)
circus_socket_map[API_SERVER_NAME] = CircusSocket(
name=API_SERVER_NAME,
Expand Down Expand Up @@ -252,7 +251,6 @@ def start_grpc_server(
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve

prometheus_dir = ensure_prometheus_dir()
working_dir = os.path.realpath(os.path.expanduser(working_dir))
svc = load(bento_identifier, working_dir=working_dir, standalone_load=True)
runner_requirements = {runner.name for runner in svc.runners}
Expand All @@ -262,6 +260,7 @@ def start_grpc_server(
)
watchers: list[Watcher] = []
circus_socket_map: dict[str, CircusSocket] = {}
prometheus_dir = ensure_prometheus_dir()
logger.debug("Runner map: %s", runner_map)
ssl_args = construct_ssl_args(
ssl_certfile=ssl_certfile,
Expand Down
13 changes: 7 additions & 6 deletions src/bentoml/testing/server.py
Expand Up @@ -187,27 +187,28 @@ def run_bento_server_docker(
enable_so_reuseport=use_grpc
) as prom_port:
pass
bind_port = "3000"

cmd = [
"docker",
"run",
"--rm",
"--name",
container_name,
"--publish",
f"{port}:{bind_port}",
f"{port}:3000",
]
if config_file is not None:
cmd.extend(["--env", "BENTOML_CONFIG=/home/bentoml/bentoml_config.yml"])
cmd.extend(
["-v", f"{os.path.abspath(config_file)}:/home/bentoml/bentoml_config.yml"]
)
if use_grpc:
bind_prom_port = BentoMLContainer.grpc.metrics.port.get()
cmd.extend(["--publish", f"{prom_port}:{bind_prom_port}"])
cmd.extend(
["--publish", f"{prom_port}:{BentoMLContainer.grpc.metrics.port.get()}"]
)
cmd.append(image_tag)
if use_grpc:
cmd.extend(["serve-grpc", "--production"])
serve_cmd = "serve-grpc" if use_grpc else "serve-http"
cmd.extend([serve_cmd, "--production"])
print(f"Running API server docker image: '{' '.join(cmd)}'")
with subprocess.Popen(
cmd,
Expand Down
3 changes: 1 addition & 2 deletions src/bentoml_cli/worker/grpc_prometheus_server.py
Expand Up @@ -68,11 +68,10 @@ def main(fd: int, backlog: int, prometheus_dir: str | None):
configure_server_logging()

BentoMLContainer.development_mode.set(False)
metrics_client = BentoMLContainer.metrics_client.get()
if prometheus_dir is not None:
BentoMLContainer.prometheus_multiproc_dir.set(prometheus_dir)

metrics_client = BentoMLContainer.metrics_client.get()

# create a ASGI app that wraps around the default HTTP prometheus server.
prom_app = Starlette(
debug=get_debug_mode(), middleware=[Middleware(GenerateLatestMiddleware)]
Expand Down
20 changes: 2 additions & 18 deletions tests/e2e/bento_server_grpc/service.py
Expand Up @@ -175,20 +175,6 @@ async def echo_image(f: PIL.Image.Image) -> NDArray[t.Any]:
return np.array(f)


@svc.api(
input=Multipart(
original=Image(mime_type="image/bmp"), compared=Image(mime_type="image/bmp")
),
output=Multipart(meta=Text(), result=Image(mime_type="image/bmp")),
)
async def predict_multi_images(original: Image, compared: Image):
output_array = await py_model.multiply_float_ndarray.async_run(
np.array(original), np.array(compared)
)
img = PIL.Image.fromarray(output_array)
return {"meta": "success", "result": img}


histogram = bentoml.metrics.Histogram(
name="inference_latency",
documentation="Inference latency in seconds",
Expand All @@ -201,11 +187,9 @@ async def predict_multi_images(original: Image, compared: Image):
input=Multipart(
original=Image(mime_type="image/bmp"), compared=Image(mime_type="image/bmp")
),
output=bentoml.io.Multipart(
meta=bentoml.io.Text(), result=bentoml.io.Image(mime_type="image/bmp")
),
output=Multipart(meta=Text(), result=Image(mime_type="image/bmp")),
)
async def multi_image_with_metrics(original: Image, compared: Image):
async def predict_multi_images(original: Image, compared: Image):
start = time.perf_counter()
output_array = await py_model.multiply_float_ndarray.async_run(
np.array(original), np.array(compared)
Expand Down
3 changes: 3 additions & 0 deletions tests/e2e/bento_server_grpc/tests/conftest.py
Expand Up @@ -24,6 +24,9 @@
def pytest_collection_modifyitems(
session: Session, config: Config, items: list[Item]
) -> None:
subprocess.check_call(
["pip", "install", "-r", f"{os.path.join(PROJECT_DIR, 'requirements.txt')}"]
)
subprocess.check_call([sys.executable, f"{os.path.join(PROJECT_DIR, 'train.py')}"])


Expand Down
48 changes: 0 additions & 48 deletions tests/e2e/bento_server_grpc/tests/test_custom_metrics.py

This file was deleted.

9 changes: 9 additions & 0 deletions tests/e2e/bento_server_grpc/tests/test_descriptors.py
Expand Up @@ -8,6 +8,7 @@

import pytest

from bentoml import metrics
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs
from bentoml.testing.grpc import create_channel
Expand Down Expand Up @@ -394,3 +395,11 @@ async def test_multipart(host: str, img_file: str):
assert_multi_images, method="pred_multi_images", im_file=img_file
),
)

# Test for metrics
histograms = [
m.name
for m in metrics.text_string_to_metric_families()
if m.type == "histogram"
]
assert "inference_latency" in histograms
3 changes: 3 additions & 0 deletions tests/e2e/bento_server_http/tests/conftest.py
Expand Up @@ -27,6 +27,9 @@ class FixtureRequest(_PytestFixtureRequest):
def pytest_collection_modifyitems(
session: Session, config: Config, items: list[Item]
) -> None:
subprocess.check_call(
["pip", "install", "-r", f"{os.path.join(PROJECT_DIR, 'requirements.txt')}"]
)
subprocess.check_call([sys.executable, f"{os.path.join(PROJECT_DIR, 'train.py')}"])


Expand Down
3 changes: 0 additions & 3 deletions tests/e2e/bento_server_http/tests/test_meta.py
Expand Up @@ -12,9 +12,6 @@
import bentoml
from bentoml.testing.utils import async_request

if TYPE_CHECKING:
from bentoml._internal.server.metrics.prometheus import PrometheusClient


@pytest.mark.asyncio
async def test_api_server_meta(host: str) -> None:
Expand Down

0 comments on commit 484d32e

Please sign in to comment.