diff --git a/src/controllers/api/streaming.ts b/src/controllers/api/streaming.ts index 552ea3bd..557ac111 100644 --- a/src/controllers/api/streaming.ts +++ b/src/controllers/api/streaming.ts @@ -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(); }; diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index f1bf897b..4e624e9b 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -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(); + 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(); } diff --git a/src/metrics.ts b/src/metrics.ts index ce3d6d9a..26dce124 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -1,4 +1,4 @@ -import { Counter } from 'prom-client'; +import { Counter, Gauge } from 'prom-client'; export const httpRequestCounter = new Counter({ name: 'http_requests_total', @@ -6,6 +6,11 @@ export const httpRequestCounter = new Counter({ labelNames: ['method'], }); +export const streamingConnectionsGauge = new Gauge({ + name: 'streaming_connections', + help: 'Number of active connections to the streaming API', +}); + export const fetchCounter = new Counter({ name: 'fetch_total', help: 'Total number of fetch requests', @@ -36,6 +41,11 @@ export const relayMessageCounter = new Counter({ labelNames: ['verb'], }); +export const relayConnectionsGauge = new Gauge({ + name: 'relay_connections', + help: 'Number of active connections to the relay', +}); + export const dbQueryCounter = new Counter({ name: 'db_query_total', help: 'Total number of database queries',