From ff433070678d0dc82eed699d9b0f6bde0d238f30 Mon Sep 17 00:00:00 2001 From: Tomasz Wiaderek Date: Wed, 6 Dec 2023 20:25:10 +0100 Subject: [PATCH 1/3] add collecting Prometheus metrics fot the tldraw app --- .../schulcloud-server-core/tasks/main.yml | 7 +++++++ .../templates/tldraw-deployment.yml.j2 | 3 +++ .../templates/tldraw-server-svc.yml.j2 | 4 ++++ .../templates/tldraw-svc-monitor.yml.j2 | 17 ++++++++++++++++ apps/server/src/apps/tldraw.app.ts | 20 ++++++++++++++----- 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 ansible/roles/schulcloud-server-core/templates/tldraw-svc-monitor.yml.j2 diff --git a/ansible/roles/schulcloud-server-core/tasks/main.yml b/ansible/roles/schulcloud-server-core/tasks/main.yml index 048da2580c8..c26b65b0f77 100644 --- a/ansible/roles/schulcloud-server-core/tasks/main.yml +++ b/ansible/roles/schulcloud-server-core/tasks/main.yml @@ -193,3 +193,10 @@ template: tldraw-ingress.yml.j2 apply: yes when: WITH_TLDRAW is defined and WITH_TLDRAW|bool + + - name: TldrawServiceMonitor + kubernetes.core.k8s: + kubeconfig: ~/.kube/config + namespace: "{{ NAMESPACE }}" + template: tldraw-svc-monitor.yml.j2 + when: WITH_TLDRAW is defined and WITH_TLDRAW|bool diff --git a/ansible/roles/schulcloud-server-core/templates/tldraw-deployment.yml.j2 b/ansible/roles/schulcloud-server-core/templates/tldraw-deployment.yml.j2 index e6e45a1fd33..4530c223673 100644 --- a/ansible/roles/schulcloud-server-core/templates/tldraw-deployment.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/tldraw-deployment.yml.j2 @@ -52,6 +52,9 @@ spec: - containerPort: 3349 name: tldraw-http protocol: TCP + - containerPort: 9090 + name: api-metrics + protocol: TCP envFrom: - configMapRef: name: api-configmap diff --git a/ansible/roles/schulcloud-server-core/templates/tldraw-server-svc.yml.j2 b/ansible/roles/schulcloud-server-core/templates/tldraw-server-svc.yml.j2 index 8a1ded9a1d9..59074ea4f29 100644 --- a/ansible/roles/schulcloud-server-core/templates/tldraw-server-svc.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/tldraw-server-svc.yml.j2 @@ -18,5 +18,9 @@ spec: targetPort: 3349 protocol: TCP name: tldraw-http + - port: {{ PORT_METRICS_SERVER }} + targetPort: 9090 + protocol: TCP + name: api-metrics selector: app: tldraw-server diff --git a/ansible/roles/schulcloud-server-core/templates/tldraw-svc-monitor.yml.j2 b/ansible/roles/schulcloud-server-core/templates/tldraw-svc-monitor.yml.j2 new file mode 100644 index 00000000000..8fe4e4d6053 --- /dev/null +++ b/ansible/roles/schulcloud-server-core/templates/tldraw-svc-monitor.yml.j2 @@ -0,0 +1,17 @@ +apiVersion: monitoring.coreos.com/v1 +kind: ServiceMonitor +metadata: + name: api-svc-monitor + namespace: {{ NAMESPACE }} + labels: + app: api +spec: + selector: + matchExpressions: + - key: app.kubernetes.io/name + operator: in + values: + - api-svc + endpoints: + - path: /metrics + port: api-metrics diff --git a/apps/server/src/apps/tldraw.app.ts b/apps/server/src/apps/tldraw.app.ts index a394b1e8deb..bfecb63e6d9 100644 --- a/apps/server/src/apps/tldraw.app.ts +++ b/apps/server/src/apps/tldraw.app.ts @@ -10,6 +10,10 @@ import { enableOpenApiDocs } from '@shared/controller/swagger'; import { AppStartLoggable } from '@src/apps/helpers/app-start-loggable'; import { ExpressAdapter } from '@nestjs/platform-express'; import express from 'express'; +import { + addPrometheusMetricsMiddlewaresIfEnabled, + createAndStartPrometheusMetricsAppIfEnabled, +} from '@src/apps/helpers/prometheus-metrics'; async function bootstrap() { sourceMapInstall(); @@ -33,6 +37,7 @@ async function bootstrap() { // mount instances const rootExpress = express(); + addPrometheusMetricsMiddlewaresIfEnabled(logger, rootExpress); const port = 3349; const basePath = '/api/v3'; @@ -40,11 +45,16 @@ async function bootstrap() { rootExpress.use(basePath, nestExpress); rootExpress.listen(port); - logger.info( - new AppStartLoggable({ - appName: 'Tldraw server app', - }) - ); + rootExpress.listen(port, () => { + logger.info( + new AppStartLoggable({ + appName: 'Tldraw server app', + port, + }) + ); + + createAndStartPrometheusMetricsAppIfEnabled(logger); + }); } void bootstrap(); From a9e7301e8566f0a0de2029f29a67c9ef2e022a1e Mon Sep 17 00:00:00 2001 From: Tomasz Wiaderek Date: Thu, 7 Dec 2023 13:15:19 +0100 Subject: [PATCH 2/3] fix names --- .../templates/tldraw-svc-monitor.yml.j2 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ansible/roles/schulcloud-server-core/templates/tldraw-svc-monitor.yml.j2 b/ansible/roles/schulcloud-server-core/templates/tldraw-svc-monitor.yml.j2 index 8fe4e4d6053..6fcc75f0402 100644 --- a/ansible/roles/schulcloud-server-core/templates/tldraw-svc-monitor.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/tldraw-svc-monitor.yml.j2 @@ -1,17 +1,17 @@ apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: - name: api-svc-monitor + name: tldraw-svc-monitor namespace: {{ NAMESPACE }} labels: - app: api + app: tldraw-server spec: selector: matchExpressions: - key: app.kubernetes.io/name operator: in values: - - api-svc + - tldraw-server-svc endpoints: - path: /metrics port: api-metrics From 62b540ac0cdc01ee98247300b4bb5554b740ddc8 Mon Sep 17 00:00:00 2001 From: Tomasz Wiaderek Date: Mon, 11 Dec 2023 14:53:09 +0100 Subject: [PATCH 3/3] fix error --- apps/server/src/apps/tldraw.app.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/server/src/apps/tldraw.app.ts b/apps/server/src/apps/tldraw.app.ts index bfecb63e6d9..704ef3be4f1 100644 --- a/apps/server/src/apps/tldraw.app.ts +++ b/apps/server/src/apps/tldraw.app.ts @@ -43,7 +43,6 @@ async function bootstrap() { // exposed alias mounts rootExpress.use(basePath, nestExpress); - rootExpress.listen(port); rootExpress.listen(port, () => { logger.info(