Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rename server context attributes #4553

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/_bentoml_impl/client/http.py
Original file line number Diff line number Diff line change
@@ -145,14 +145,14 @@ def __init__(
stream_output=method.is_stream,
)

from bentoml._internal.context import component_context
from bentoml._internal.context import server_context

const_headers = {
"Bento-Name": component_context.bento_name,
"Bento-Version": component_context.bento_version,
"Bento-Name": server_context.bento_name,
"Bento-Version": server_context.bento_version,
"Runner-Name": service.name,
"Yatai-Bento-Deployment-Name": component_context.yatai_bento_deployment_name,
"Yatai-Bento-Deployment-Namespace": component_context.yatai_bento_deployment_namespace,
"Yatai-Bento-Deployment-Name": server_context.yatai_bento_deployment_name,
"Yatai-Bento-Deployment-Namespace": server_context.yatai_bento_deployment_namespace,
}
self.__attrs_init__( # type: ignore
url=url,
8 changes: 4 additions & 4 deletions src/_bentoml_impl/server/app.py
Original file line number Diff line number Diff line change
@@ -370,17 +370,17 @@ async def batch_infer(
async def inner_infer(
batches: t.Sequence[t.Any], **kwargs: t.Any
) -> t.Sequence[t.Any]:
from bentoml._internal.context import component_context
from bentoml._internal.context import server_context
from bentoml._internal.runner.container import AutoContainer
from bentoml._internal.utils import is_async_callable

if self.enable_metrics:
self.adaptive_batch_size_hist.labels( # type: ignore
runner_name=self.service.name,
worker_index=component_context.component_index,
worker_index=server_context.worker_index,
method_name=name,
service_version=component_context.bento_version,
service_name=component_context.bento_name,
service_version=server_context.bento_version,
service_name=server_context.bento_name,
).observe(len(batches))

if len(batches) == 0:
12 changes: 6 additions & 6 deletions src/_bentoml_impl/worker/service.py
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ def main(

from _bentoml_impl.loader import import_service
from bentoml._internal.container import BentoMLContainer
from bentoml._internal.context import component_context
from bentoml._internal.context import server_context
from bentoml._internal.log import configure_server_logging

from ..server.app import ServiceAppFactory
@@ -149,22 +149,22 @@ def main(

if service_name and service_name != service.name:
service = service.find_dependent(service_name)
component_context.component_type = "service"
server_context.service_type = "service"
else:
component_context.component_type = "entry_service"
server_context.service_type = "entry_service"

if worker_id is not None:
component_context.component_index = worker_id
server_context.worker_index = worker_id

configure_server_logging()
BentoMLContainer.development_mode.set(development_mode)

if prometheus_dir is not None:
BentoMLContainer.prometheus_multiproc_dir.set(prometheus_dir)
component_context.component_name = service.name
server_context.service_name = service.name

asgi_app = ServiceAppFactory(
service, is_main=component_context.component_type == "entry_service"
service, is_main=server_context.service_type == "entry_service"
)()

uvicorn_extra_options: dict[str, t.Any] = {}
4 changes: 2 additions & 2 deletions src/bentoml/__init__.py
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@
from ._internal.bento import Bento
from ._internal.cloud import YataiClient
from ._internal.context import ServiceContext as Context
from ._internal.context import component_context
from ._internal.context import server_context
from ._internal.models import Model
from ._internal.monitoring import monitor
from ._internal.resource import Resource
@@ -216,7 +216,7 @@ def __getattr__(name: str) -> Any:
"batch",
"metrics",
"container",
"component_context",
"server_context",
"client",
"server",
"io",
18 changes: 9 additions & 9 deletions src/bentoml/_internal/configuration/containers.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
from simple_di import providers

from ...exceptions import BentoMLConfigException
from ..context import component_context
from ..context import server_context
from ..context import trace_context
from ..resource import CpuResource
from ..utils import split_with_quotes
@@ -362,14 +362,14 @@ def tracer_provider(
system_otel_resources: Resource = OTELResourceDetector().detect()

_resource = {}
if component_context.component_name:
_resource[SERVICE_NAME] = component_context.component_name
if component_context.component_index:
_resource[SERVICE_INSTANCE_ID] = component_context.component_index
if component_context.bento_name:
_resource[SERVICE_NAMESPACE] = component_context.bento_name
if component_context.bento_version:
_resource[SERVICE_VERSION] = component_context.bento_version
if server_context.service_name:
_resource[SERVICE_NAME] = server_context.service_name
if server_context.worker_index:
_resource[SERVICE_INSTANCE_ID] = server_context.worker_index
if server_context.bento_name:
_resource[SERVICE_NAMESPACE] = server_context.bento_name
if server_context.bento_version:
_resource[SERVICE_VERSION] = server_context.bento_version

bentoml_resource = Resource.create(_resource)

21 changes: 17 additions & 4 deletions src/bentoml/_internal/context.py
Original file line number Diff line number Diff line change
@@ -213,12 +213,25 @@ def service_name(self, service_name: t.Optional[str]) -> None:
self._service_name_var.set(service_name)


@attr.define
class _ComponentContext:
bento_name: str = ""
bento_version: str = "not available"
component_type: str | None = None
component_name: str | None = None
component_index: int | None = None
service_type: str | None = None
service_name: str | None = None
worker_index: int | None = None

@property
def component_type(self) -> str | None:
return self.service_type

@property
def component_name(self) -> str | None:
return self.service_name

@property
def component_index(self) -> int | None:
return self.worker_index

@property
def yatai_bento_deployment_name(self) -> str:
@@ -230,4 +243,4 @@ def yatai_bento_deployment_namespace(self) -> str:


trace_context = _ServiceTraceContext()
component_context = _ComponentContext()
server_context = component_context = _ComponentContext()
14 changes: 7 additions & 7 deletions src/bentoml/_internal/log.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@

from .configuration import get_debug_mode
from .configuration import get_quiet_mode
from .context import component_context
from .context import server_context
from .context import trace_context


@@ -127,12 +127,12 @@ def configure_logging():
@lru_cache(maxsize=1)
def _component_name():
result = ""
if component_context.component_type:
result = component_context.component_type
if component_context.component_name:
result = f"{result}:{component_context.component_name}"
if component_context.component_index:
result = f"{result}:{component_context.component_index}"
if server_context.service_type:
result = server_context.service_type
if server_context.service_name:
result = f"{result}:{server_context.service_name}"
if server_context.worker_index:
result = f"{result}:{server_context.worker_index}"
return result


8 changes: 4 additions & 4 deletions src/bentoml/_internal/monitoring/default.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@

import yaml

from ..context import component_context
from ..context import server_context
from ..context import trace_context
from .base import MonitorBase

@@ -78,7 +78,7 @@ def _init_logger(self) -> None:
with open(self.log_config_file, "r", encoding="utf8") as f:
logging_config_yaml = f.read()

worker_id = component_context.component_index or 0
worker_id = server_context.worker_index or 0
schema_path = Path(self.log_path).joinpath(
self.name, "schema", f"schema.{worker_id}.log"
)
@@ -118,8 +118,8 @@ def export_schema(self, columns_schema: dict[str, dict[str, str]]) -> None:
self.schema_logger.info(
dict(
meta_data={
"bento_name": component_context.bento_name,
"bento_version": component_context.bento_version,
"bento_name": server_context.bento_name,
"bento_version": server_context.bento_version,
},
columns=list(columns_schema.values()),
)
14 changes: 7 additions & 7 deletions src/bentoml/_internal/monitoring/otlp.py
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
from opentelemetry.sdk.resources import Resource

from ...exceptions import MissingDependencyException
from ..context import component_context
from ..context import server_context
from ..context import trace_context
from .base import MonitorBase

@@ -143,10 +143,10 @@ def _init_logger(self) -> None:
# configure resource if user has not explicitly configured it.
system_otel_resources: Resource = OTELResourceDetector().detect()
_resource = {}
if component_context.bento_name:
_resource[SERVICE_NAME] = f"{component_context.bento_name}:{self.name}"
if component_context.bento_version:
_resource[SERVICE_INSTANCE_ID] = component_context.bento_version
if server_context.bento_name:
_resource[SERVICE_NAME] = f"{server_context.bento_name}:{self.name}"
if server_context.bento_version:
_resource[SERVICE_INSTANCE_ID] = server_context.bento_version

bentoml_resource = Resource.create(_resource)

@@ -216,8 +216,8 @@ def export_data(
self.COLUMN_TIME: datetime.datetime.now().timestamp(),
self.COLUMN_RID: str(trace_context.request_id),
self.COLUMN_META: {
"bento_name": component_context.bento_name,
"bento_version": component_context.bento_version,
"bento_name": server_context.bento_name,
"bento_version": server_context.bento_version,
"monitor_name": self.name,
"schema": self._schema,
},
26 changes: 13 additions & 13 deletions src/bentoml/_internal/runner/runner_handle/remote.py
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
from ....exceptions import RemoteException
from ....exceptions import ServiceUnavailable
from ...configuration.containers import BentoMLContainer
from ...context import component_context
from ...context import server_context
from ...utils import LazyLoader
from ...utils.uri import uri_to_path
from ..container import Payload
@@ -172,11 +172,11 @@ async def async_run_method(
inp_batch_dim = __bentoml_method.config.batch_dim[0]

headers = {
"Bento-Name": component_context.bento_name,
"Bento-Version": component_context.bento_version,
"Bento-Name": server_context.bento_name,
"Bento-Version": server_context.bento_version,
"Runner-Name": self._runner.name,
"Yatai-Bento-Deployment-Name": component_context.yatai_bento_deployment_name,
"Yatai-Bento-Deployment-Namespace": component_context.yatai_bento_deployment_namespace,
"Yatai-Bento-Deployment-Name": server_context.yatai_bento_deployment_name,
"Yatai-Bento-Deployment-Namespace": server_context.yatai_bento_deployment_namespace,
}
total_args_num = len(args) + len(kwargs)
headers["Args-Number"] = str(total_args_num)
@@ -287,11 +287,11 @@ async def async_stream_method(
inp_batch_dim = __bentoml_method.config.batch_dim[0]

headers = {
"Bento-Name": component_context.bento_name,
"Bento-Version": component_context.bento_version,
"Bento-Name": server_context.bento_name,
"Bento-Version": server_context.bento_version,
"Runner-Name": self._runner.name,
"Yatai-Bento-Deployment-Name": component_context.yatai_bento_deployment_name,
"Yatai-Bento-Deployment-Namespace": component_context.yatai_bento_deployment_namespace,
"Yatai-Bento-Deployment-Name": server_context.yatai_bento_deployment_name,
"Yatai-Bento-Deployment-Namespace": server_context.yatai_bento_deployment_namespace,
}
total_args_num = len(args) + len(kwargs)
headers["Args-Number"] = str(total_args_num)
@@ -360,11 +360,11 @@ async def is_ready(self, timeout: int) -> bool:
async with self._client.get(
f"{self._addr}/readyz",
headers={
"Bento-Name": component_context.bento_name,
"Bento-Version": component_context.bento_version,
"Bento-Name": server_context.bento_name,
"Bento-Version": server_context.bento_version,
"Runner-Name": self._runner.name,
"Yatai-Bento-Deployment-Name": component_context.yatai_bento_deployment_name,
"Yatai-Bento-Deployment-Namespace": component_context.yatai_bento_deployment_namespace,
"Yatai-Bento-Deployment-Name": server_context.yatai_bento_deployment_name,
"Yatai-Bento-Deployment-Namespace": server_context.yatai_bento_deployment_namespace,
},
timeout=aio_timeout,
) as resp:
32 changes: 16 additions & 16 deletions src/bentoml/_internal/server/http/instruments.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
from simple_di import inject

from ...configuration.containers import BentoMLContainer
from ...context import component_context
from ...context import server_context

if TYPE_CHECKING:
from ... import external_typing as ext
@@ -109,17 +109,17 @@ async def wrapped_send(message: "ext.ASGIMessage") -> None:
# instrument request total count
self.metrics_request_total.labels(
endpoint=endpoint,
service_name=component_context.bento_name,
service_version=component_context.bento_version,
service_name=server_context.bento_name,
service_version=server_context.bento_version,
http_response_code=STATUS_VAR.get(),
).inc()

# instrument request duration
total_time = max(default_timer() - START_TIME_VAR.get(), 0)
self.metrics_request_duration.labels( # type: ignore
endpoint=endpoint,
service_name=component_context.bento_name,
service_version=component_context.bento_version,
service_name=server_context.bento_name,
service_version=server_context.bento_version,
http_response_code=STATUS_VAR.get(),
).observe(total_time)

@@ -129,8 +129,8 @@ async def wrapped_send(message: "ext.ASGIMessage") -> None:

with self.metrics_request_in_progress.labels(
endpoint=endpoint,
service_name=component_context.bento_name,
service_version=component_context.bento_version,
service_name=server_context.bento_name,
service_version=server_context.bento_version,
).track_inprogress():
await self.app(scope, receive, wrapped_send)
return
@@ -223,20 +223,20 @@ async def wrapped_send(message: "ext.ASGIMessage") -> None:
# instrument request total count
self.metrics_request_total.labels(
endpoint=endpoint,
service_name=component_context.bento_name,
service_version=component_context.bento_version,
service_name=server_context.bento_name,
service_version=server_context.bento_version,
http_response_code=STATUS_VAR.get(),
runner_name=component_context.component_name,
runner_name=server_context.service_name,
).inc()

# instrument request duration
total_time = max(default_timer() - START_TIME_VAR.get(), 0)
self.metrics_request_duration.labels( # type: ignore
endpoint=endpoint,
service_name=component_context.bento_name,
service_version=component_context.bento_version,
service_name=server_context.bento_name,
service_version=server_context.bento_version,
http_response_code=STATUS_VAR.get(),
runner_name=component_context.component_name,
runner_name=server_context.service_name,
).observe(total_time)

START_TIME_VAR.set(0)
@@ -245,9 +245,9 @@ async def wrapped_send(message: "ext.ASGIMessage") -> None:

with self.metrics_request_in_progress.labels(
endpoint=endpoint,
service_name=component_context.bento_name,
service_version=component_context.bento_version,
runner_name=component_context.component_name,
service_name=server_context.bento_name,
service_version=server_context.bento_version,
runner_name=server_context.service_name,
).track_inprogress():
await self.app(scope, receive, wrapped_send)
return
Loading