Skip to content

Commit

Permalink
Merge pull request #1573 from DSD-DBS/session-annotations
Browse files Browse the repository at this point in the history
feat(monitoring): Filter active sessions by session variables
  • Loading branch information
MoritzWeber0 authored May 27, 2024
2 parents bda5a8d + da1c8c8 commit 52f059a
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 38 deletions.
3 changes: 3 additions & 0 deletions backend/capellacollab/core/database/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def get_eclipse_session_configuration() -> (
connection=tools_models.ToolSessionConnection(
methods=[
tools_models.GuacamoleConnectionMethod(
id="guacamole",
name="Classic (Guacamole)",
description=(
"Old connection method using Guacamole. "
Expand All @@ -143,6 +144,7 @@ def get_eclipse_session_configuration() -> (
environment={"CONNECTION_METHOD": "xrdp"},
),
tools_models.HTTPConnectionMethod(
id="xpra",
name="Experimental (Xpra)",
description=(
"Experimental connection method using Xpra. "
Expand Down Expand Up @@ -280,6 +282,7 @@ def create_jupyter_tool(db: orm.Session) -> tools_models.DatabaseTool:
connection=tools_models.ToolSessionConnection(
methods=[
tools_models.HTTPConnectionMethod(
id="jupyter-direct",
name="Direct Jupyter connection (Browser)",
description="The only available connection method for Jupyter.",
ports=tools_models.HTTPPorts(http=8888, metrics=9118),
Expand Down
11 changes: 9 additions & 2 deletions backend/capellacollab/sessions/operators/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def start_session(
ports: dict[str, int],
volumes: list[models.Volume],
init_volumes: list[models.Volume],
annotations: dict[str, str],
prometheus_path="/metrics",
prometheus_port=9118,
) -> Session:
Expand All @@ -123,6 +124,7 @@ def start_session(
volumes=volumes,
init_volumes=init_volumes,
tool_resources=tool.config.resources,
annotations=annotations,
)

self._create_disruption_budget(
Expand All @@ -136,6 +138,7 @@ def start_session(
ports=ports,
prometheus_path=prometheus_path,
prometheus_port=prometheus_port,
annotations=annotations,
)

log.info(
Expand Down Expand Up @@ -447,6 +450,7 @@ def _create_deployment(
volumes: list[models.Volume],
init_volumes: list[models.Volume],
tool_resources: tools_models.Resources,
annotations: dict[str, str],
) -> client.V1Deployment:
k8s_volumes, k8s_volume_mounts = self._map_volumes_to_k8s_volumes(
volumes
Expand Down Expand Up @@ -530,14 +534,15 @@ def _create_deployment(
deployment: client.V1Deployment = client.V1Deployment(
kind="Deployment",
api_version="apps/v1",
metadata=client.V1ObjectMeta(name=name),
metadata=client.V1ObjectMeta(name=name, annotations=annotations),
spec=client.V1DeploymentSpec(
replicas=1,
strategy=client.V1DeploymentStrategy(type="Recreate"),
selector=client.V1LabelSelector(match_labels={"app": name}),
template=client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(
labels={"app": name, "workload": "session"}
labels={"app": name, "workload": "session"},
annotations=annotations,
),
spec=client.V1PodSpec(
automount_service_account_token=False,
Expand Down Expand Up @@ -627,6 +632,7 @@ def _create_service(
ports: dict[str, int],
prometheus_path: str,
prometheus_port: int,
annotations: dict[str, str],
) -> client.V1Service:
service: client.V1Service = client.V1Service(
kind="Service",
Expand All @@ -638,6 +644,7 @@ def _create_service(
"prometheus.io/scrape": "true",
"prometheus.io/path": prometheus_path,
"prometheus.io/port": f"{prometheus_port}",
**annotations,
},
),
spec=client.V1ServiceSpec(
Expand Down
16 changes: 15 additions & 1 deletion backend/capellacollab/sessions/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,32 @@ def request_session(

docker_image = util.get_docker_image(version, body.session_type)

annotations: dict[str, str] = {
"capellacollab/owner-name": user.name,
"capellacollab/owner-id": str(user.id),
"capellacollab/tool-name": tool.name,
"capellacollab/tool-id": str(tool.id),
"capellacollab/tool-version-name": version.name,
"capellacollab/tool-version-id": str(version.id),
"capellacollab/session-type": body.session_type.value,
"capellacollab/session-id": session_id,
"capellacollab/connection-method-id": connection_method.id,
"capellacollab/connection-method-name": connection_method.name,
}

session = operator.start_session(
session_id=session_id,
image=docker_image,
username=user.name,
session_type=models.SessionType.PERSISTENT,
session_type=body.session_type,
tool=tool,
version=version,
environment=environment,
init_environment=init_environment,
ports=connection_method.ports.model_dump(),
volumes=volumes,
init_volumes=init_volumes,
annotations=annotations,
prometheus_path=tool.config.monitoring.prometheus.path,
prometheus_port=connection_method.ports.metrics,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def create_namespaced_pod_disruption_budget(namespace, budget):
ports={"rdp": 3389},
volumes=[],
init_volumes=[],
annotations={},
)

assert deployment_counter == 1
Expand Down
9 changes: 3 additions & 6 deletions backend/tests/sessions/test_session_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ def fixture_patch_irrelevant_request_session_calls(
monkeypatch: pytest.MonkeyPatch,
tool: tools_models.DatabaseTool,
):
monkeypatch.setattr(
tools_injectables,
"get_existing_tool_version",
lambda *args, **kwargs: None,
)
monkeypatch.setattr(
sessions_util,
"get_connection_method",
Expand Down Expand Up @@ -112,7 +107,9 @@ def fixture_patch_irrelevant_request_session_calls(
)


@pytest.mark.usefixtures("patch_irrelevant_request_session_calls")
@pytest.mark.usefixtures(
"patch_irrelevant_request_session_calls", "tool_version"
)
def test_environment_behaviour(
monkeypatch: pytest.MonkeyPatch,
operator: MockOperator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { filter, map, mergeMap, tap } from 'rxjs';
import { BreadcrumbsService } from 'src/app/general/breadcrumbs/breadcrumbs.service';
import { EditorComponent } from 'src/app/helpers/editor/editor.component';
import { ToastService } from 'src/app/helpers/toast/toast.service';
import { CreateToolInput, Tool, ToolsService } from 'src/app/openapi';
import { Tool, ToolsService } from 'src/app/openapi';
import { ApiDocumentationComponent } from '../../../../general/api-documentation/api-documentation.component';
import { EditorComponent as EditorComponent_1 } from '../../../../helpers/editor/editor.component';
import { ToolWrapperService } from '../tool.service';
Expand Down Expand Up @@ -66,9 +66,10 @@ export class ToolDetailsComponent {
});
}

submitValue(value: CreateToolInput): void {
submitValue(value: Tool): void {
const { id, ...valueWithoutID } = value; // eslint-disable-line @typescript-eslint/no-unused-vars
this.toolsService
.updateTool(this.selectedTool!.id, value)
.updateTool(this.selectedTool!.id, valueWithoutID)
.pipe(
tap((tool) => {
this.toastService.showSuccess(
Expand Down
150 changes: 139 additions & 11 deletions helm/config/grafana/active-sessions.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"editorMode": "builder",
"expr": "sum(count by(instance) (idletime_minutes))",
"editorMode": "code",
"expr": "sum(count(up{connection_method_name=~\"$connection_method\", tool_version_name=~\"$tool_version\", tool_name=~\"$tool\", session_type=~\"$session_type\", job=\"sessions\"})) OR on() vector(0)",
"legendFormat": "__auto",
"range": true,
"refId": "A"
Expand Down Expand Up @@ -147,7 +147,38 @@
]
}
},
"overrides": []
"overrides": [
{
"matcher": {
"id": "byName",
"options": "Sessions with scraping errors"
},
"properties": [
{
"id": "color",
"value": {
"fixedColor": "semi-dark-red",
"mode": "fixed"
}
}
]
},
{
"matcher": {
"id": "byName",
"options": "Number of sessions"
},
"properties": [
{
"id": "color",
"value": {
"fixedColor": "semi-dark-blue",
"mode": "fixed"
}
}
]
}
]
},
"gridPos": {
"h": 7,
Expand All @@ -161,7 +192,7 @@
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": false
"showLegend": true
},
"tooltip": {
"mode": "single",
Expand All @@ -175,11 +206,25 @@
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"editorMode": "builder",
"expr": "sum(count by(instance) (idletime_minutes))",
"legendFormat": "__auto",
"editorMode": "code",
"expr": "sum(count(up{connection_method_name=~\"$connection_method\", tool_version_name=~\"$tool_version\", session_type=~\"$session_type\", tool_name=~\"$tool\", job=\"sessions\"})) OR on() vector(0)",
"hide": false,
"legendFormat": "Number of sessions",
"range": true,
"refId": "A"
},
{
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"editorMode": "code",
"expr": "(count(up{connection_method_name=~\"$connection_method\", tool_version_name=~\"$tool_version\", tool_name=~\"$tool\", session_type=~\"$session_type\", job=\"sessions\"}) == 0) OR vector(0)",
"hide": false,
"instant": false,
"legendFormat": "Sessions with scraping errors",
"range": true,
"refId": "B"
}
],
"title": "Active Sessions (instances)",
Expand Down Expand Up @@ -272,8 +317,8 @@
"uid": "PBFA97CFB590B2093"
},
"editorMode": "builder",
"expr": "idletime_minutes",
"legendFormat": "{{app}}",
"expr": "idletime_minutes{tool_name=~\"$tool\", tool_version_name=~\"$tool_version\", connection_method_name=~\"$connection_method\", session_type=~\"$session_type\"}",
"legendFormat": "{{session_id}} ({{tool_name}} {{tool_version_name}} with {{connection_method_name}})",
"range": true,
"refId": "A"
}
Expand All @@ -287,7 +332,90 @@
"style": "dark",
"tags": [],
"templating": {
"list": []
"list": [
{
"current": {
"selected": false,
"text": "7",
"value": "7"
},
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"definition": "up{job=\"sessions\"}",
"hide": 0,
"includeAll": false,
"label": "Tool and Version",
"multi": true,
"name": "tool_version_id",
"options": [],
"query": {
"query": "up{job=\"sessions\"}",
"refId": "PrometheusVariableQueryEditor-VariableQuery"
},
"refresh": 2,
"regex": "/tool_version_name_full=\"(?<text>[^\"]+)|tool_version_id=\"(?<value>[^\"]+)/g",
"skipUrlSync": false,
"sort": 0,
"type": "query"
},
{
"current": {
"selected": false,
"text": [""],
"value": [""]
},
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"definition": "up{job=\"sessions\", tool_version_id~=\"$tool_version_id\"}",
"description": "",
"hide": 0,
"includeAll": false,
"label": "Connection Method",
"multi": true,
"name": "connection_method_id",
"options": [],
"query": {
"query": "up{job=\"sessions\", tool_version_id~=\"$tool_version_id\"}",
"refId": "PrometheusVariableQueryEditor-VariableQuery"
},
"refresh": 1,
"regex": "/connection_method_name=\"(?<text>[^\"]+)|connection_method_id=\"(?<value>[^\"]+)/g",
"skipUrlSync": false,
"sort": 0,
"type": "query"
},
{
"current": {
"selected": false,
"text": [""],
"value": [""]
},
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"definition": "label_values(up{job=\"sessions\", connection_method_id~=},session_type)",
"hide": 0,
"includeAll": false,
"label": "Session Type",
"multi": true,
"name": "session_type",
"options": [],
"query": {
"query": "label_values(up{job=\"sessions\", connection_method_id~=},session_type)",
"refId": "PrometheusVariableQueryEditor-VariableQuery"
},
"refresh": 1,
"regex": "",
"skipUrlSync": false,
"sort": 0,
"type": "query"
}
]
},
"time": {
"from": "now-6h",
Expand All @@ -297,6 +425,6 @@
"timezone": "",
"title": "Active sessions",
"uid": "0kK_I7T4k",
"version": 1,
"version": 4,
"weekStart": ""
}
Loading

0 comments on commit 52f059a

Please sign in to comment.