From 5c81e4a1d12190314ee1610c0bf1008820e88b94 Mon Sep 17 00:00:00 2001 From: Mads Bisgaard Date: Wed, 13 Nov 2024 11:51:52 +0100 Subject: [PATCH] instrument httx clients in director-v2 --- .../core/application.py | 13 ++++---- .../modules/catalog.py | 25 +++++++++----- .../modules/director_v0.py | 33 +++++++++++-------- .../modules/dynamic_services.py | 15 ++++++--- .../modules/resource_usage_tracker_client.py | 3 ++ .../modules/storage.py | 24 +++++++++----- 6 files changed, 73 insertions(+), 40 deletions(-) diff --git a/services/director-v2/src/simcore_service_director_v2/core/application.py b/services/director-v2/src/simcore_service_director_v2/core/application.py index f1c81f18f98..20261d88130 100644 --- a/services/director-v2/src/simcore_service_director_v2/core/application.py +++ b/services/director-v2/src/simcore_service_director_v2/core/application.py @@ -149,19 +149,22 @@ def init_app(settings: AppSettings | None = None) -> FastAPI: substitutions.setup(app) + if settings.DIRECTOR_V2_TRACING: + setup_tracing(app, app.state.settings.DIRECTOR_V2_TRACING, APP_NAME) + if settings.DIRECTOR_V0.DIRECTOR_V0_ENABLED: - director_v0.setup(app, settings.DIRECTOR_V0) + director_v0.setup(app, settings) if settings.DIRECTOR_V2_STORAGE: - storage.setup(app, settings.DIRECTOR_V2_STORAGE) + storage.setup(app, settings) if settings.DIRECTOR_V2_CATALOG: - catalog.setup(app, settings.DIRECTOR_V2_CATALOG) + catalog.setup(app, settings) db.setup(app, settings.POSTGRES) if settings.DYNAMIC_SERVICES.DIRECTOR_V2_DYNAMIC_SERVICES_ENABLED: - dynamic_services.setup(app) + dynamic_services.setup(app, settings) dynamic_scheduler_enabled = settings.DYNAMIC_SERVICES.DYNAMIC_SIDECAR and ( settings.DYNAMIC_SERVICES.DYNAMIC_SCHEDULER @@ -192,8 +195,6 @@ def init_app(settings: AppSettings | None = None) -> FastAPI: if settings.DIRECTOR_V2_PROMETHEUS_INSTRUMENTATION_ENABLED: instrumentation.setup(app) - if settings.DIRECTOR_V2_TRACING: - setup_tracing(app, app.state.settings.DIRECTOR_V2_TRACING, APP_NAME) if settings.DIRECTOR_V2_PROFILING: app.add_middleware(ProfilerMiddleware) diff --git a/services/director-v2/src/simcore_service_director_v2/modules/catalog.py b/services/director-v2/src/simcore_service_director_v2/modules/catalog.py index f5e378afa43..49ac5e54702 100644 --- a/services/director-v2/src/simcore_service_director_v2/modules/catalog.py +++ b/services/director-v2/src/simcore_service_director_v2/modules/catalog.py @@ -9,26 +9,35 @@ from models_library.services_resources import ServiceResourcesDict from models_library.users import UserID from pydantic import parse_obj_as +from servicelib.fastapi.tracing import setup_httpx_client_tracing from settings_library.catalog import CatalogSettings +from ..core.settings import AppSettings from ..utils.client_decorators import handle_errors, handle_retry logger = logging.getLogger(__name__) -def setup(app: FastAPI, settings: CatalogSettings) -> None: - if not settings: - settings = CatalogSettings() +def setup(app: FastAPI, settings: AppSettings) -> None: + catalog_settings = settings.DIRECTOR_V2_CATALOG + tracing_settings = settings.DIRECTOR_V2_TRACING + + if not catalog_settings: + catalog_settings = CatalogSettings() async def on_startup() -> None: + client = httpx.AsyncClient( + base_url=f"{catalog_settings.api_base_url}", + timeout=app.state.settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT, + ) + if tracing_settings: + setup_httpx_client_tracing(client=client) + CatalogClient.create( app, - client=httpx.AsyncClient( - base_url=f"{settings.api_base_url}", - timeout=app.state.settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT, - ), + client=client, ) - logger.debug("created client for catalog: %s", settings.api_base_url) + logger.debug("created client for catalog: %s", catalog_settings.api_base_url) # Here we currently do not ensure the catalog is up on start # This will need to be assessed. diff --git a/services/director-v2/src/simcore_service_director_v2/modules/director_v0.py b/services/director-v2/src/simcore_service_director_v2/modules/director_v0.py index 0bc8c799dcb..ec3407c7f28 100644 --- a/services/director-v2/src/simcore_service_director_v2/modules/director_v0.py +++ b/services/director-v2/src/simcore_service_director_v2/modules/director_v0.py @@ -1,7 +1,4 @@ -""" Module that takes care of communications with director v0 service - - -""" +"""Module that takes care of communications with director v0 service""" import logging import urllib.parse @@ -20,9 +17,10 @@ from models_library.service_settings_labels import SimcoreServiceLabels from models_library.services import ServiceKey, ServiceKeyVersion, ServiceVersion from models_library.users import UserID +from servicelib.fastapi.tracing import setup_httpx_client_tracing from servicelib.logging_utils import log_decorator -from ..core.settings import DirectorV0Settings +from ..core.settings import AppSettings, DirectorV0Settings from ..utils.client_decorators import handle_errors, handle_retry from ..utils.clients import unenvelope_or_raise_error @@ -31,25 +29,32 @@ # Module's setup logic --------------------------------------------- -def setup(app: FastAPI, settings: DirectorV0Settings | None): - if not settings: - settings = DirectorV0Settings() +def setup(app: FastAPI, settings: AppSettings): + director_v0_settings = settings.DIRECTOR_V0 + tracing_settings = settings.DIRECTOR_V2_TRACING + if not director_v0_settings: + director_v0_settings = DirectorV0Settings() def on_startup() -> None: + client = httpx.AsyncClient( + base_url=f"{director_v0_settings.endpoint}", + timeout=app.state.settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT, + ) + if tracing_settings: + setup_httpx_client_tracing(client=client) DirectorV0Client.create( app, - client=httpx.AsyncClient( - base_url=f"{settings.endpoint}", - timeout=app.state.settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT, - ), + client=client, + ) + logger.debug( + "created client for director-v0: %s", director_v0_settings.endpoint ) - logger.debug("created client for director-v0: %s", settings.endpoint) async def on_shutdown() -> None: client = DirectorV0Client.instance(app).client await client.aclose() del client - logger.debug("delete client for director-v0: %s", settings.endpoint) + logger.debug("delete client for director-v0: %s", director_v0_settings.endpoint) app.add_event_handler("startup", on_startup) app.add_event_handler("shutdown", on_shutdown) diff --git a/services/director-v2/src/simcore_service_director_v2/modules/dynamic_services.py b/services/director-v2/src/simcore_service_director_v2/modules/dynamic_services.py index d572a9f23fb..0597e0d3584 100644 --- a/services/director-v2/src/simcore_service_director_v2/modules/dynamic_services.py +++ b/services/director-v2/src/simcore_service_director_v2/modules/dynamic_services.py @@ -8,19 +8,26 @@ import httpx from fastapi import FastAPI +from servicelib.fastapi.tracing import setup_httpx_client_tracing +from ..core.settings import AppSettings from ..utils.client_decorators import handle_errors, handle_retry logger = logging.getLogger(__name__) -def setup(app: FastAPI) -> None: +def setup(app: FastAPI, settings: AppSettings) -> None: + tracing_settings = settings.DIRECTOR_V2_TRACING + def on_startup() -> None: + client = httpx.AsyncClient( + timeout=app.state.settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT + ) + if tracing_settings: + setup_httpx_client_tracing(client=client) ServicesClient.create( app, - client=httpx.AsyncClient( - timeout=app.state.settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT - ), + client=client, ) async def on_shutdown() -> None: diff --git a/services/director-v2/src/simcore_service_director_v2/modules/resource_usage_tracker_client.py b/services/director-v2/src/simcore_service_director_v2/modules/resource_usage_tracker_client.py index 2c546ea3d84..4eaf3ba2016 100644 --- a/services/director-v2/src/simcore_service_director_v2/modules/resource_usage_tracker_client.py +++ b/services/director-v2/src/simcore_service_director_v2/modules/resource_usage_tracker_client.py @@ -24,6 +24,7 @@ from models_library.services import ServiceKey, ServiceVersion from models_library.wallets import WalletID from pydantic import parse_obj_as +from servicelib.fastapi.tracing import setup_httpx_client_tracing from ..core.errors import PricingPlanUnitNotFoundError from ..core.settings import AppSettings @@ -41,6 +42,8 @@ def create(cls, settings: AppSettings) -> "ResourceUsageTrackerClient": client = httpx.AsyncClient( base_url=settings.DIRECTOR_V2_RESOURCE_USAGE_TRACKER.api_base_url, ) + if settings.DIRECTOR_V2_TRACING: + setup_httpx_client_tracing(client=client) exit_stack = contextlib.AsyncExitStack() return cls(client=client, exit_stack=exit_stack) diff --git a/services/director-v2/src/simcore_service_director_v2/modules/storage.py b/services/director-v2/src/simcore_service_director_v2/modules/storage.py index 98e18845333..f725c7fa00e 100644 --- a/services/director-v2/src/simcore_service_director_v2/modules/storage.py +++ b/services/director-v2/src/simcore_service_director_v2/modules/storage.py @@ -8,6 +8,7 @@ import httpx from fastapi import FastAPI, HTTPException from models_library.users import UserID +from servicelib.fastapi.tracing import setup_httpx_client_tracing from servicelib.logging_utils import log_decorator from settings_library.s3 import S3Settings from settings_library.storage import StorageSettings @@ -15,6 +16,7 @@ # Module's business logic --------------------------------------------- from starlette import status +from ..core.settings import AppSettings from ..utils.client_decorators import handle_errors, handle_retry from ..utils.clients import unenvelope_or_raise_error @@ -23,19 +25,25 @@ # Module's setup logic --------------------------------------------- -def setup(app: FastAPI, settings: StorageSettings): - if not settings: - settings = StorageSettings() +def setup(app: FastAPI, settings: AppSettings): + storage_settings = settings.DIRECTOR_V2_STORAGE + tracing_settings = settings.DIRECTOR_V2_TRACING + + if not storage_settings: + storage_settings = StorageSettings() def on_startup() -> None: + client = httpx.AsyncClient( + base_url=f"{storage_settings.api_base_url}", + timeout=app.state.settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT, + ) + if tracing_settings: + setup_httpx_client_tracing(client=client) StorageClient.create( app, - client=httpx.AsyncClient( - base_url=f"{settings.api_base_url}", - timeout=app.state.settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT, - ), + client=client, ) - logger.debug("created client for storage: %s", settings.api_base_url) + logger.debug("created client for storage: %s", storage_settings.api_base_url) async def on_shutdown() -> None: client = StorageClient.instance(app).client