Skip to content

Commit cad49bc

Browse files
committed
add metrics on subscription
1 parent c5dc598 commit cad49bc

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/common/metrics/api.metrics.service.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class ApiMetricsService {
2222
private static transactionsCompletedCounter: Counter<string>;
2323
private static transactionsPendingResultsCounter: Counter<string>;
2424
private static batchUpdatesCounter: Counter<string>;
25+
private static subscriptionsConnectionsGauge: Gauge<string>;
2526

2627
constructor(
2728
private readonly apiConfigService: ApiConfigService,
@@ -32,6 +33,13 @@ export class ApiMetricsService {
3233
private readonly metricsService: MetricsService,
3334
) {
3435

36+
if (!ApiMetricsService.subscriptionsConnectionsGauge) {
37+
ApiMetricsService.subscriptionsConnectionsGauge = new Gauge({
38+
name: 'websocket_subscriptions_connections',
39+
help: 'Number of websocket connections for subscriptions',
40+
});
41+
}
42+
3543
if (!ApiMetricsService.vmQueriesHistogram) {
3644
ApiMetricsService.vmQueriesHistogram = new Histogram({
3745
name: 'vm_query',
@@ -182,6 +190,14 @@ export class ApiMetricsService {
182190
ApiMetricsService.lastProcessedTransactionCompletedProcessorNonce.set({ shardId }, nonce);
183191
}
184192

193+
@OnEvent(MetricsEvents.SetWebsocketMetrics)
194+
setWebsocketSubscriptionsMetrics(payload: { connectedClients: number }) {
195+
const { connectedClients } = payload;
196+
197+
ApiMetricsService.subscriptionsConnectionsGauge.set(connectedClients);
198+
}
199+
200+
185201
@OnEvent(MetricsEvents.SetTransactionsCompleted)
186202
recordTransactionsCompleted(payload: { transactions: any[] }) {
187203
ApiMetricsService.transactionsCompletedCounter.inc(payload.transactions.length);

src/crons/websocket/websocket.cron.service.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,40 @@ import { NetworkGateway } from 'src/crons/websocket/network.gateway';
66
import { Lock } from "@multiversx/sdk-nestjs-common";
77
import { PoolGateway } from 'src/crons/websocket/pool.gateway';
88
import { EventsGateway } from 'src/crons/websocket/events.gateway';
9+
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
10+
import { EventEmitter2 } from '@nestjs/event-emitter';
11+
import { MetricsEvents } from 'src/utils/metrics-events.constants';
12+
import { Server } from 'socket.io';
913
@Injectable()
14+
@WebSocketGateway({ cors: { origin: '*' }, path: '/ws/subscription' })
1015
export class WebsocketCronService {
16+
@WebSocketServer()
17+
server!: Server;
18+
1119
constructor(
1220
private readonly transactionsGateway: TransactionsGateway,
1321
private readonly blocksGateway: BlocksGateway,
1422
private readonly networkGateway: NetworkGateway,
1523
private readonly poolGateway: PoolGateway,
1624
private readonly eventsGateway: EventsGateway,
25+
private readonly eventEmitter: EventEmitter2,
1726
) { }
1827

28+
@Cron('*/6 * * * * *')
29+
async handleWebsocketMetrics() {
30+
const connectedClients = this.server.sockets.sockets.size ?? 0;
31+
// TODO: add more metrics in the future
32+
// const subscriptions: Record<string, number> = {};
33+
34+
// this.server.sockets.adapter.rooms.forEach((socketsSet, roomName) => {
35+
// subscriptions[roomName] = socketsSet.size;
36+
// });
37+
38+
this.eventEmitter.emit(MetricsEvents.SetWebsocketMetrics, {
39+
connectedClients,
40+
});
41+
}
42+
1943
@Cron('*/6 * * * * *')
2044
@Lock({ name: 'Push transactions to subscribers', verbose: true })
2145
async handleTransactionsUpdate() {

src/utils/metrics-events.constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export enum MetricsEvents {
1010
SetTransactionsCompleted = "setTransactionsCompleted",
1111
SetTransactionsPendingResults = "setTransactionsPendingResults",
1212
SetBatchUpdated = "setBatchUpdated",
13+
SetWebsocketMetrics = "setWebsocketMetrics",
1314
}

0 commit comments

Comments
 (0)