Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics #4

Merged
merged 9 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ansible/roles/tldraw-server/templates/configmap.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ data:
API_HOST: "http://api-svc:3030"
WS_PATH_PREFIX: "/tldraw-server"
LOG: "^(yjs|@y|server)"
FEATURE_PROMETHEUS_METRICS_ENABLED: true
REDIS_SENTINEL_SERVICE_NAME: valkey-headless.{{ NAMESPACE }}.svc.cluster.local
3 changes: 3 additions & 0 deletions ansible/roles/tldraw-server/templates/deployment.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ spec:
- containerPort: 3345
name: tldraw-ws
protocol: TCP
- containerPort: 9090
name: tldraw-server-metrics
protocol: TCP
envFrom:
- configMapRef:
name: tldraw-server-configmap
Expand Down
14 changes: 14 additions & 0 deletions ansible/roles/tldraw-server/templates/pod-monitor.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: tldraw-server-pod-monitor
namespace: {{ NAMESPACE }}
labels:
app: tldraw-server
spec:
selector:
matchLabels:
app: tldraw-server
endpoints:
- path: /metrics
port: tldraw-server-metrics
44 changes: 38 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@y/redis": "github:hpi-schul-cloud/y-redis#e7a2965d85a668f4f97040f18c129af6eb21287a",
"@y/redis": "github:hpi-schul-cloud/y-redis#ee6d920f77c3d30521f49506797e3d71dd026903",
"ioredis": "^5.4.1",
"lib0": "^0.2.93",
"prom-client": "^15.1.3",
"uws": "github:uNetworking/uWebSockets.js#v20.40.0",
"y-protocols": "^1.0.6"
},
Expand Down
33 changes: 33 additions & 0 deletions src/metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { number } from 'lib0';
import * as env from 'lib0/environment';
import { Gauge, register } from 'prom-client';
import * as uws from 'uws';

const openConnectionsGauge = new Gauge({
name: 'tldraw_open_connections',
help: 'Number of open WebSocket connections on tldraw-server.',
});

export const incOpenConnectionsGauge = () => {
openConnectionsGauge.inc();
};

export const decOpenConnectionsGauge = () => {
openConnectionsGauge.dec();
};

export const exposeMetricsToPrometheus = () => {
const route = env.getConf('prometheus-metrics-route') ?? '/metrics';
const port = number.parseInt(env.getConf('prometheus-metrics-port') ?? '9090');

const app = uws.App({});

app.get(route, async (res) => {
const metrics = await register.metrics();
res.end(metrics);
});

app.listen(port, () => {
console.log('Prometheus metrics exposed on port 9090');
});
};
26 changes: 25 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as logging from 'lib0/logging';
import * as number from 'lib0/number';
import * as promise from 'lib0/promise';
import * as uws from 'uws';
import { decOpenConnectionsGauge, exposeMetricsToPrometheus, incOpenConnectionsGauge } from './metrics.js';
import { redis } from './redis.js';
import { initStorage } from './storage.js';

Expand Down Expand Up @@ -34,7 +35,18 @@ export const createYWebsocketServer = async ({ redisPrefix = 'y', port, store })

const app = uws.App({});

await registerYWebsocketServer(app, `${wsPathPrefix}/:room`, store, checkAuthz, { redisPrefix }, redis);
await registerYWebsocketServer(
app,
`${wsPathPrefix}/:room`,
store,
checkAuthz,
{
redisPrefix,
openWsCallback,
closeWsCallback,
},
redis,
);

await promise.create((resolve, reject) => {
app.listen(port, (token) => {
Expand Down Expand Up @@ -94,8 +106,20 @@ const createAuthzRequestOptions = (room, token) => {
return requestOptions;
};

const openWsCallback = () => {
incOpenConnectionsGauge();
};

const closeWsCallback = () => {
decOpenConnectionsGauge();
};

const port = number.parseInt(env.getConf('port') || '3345');
const redisPrefix = env.getConf('redis-prefix') || 'y';
const store = await initStorage();

if (env.getConf('feature-prometheus-metrics-enabled') === 'true') {
exposeMetricsToPrometheus();
}

createYWebsocketServer({ port, store, redisPrefix });