Skip to content

Commit

Permalink
chore: ensure to pass prometheus_dir to runner [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 27, 2022
1 parent d31c287 commit 1a9bbe7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Expand Up @@ -49,7 +49,8 @@ dependencies = [
"pathspec",
"pip-tools>=6.6.2",
"pip-requirements-parser>=31.2.0",
"prometheus-client>=0.10.0",
# Lowest version of prometheus_client that supports 3.10
"prometheus-client>=0.12.0",
"psutil",
"pynvml<12",
"python-dateutil",
Expand Down
16 changes: 16 additions & 0 deletions src/bentoml/_internal/server/metrics/prometheus.py
Expand Up @@ -96,9 +96,25 @@ def start_http_server(self, port: int, addr: str = "") -> None:
registry=self.registry,
)

start_wsgi_server = start_http_server

def write_to_textfile(self, path: str) -> None:
"""
Write metrics to given path. This is intended to be used with
the Node expoerter textfile collector.
Args:
path: path to write the metrics to. This file must end
with '.prom' for the textfile collector to process it.
"""
self.prometheus_client.write_to_textfile(path, registry=self.registry)

def make_wsgi_app(self) -> ext.WSGIApp:
return self.prometheus_client.make_wsgi_app(registry=self.registry) # type: ignore (unfinished prometheus types)

def make_asgi_app(self) -> ext.ASGIApp:
return self.prometheus_client.make_asgi_app(registry=self.registry) # type: ignore (unfinished prometheus types)

def generate_latest(self):
if self.multiproc:
registry = self.prometheus_client.CollectorRegistry()
Expand Down
33 changes: 18 additions & 15 deletions src/bentoml/metrics.py
Expand Up @@ -20,23 +20,26 @@
# This sets of functions are implemented in the PrometheusClient class
_INTERNAL_IMPL = [
"start_http_server",
"start_wsgi_server",
"make_wsgi_app",
"make_asgi_app",
"generate_latest",
"text_string_to_metric_families",
"write_to_textfile",
]
_NOT_IMPLEMENTED = [
"delete_from_gateway",
"instance_ip_grouping_key",
"make_asgi_app",
"push_to_gateway",
"pushadd_to_gateway",
"start_wsgi_server",
"write_to_textfile",
]
_NOT_SUPPORTED = [
"GC_COLLECTOR",
"GCCollector",
"PLATFORM_COLLECTOR",
"PlatformCollector",
"PROCESS_COLLECTOR",
"ProcessCollector",
"REGISTRY",
] + _NOT_IMPLEMENTED
_PROPERTY = ["CONTENT_TYPE_LATEST"]
Expand All @@ -45,8 +48,18 @@
def __dir__() -> list[str]:
# This is for IPython and IDE autocompletion.
metrics_client = BentoMLContainer.metrics_client.get()
_dir: set[str] = set(dir(metrics_client.prometheus_client)) - set(_NOT_SUPPORTED)
return list(_dir)
return list(set(dir(metrics_client.prometheus_client)) - set(_NOT_SUPPORTED))


def __getattr__(item: t.Any):
# This is the entrypoint for all bentoml.metrics.*
if item in _PROPERTY:
logger.warning(
"'%s' is a '@property', which means there is no lazy loading. See https://docs.bentoml.org/en/latest/guides/metrics.html.",
item,
)
return getattr(_LazyMetric(item), item)
return _LazyMetric(item)


class _LazyMetric:
Expand Down Expand Up @@ -111,13 +124,3 @@ def _load_proxy(
proxy = proxy(*self._args, **self._kwargs)
self._initialized = True
return proxy


def __getattr__(item: t.Any):
if item in _PROPERTY:
logger.warning(
"'%s' is a '@property', which means there is no lazy loading. See https://docs.bentoml.org/en/latest/guides/metrics.html.",
item,
)
return getattr(_LazyMetric(item), item)
return _LazyMetric(item)
4 changes: 3 additions & 1 deletion src/bentoml/start.py
Expand Up @@ -42,7 +42,7 @@ def start_runner_server(
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve

ensure_prometheus_dir()
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 Down Expand Up @@ -83,6 +83,8 @@ def start_runner_server(
"--no-access-log",
"--worker-id",
"$(circus.wid)",
"--prometheus-dir",
prometheus_dir,
],
copy_env=True,
stop_children=True,
Expand Down
3 changes: 2 additions & 1 deletion src/bentoml_cli/worker/grpc_prometheus_server.py
Expand Up @@ -68,10 +68,11 @@ 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

0 comments on commit 1a9bbe7

Please sign in to comment.