Skip to content

Commit e5d335a

Browse files
authored
πŸ› Adding metrics to dynamic-sidecar created services (ITISFoundation#3666)
1 parent e64d5bc commit e5d335a

File tree

8 files changed

+66
-13
lines changed

8 files changed

+66
-13
lines changed

β€Žpackages/service-library/src/servicelib/aiohttp/monitor_services.pyβ€Ž

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import Enum
2-
from typing import List, Union
2+
from typing import Union
33

44
from aiohttp import web
55
from prometheus_client import Counter
@@ -26,12 +26,12 @@
2626
kSERVICE_STARTED = f"{__name__}.services_started"
2727
kSERVICE_STOPPED = f"{__name__}.services_stopped"
2828

29-
SERVICE_STARTED_LABELS: List[str] = [
29+
SERVICE_STARTED_LABELS: list[str] = [
3030
"service_key",
3131
"service_tag",
3232
]
3333

34-
SERVICE_STOPPED_LABELS: List[str] = [
34+
SERVICE_STOPPED_LABELS: list[str] = [
3535
"service_key",
3636
"service_tag",
3737
"result",
@@ -66,11 +66,6 @@ class ServiceResult(Enum):
6666
FAILURE = "FAILURE"
6767

6868

69-
class ServiceType(Enum):
70-
COMPUTATIONAL = 0
71-
DYNAMIC = 1
72-
73-
7469
def service_started(
7570
# pylint: disable=too-many-arguments
7671
app: web.Application,

β€Žservices/director-v2/src/simcore_service_director_v2/core/application.pyβ€Ž

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,26 @@ def init_app(settings: Optional[AppSettings] = None) -> FastAPI:
134134
if settings.DYNAMIC_SERVICES.DIRECTOR_V2_DYNAMIC_SERVICES_ENABLED:
135135
dynamic_services.setup(app, settings.DYNAMIC_SERVICES)
136136

137-
if settings.DYNAMIC_SERVICES.DYNAMIC_SIDECAR and (
137+
dynamic_scheduler_enabled = settings.DYNAMIC_SERVICES.DYNAMIC_SIDECAR and (
138138
settings.DYNAMIC_SERVICES.DYNAMIC_SCHEDULER
139139
and settings.DYNAMIC_SERVICES.DYNAMIC_SCHEDULER.DIRECTOR_V2_DYNAMIC_SCHEDULER_ENABLED
140-
):
140+
)
141+
142+
computational_backend_enabled = (
143+
settings.DIRECTOR_V2_COMPUTATIONAL_BACKEND.COMPUTATIONAL_BACKEND_ENABLED
144+
)
145+
if dynamic_scheduler_enabled or computational_backend_enabled:
146+
rabbitmq.setup(app)
147+
148+
if dynamic_scheduler_enabled:
141149
dynamic_sidecar.setup(app)
142150

143151
if (
144152
settings.DIRECTOR_V2_COMPUTATIONAL_BACKEND.COMPUTATIONAL_BACKEND_DASK_CLIENT_ENABLED
145153
):
146154
dask_clients_pool.setup(app, settings.DIRECTOR_V2_COMPUTATIONAL_BACKEND)
147155

148-
if settings.DIRECTOR_V2_COMPUTATIONAL_BACKEND.COMPUTATIONAL_BACKEND_ENABLED:
149-
rabbitmq.setup(app)
156+
if computational_backend_enabled:
150157
comp_scheduler.setup(app)
151158

152159
node_rights.setup(app)

β€Žservices/director-v2/src/simcore_service_director_v2/modules/dynamic_sidecar/scheduler/_utils.pyβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
from typing import Any, Deque, Final, Optional
44

55
from fastapi import FastAPI
6+
from models_library.rabbitmq_messages import InstrumentationRabbitMessage
67
from pydantic import AnyHttpUrl
78
from servicelib.fastapi.long_running_tasks.client import (
89
ProgressCallback,
910
TaskClientResultError,
1011
)
1112
from servicelib.fastapi.long_running_tasks.server import TaskProgress
1213
from servicelib.utils import logged_gather
14+
from simcore_postgres_database.models.comp_tasks import NodeClass
1315

1416
from ....api.dependencies.database import get_base_repository
1517
from ....core.errors import NodeRightsAcquireError
@@ -19,6 +21,7 @@
1921
DockerStatus,
2022
SchedulerData,
2123
)
24+
from ....modules.rabbitmq import RabbitMQClient
2225
from ...db.repositories import BaseRepository
2326
from ...director_v0 import DirectorV0Client
2427
from ...node_rights import NodeRightsManager, ResourceName
@@ -275,3 +278,17 @@ async def _remove_containers_save_state_and_outputs() -> None:
275278
await service_remove_sidecar_proxy_docker_networks_and_volumes(
276279
TaskProgress.create(), app, scheduler_data, dynamic_sidecar_settings
277280
)
281+
282+
# instrumentation
283+
message = InstrumentationRabbitMessage(
284+
metrics="service_stopped",
285+
user_id=scheduler_data.user_id,
286+
project_id=scheduler_data.project_id,
287+
node_id=scheduler_data.node_uuid,
288+
service_uuid=scheduler_data.node_uuid,
289+
service_type=NodeClass.INTERACTIVE.value,
290+
service_key=scheduler_data.key,
291+
service_tag=scheduler_data.version,
292+
)
293+
rabbitmq_client: RabbitMQClient = app.state.rabbitmq_client
294+
await rabbitmq_client.publish(message.channel_name, message.json())

β€Žservices/director-v2/src/simcore_service_director_v2/modules/dynamic_sidecar/scheduler/events.pyβ€Ž

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from models_library.projects_networks import ProjectsNetworks
1111
from models_library.projects_nodes import Node
1212
from models_library.projects_nodes_io import NodeIDStr
13+
from models_library.rabbitmq_messages import InstrumentationRabbitMessage
1314
from models_library.service_settings_labels import (
1415
SimcoreServiceLabels,
1516
SimcoreServiceSettingsLabel,
@@ -19,6 +20,7 @@
1920
from servicelib.fastapi.long_running_tasks.client import TaskId
2021
from servicelib.json_serialization import json_dumps
2122
from servicelib.utils import logged_gather
23+
from simcore_postgres_database.models.comp_tasks import NodeClass
2224
from simcore_service_director_v2.utils.dict_utils import nested_update
2325
from tenacity import TryAgain
2426
from tenacity._asyncio import AsyncRetrying
@@ -34,6 +36,7 @@
3436
DockerStatus,
3537
)
3638
from ....modules.director_v0 import DirectorV0Client
39+
from ....modules.rabbitmq import RabbitMQClient
3740
from ...catalog import CatalogClient
3841
from ...db.repositories.projects import ProjectsRepository
3942
from ...db.repositories.projects_networks import ProjectsNetworksRepository
@@ -101,6 +104,21 @@ async def will_trigger(cls, app: FastAPI, scheduler_data: SchedulerData) -> bool
101104

102105
@classmethod
103106
async def action(cls, app: FastAPI, scheduler_data: SchedulerData) -> None:
107+
108+
# instrumentation
109+
message = InstrumentationRabbitMessage(
110+
metrics="service_started",
111+
user_id=scheduler_data.user_id,
112+
project_id=scheduler_data.project_id,
113+
node_id=scheduler_data.node_uuid,
114+
service_uuid=scheduler_data.node_uuid,
115+
service_type=NodeClass.INTERACTIVE.value,
116+
service_key=scheduler_data.key,
117+
service_tag=scheduler_data.version,
118+
)
119+
rabbitmq_client: RabbitMQClient = app.state.rabbitmq_client
120+
await rabbitmq_client.publish(message.channel_name, message.json())
121+
104122
dynamic_sidecar_settings: DynamicSidecarSettings = (
105123
app.state.settings.DYNAMIC_SERVICES.DYNAMIC_SIDECAR
106124
)

β€Žservices/director-v2/tests/conftest.pyβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from copy import deepcopy
1010
from pathlib import Path
1111
from typing import Any, AsyncIterable, Iterable
12+
from unittest.mock import AsyncMock
1213

1314
import httpx
1415
import pytest
@@ -238,3 +239,13 @@ def fake_workbench_complete_adjacency(
238239
fake_workbench_complete_adjacency_file: Path,
239240
) -> dict[str, Any]:
240241
return json.loads(fake_workbench_complete_adjacency_file.read_text())
242+
243+
244+
@pytest.fixture
245+
def disable_rabbitmq(mocker) -> None:
246+
def mock_setup(app: FastAPI) -> None:
247+
app.state.rabbitmq_client = AsyncMock()
248+
249+
mocker.patch(
250+
"simcore_service_director_v2.modules.rabbitmq.setup", side_effect=mock_setup
251+
)

β€Žservices/director-v2/tests/unit/test_api_route_dynamic_scheduler.pyβ€Ž

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
@pytest.fixture
2828
def mock_env(
29-
mock_env: EnvVarsDict, monkeypatch: MonkeyPatch, docker_swarm: None
29+
disable_rabbitmq: None,
30+
mock_env: EnvVarsDict,
31+
monkeypatch: MonkeyPatch,
32+
docker_swarm: None,
3033
) -> None:
3134
monkeypatch.setenv("SC_BOOT_MODE", "default")
3235
monkeypatch.setenv("DIRECTOR_ENABLED", "false")

β€Žservices/director-v2/tests/unit/test_modules_dynamic_sidecar_scheduler.pyβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ async def _assert_get_dynamic_services_mocked(
108108

109109
@pytest.fixture
110110
def mock_env(
111+
disable_rabbitmq: None,
111112
mock_env: EnvVarsDict,
112113
monkeypatch: MonkeyPatch,
113114
simcore_services_network_name: str,

β€Žservices/director-v2/tests/unit/test_modules_dynamic_sidecar_scheduler_task.pyβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
@pytest.fixture
3838
def mock_env(
39+
disable_rabbitmq: None,
3940
mock_env: EnvVarsDict,
4041
monkeypatch: MonkeyPatch,
4142
simcore_services_network_name: str,

0 commit comments

Comments
Β (0)