metrics: add messages sent and received by streaming API

This commit is contained in:
Alex Gleason 2024-09-21 21:44:24 -05:00
parent fc7228e183
commit 195cf9f44e
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 18 additions and 1 deletions

View file

@ -4,7 +4,11 @@ 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 { streamingConnectionsGauge } from '@/metrics.ts'; import {
streamingClientMessagesCounter,
streamingConnectionsGauge,
streamingServerMessagesCounter,
} 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';
@ -96,6 +100,7 @@ const streamingController: AppController = async (c) => {
function send(e: StreamingEvent) { function send(e: StreamingEvent) {
if (socket.readyState === WebSocket.OPEN) { if (socket.readyState === WebSocket.OPEN) {
debug('send', e.event, e.payload); debug('send', e.event, e.payload);
streamingServerMessagesCounter.inc();
socket.send(JSON.stringify(e)); socket.send(JSON.stringify(e));
} }
} }
@ -172,6 +177,8 @@ const streamingController: AppController = async (c) => {
}; };
socket.onmessage = (e) => { socket.onmessage = (e) => {
streamingClientMessagesCounter.inc();
if (ip) { if (ip) {
const count = limiter.get(ip) ?? 0; const count = limiter.get(ip) ?? 0;
limiter.set(ip, count + 1, { ttl: LIMITER_WINDOW }); limiter.set(ip, count + 1, { ttl: LIMITER_WINDOW });

View file

@ -17,6 +17,16 @@ export const streamingConnectionsGauge = new Gauge({
help: 'Number of active connections to the streaming API', help: 'Number of active connections to the streaming API',
}); });
export const streamingServerMessagesCounter = new Counter({
name: 'ditto_streaming_server_messages_total',
help: 'Total number of messages sent from the streaming API',
});
export const streamingClientMessagesCounter = new Counter({
name: 'ditto_streaming_client_messages_total',
help: 'Total number of messages received by the streaming API',
});
export const fetchCounter = new Counter({ export const fetchCounter = new Counter({
name: 'ditto_fetch_total', name: 'ditto_fetch_total',
help: 'Total number of fetch requests', help: 'Total number of fetch requests',