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
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
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 { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts'; import { DittoDB } from '@/db/DittoDB.ts';
import { streamingConnectionsGauge } from '@/metrics.ts';
import { MuteListPolicy } from '@/policies/MuteListPolicy.ts'; import { MuteListPolicy } from '@/policies/MuteListPolicy.ts';
import { getFeedPubkeys } from '@/queries.ts'; import { getFeedPubkeys } from '@/queries.ts';
import { hydrateEvents } from '@/storages/hydrate.ts'; import { hydrateEvents } from '@/storages/hydrate.ts';
@ -97,6 +98,8 @@ const streamingController: AppController = async (c) => {
} }
socket.onopen = async () => { socket.onopen = async () => {
streamingConnectionsGauge.inc();
if (!stream) return; if (!stream) return;
const topicFilter = await topicToFilter(stream, c.req.query(), pubkey); const topicFilter = await topicToFilter(stream, c.req.query(), pubkey);
@ -120,6 +123,7 @@ const streamingController: AppController = async (c) => {
}; };
socket.onclose = () => { socket.onclose = () => {
streamingConnectionsGauge.dec();
controller.abort(); controller.abort();
}; };

View file

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

View file

@ -1,4 +1,4 @@
import { Counter } from 'prom-client'; import { Counter, Gauge } from 'prom-client';
export const httpRequestCounter = new Counter({ export const httpRequestCounter = new Counter({
name: 'http_requests_total', name: 'http_requests_total',
@ -6,6 +6,11 @@ export const httpRequestCounter = new Counter({
labelNames: ['method'], labelNames: ['method'],
}); });
export const streamingConnectionsGauge = new Gauge({
name: 'streaming_connections',
help: 'Number of active connections to the streaming API',
});
export const fetchCounter = new Counter({ export const fetchCounter = new Counter({
name: 'fetch_total', name: 'fetch_total',
help: 'Total number of fetch requests', help: 'Total number of fetch requests',
@ -36,6 +41,11 @@ export const relayMessageCounter = new Counter({
labelNames: ['verb'], labelNames: ['verb'],
}); });
export const relayConnectionsGauge = new Gauge({
name: 'relay_connections',
help: 'Number of active connections to the relay',
});
export const dbQueryCounter = new Counter({ export const dbQueryCounter = new Counter({
name: 'db_query_total', name: 'db_query_total',
help: 'Total number of database queries', help: 'Total number of database queries',