Skip to content

Commit

Permalink
instrument httx clients in director-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
bisgaard-itis committed Nov 13, 2024
1 parent 7aba213 commit 5c81e4a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
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

# 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

Expand All @@ -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
Expand Down

0 comments on commit 5c81e4a

Please sign in to comment.