Skip to content

Commit 2ee5c00

Browse files
committed
Added monitoring
1 parent 7eab897 commit 2ee5c00

File tree

10 files changed

+79
-7
lines changed

10 files changed

+79
-7
lines changed

mobile-magic/apps/k8s-orchestrator/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import cors from 'cors';
66
import { Writable } from 'stream';
77
import { DOMAIN } from './config';
88
import { prismaClient } from "db/client";
9+
import promClient from 'prom-client';
10+
11+
const containerCreateBucket = new promClient.Histogram({
12+
name: 'container_create_bucket',
13+
help: 'Number of times a container was created',
14+
labelNames: ['type'],
15+
buckets: [50, 100, 250, 500, 1000, 2500, 5000, 10000, 20000],
16+
});
17+
918

1019
const kc = new KubeConfig();
1120
const app = express();
@@ -16,8 +25,6 @@ const PROJECT_TYPE_TO_BASE_FOLDER = {
1625
REACT_NATIVE: "/tmp/mobile-app"
1726
}
1827

19-
const POD_EXPIRY = 1000 * 60 * 5; // 5 minutes
20-
const EMPTY_POD_BUFFER_SIZE = 3;
2128
kc.loadFromDefault();
2229

2330
app.use(cors());
@@ -192,8 +199,10 @@ app.get("/worker/:projectId", async (req, res) => {
192199
}
193200

194201
console.log("Project found, assigning to pod");
195-
await assignPodToProject(projectId, "REACT"); // project.type);
202+
const startTime = Date.now();
203+
await assignPodToProject(projectId, "REACT");
196204
console.log("Pod assigned, sending response");
205+
containerCreateBucket.observe({ type: project.type }, Date.now() - startTime);
197206

198207
res.json({
199208
sessionUrl: `https://session-${projectId}.${DOMAIN}`,
@@ -202,6 +211,11 @@ app.get("/worker/:projectId", async (req, res) => {
202211
});
203212
});
204213

214+
app.get("/metrics", async (req, res) => {
215+
res.setHeader('Content-Type', promClient.register.contentType);
216+
res.end(await promClient.register.metrics());
217+
});
218+
205219
app.listen(7001, () => {
206220
console.log("Server is running on port 7001");
207221
});

mobile-magic/apps/k8s-orchestrator/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
"@types/cors": "^2.8.17",
1414
"@types/express": "^5.0.0",
1515
"cors": "^2.8.5",
16-
"express": "^4.21.2",
1716
"db": "*",
17+
"express": "^4.21.2",
18+
"prom-client": "^15.1.3",
1819
"redis": "^4.7.0"
1920
}
2021
}

mobile-magic/apps/primary-backend/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { prismaClient } from "db/client";
22
import express from "express";
33
import cors from "cors";
44
import { authMiddleware } from "common/middleware";
5-
const promMid = require('express-prometheus-middleware')
6-
5+
import promMid from 'express-prometheus-middleware'
76

87
const app = express();
98

mobile-magic/apps/primary-backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"@types/cors": "^2.8.17",
1414
"@types/express": "^5.0.0",
15+
"@types/express-prometheus-middleware": "^1.2.3",
1516
"@types/jsonwebtoken": "^9.0.9",
1617
"common": "*",
1718
"cors": "^2.8.5",

mobile-magic/bun.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"cors": "^2.8.5",
6060
"db": "*",
6161
"express": "^4.21.2",
62+
"prom-client": "^15.1.3",
6263
"redis": "^4.7.0",
6364
},
6465
"devDependencies": {
@@ -73,6 +74,7 @@
7374
"dependencies": {
7475
"@types/cors": "^2.8.17",
7576
"@types/express": "^5.0.0",
77+
"@types/express-prometheus-middleware": "^1.2.3",
7678
"@types/jsonwebtoken": "^9.0.9",
7779
"common": "*",
7880
"cors": "^2.8.5",
@@ -678,6 +680,8 @@
678680

679681
"@types/express": ["@types/[email protected]", "", { "dependencies": { "@types/body-parser": "*", "@types/express-serve-static-core": "^5.0.0", "@types/qs": "*", "@types/serve-static": "*" } }, "sha512-DvZriSMehGHL1ZNLzi6MidnsDhUZM/x2pRdDIKdwbUNqqwHxMlRdkxtn6/EPKyqKpHqTl/4nRZsRNLpZxZRpPQ=="],
680682

683+
"@types/express-prometheus-middleware": ["@types/[email protected]", "", { "dependencies": { "@types/express": "*" } }, "sha512-3B+/27E/pxkZOzTWHqSQOhi5IctzoEyX6F1izBxAjiC5oe6tHLDy0kNQAj31SGR3wjxfvH1dgZi9fzmKX90OBw=="],
684+
681685
"@types/express-serve-static-core": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*", "@types/qs": "*", "@types/range-parser": "*", "@types/send": "*" } }, "sha512-3xhRnjJPkULekpSzgtoNYYcTWgEZkp4myc+Saevii5JPnHNvHMRlBSHDbs7Bh1iPPoVTERHEZXyhyLbMEsExsA=="],
682686

683687
"@types/glob": ["@types/[email protected]", "", { "dependencies": { "@types/minimatch": "*", "@types/node": "*" } }, "sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA=="],

ops/app/primary-backend/deployment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ spec:
1515
spec:
1616
containers:
1717
- name: primary-backend
18-
image: 100xdevs/antidevs-primary-backend:latest
18+
image: 100xdevs/antidevs-primary-backend:7eab897aab7517fde2625afe89e265e175f9b678
1919
ports:
2020
- containerPort: 9090
2121
env:

ops/monitoring/grafana-values.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
datasources:
2+
datasources.yaml:
3+
apiVersion: 1
4+
datasources:
5+
- name: Prometheus
6+
type: prometheus
7+
url: http://prometheus-server
8+
access: proxy
9+
isDefault: true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: monitoring.coreos.com/v1
2+
kind: ServiceMonitor
3+
metadata:
4+
name: primary-backend-monitor
5+
spec:
6+
selector:
7+
matchLabels:
8+
app: primary-backend
9+
endpoints:
10+
- port: http
11+
path: /metrics
12+
interval: 15s
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
prometheus:
2+
prometheusSpec:
3+
additionalScrapeConfigs:
4+
- job_name: 'primary-backend'
5+
kubernetes_sd_configs:
6+
- role: pod
7+
namespaces:
8+
names:
9+
- app
10+
relabel_configs:
11+
- source_labels: [__meta_kubernetes_pod_label_app]
12+
regex: primary-backend
13+
action: keep
14+
- source_labels: [__meta_kubernetes_pod_ip]
15+
target_label: __address__
16+
replacement: $1:9090
17+
- source_labels: [__meta_kubernetes_pod_name]
18+
target_label: pod
19+
- source_labels: [__meta_kubernetes_namespace]
20+
target_label: namespace

ops/monitoring/script.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
kubectl create namespace monitoring
2+
helm install prometheus prometheus-community/kube-prometheus-stack -f prometheus-values.yaml -n monitoring
3+
4+
## Get grafana password
5+
kubectl --namespace monitoring get secrets prometheus-grafana -o jsonpath="{.data.admin-password}" | base64 -d ; echo
6+
7+
## Get grafana url
8+
export POD_NAME=$(kubectl --namespace monitoring get pod -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=prometheus" -oname)
9+
kubectl --namespace monitoring port-forward $POD_NAME 3000
10+
11+
## Get prometheus url
12+
kubectl port-forward svc/prometheus-kube-prometheus-prometheus 9090:9090

0 commit comments

Comments
 (0)