prometheus: add gauges for websocket connections

This commit is contained in:
Alex Gleason
2024-06-24 09:23:26 -05:00
parent 4ed289e5c3
commit e6cd3d9e47
3 changed files with 22 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ import { z } from 'zod';
import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { streamingConnectionsGauge } from '@/metrics.ts';
import { MuteListPolicy } from '@/policies/MuteListPolicy.ts';
import { getFeedPubkeys } from '@/queries.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
@@ -97,6 +98,8 @@ const streamingController: AppController = async (c) => {
}
socket.onopen = async () => {
streamingConnectionsGauge.inc();
if (!stream) return;
const topicFilter = await topicToFilter(stream, c.req.query(), pubkey);
@@ -120,6 +123,7 @@ const streamingController: AppController = async (c) => {
};
socket.onclose = () => {
streamingConnectionsGauge.dec();
controller.abort();
};

View File

@@ -10,7 +10,7 @@ import {
import { AppController } from '@/app.ts';
import { relayInfoController } from '@/controllers/nostr/relay-info.ts';
import { relayEventCounter, relayMessageCounter } from '@/metrics.ts';
import { relayConnectionsGauge, relayEventCounter, relayMessageCounter } from '@/metrics.ts';
import * as pipeline from '@/pipeline.ts';
import { RelayError } from '@/RelayError.ts';
import { Storages } from '@/storages.ts';
@@ -22,6 +22,10 @@ const FILTER_LIMIT = 100;
function connectStream(socket: WebSocket) {
const controllers = new Map<string, AbortController>();
socket.onopen = () => {
relayConnectionsGauge.inc();
};
socket.onmessage = (e) => {
const result = n.json().pipe(n.clientMsg()).safeParse(e.data);
if (result.success) {
@@ -34,6 +38,8 @@ function connectStream(socket: WebSocket) {
};
socket.onclose = () => {
relayConnectionsGauge.dec();
for (const controller of controllers.values()) {
controller.abort();
}