From f335c98b275da791f94b418e3227e2564148b1dd Mon Sep 17 00:00:00 2001 From: wiaderwek Date: Tue, 9 Jan 2024 11:50:25 +0100 Subject: [PATCH] add tldraw application level metrics (#4663) * add tldraw application level metrics --------- Co-authored-by: Tomasz Wiaderek --- .../src/modules/tldraw/metrics/index.ts | 1 + .../modules/tldraw/metrics/metrics.service.ts | 40 +++++++++++++++++++ .../tldraw/service/tldraw.ws.service.spec.ts | 3 +- .../tldraw/service/tldraw.ws.service.ts | 8 +++- .../src/modules/tldraw/tldraw-test.module.ts | 3 +- .../modules/tldraw/tldraw-ws-test.module.ts | 3 +- .../src/modules/tldraw/tldraw-ws.module.ts | 3 +- 7 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 apps/server/src/modules/tldraw/metrics/index.ts create mode 100644 apps/server/src/modules/tldraw/metrics/metrics.service.ts diff --git a/apps/server/src/modules/tldraw/metrics/index.ts b/apps/server/src/modules/tldraw/metrics/index.ts new file mode 100644 index 00000000000..70337867b90 --- /dev/null +++ b/apps/server/src/modules/tldraw/metrics/index.ts @@ -0,0 +1 @@ +export * from './metrics.service'; diff --git a/apps/server/src/modules/tldraw/metrics/metrics.service.ts b/apps/server/src/modules/tldraw/metrics/metrics.service.ts new file mode 100644 index 00000000000..ace2899c36b --- /dev/null +++ b/apps/server/src/modules/tldraw/metrics/metrics.service.ts @@ -0,0 +1,40 @@ +import { Injectable } from '@nestjs/common'; +import { Gauge, register } from 'prom-client'; + +@Injectable() +export class MetricsService { + private numberOfUsersOnServerCounter: Gauge; + + private numberOfBoardsOnServerCounter: Gauge; + + constructor() { + this.numberOfUsersOnServerCounter = new Gauge({ + name: 'sc_tldraw_users', + help: 'Number of active users per pod', + }); + + this.numberOfBoardsOnServerCounter = new Gauge({ + name: 'sc_tldraw_boards', + help: 'Number of active boards per pod', + }); + + register.registerMetric(this.numberOfUsersOnServerCounter); + register.registerMetric(this.numberOfBoardsOnServerCounter); + } + + public incrementNumberOfUsersOnServerCounter(): void { + this.numberOfUsersOnServerCounter.inc(); + } + + public decrementNumberOfUsersOnServerCounter(): void { + this.numberOfUsersOnServerCounter.dec(); + } + + public incrementNumberOfBoardsOnServerCounter(): void { + this.numberOfBoardsOnServerCounter.inc(); + } + + public decrementNumberOfBoardsOnServerCounter(): void { + this.numberOfBoardsOnServerCounter.dec(); + } +} diff --git a/apps/server/src/modules/tldraw/service/tldraw.ws.service.spec.ts b/apps/server/src/modules/tldraw/service/tldraw.ws.service.spec.ts index ddd186fed0a..1199bf217cc 100644 --- a/apps/server/src/modules/tldraw/service/tldraw.ws.service.spec.ts +++ b/apps/server/src/modules/tldraw/service/tldraw.ws.service.spec.ts @@ -10,6 +10,7 @@ import * as SyncProtocols from 'y-protocols/sync'; import * as AwarenessProtocol from 'y-protocols/awareness'; import { encoding } from 'lib0'; import { TldrawWsFactory } from '@shared/testing/factory/tldraw.ws.factory'; +import { MetricsService } from '@modules/tldraw/metrics'; import { WsSharedDocDo } from '../domain/ws-shared-doc.do'; import { config } from '../config'; import { TldrawBoardRepo } from '../repo'; @@ -51,7 +52,7 @@ describe('TldrawWSService', () => { const imports = [CoreModule, ConfigModule.forRoot(createConfigModuleOptions(config))]; const testingModule = await Test.createTestingModule({ imports, - providers: [TldrawWs, TldrawBoardRepo, TldrawWsService], + providers: [TldrawWs, TldrawBoardRepo, TldrawWsService, MetricsService], }).compile(); service = testingModule.get(TldrawWsService); diff --git a/apps/server/src/modules/tldraw/service/tldraw.ws.service.ts b/apps/server/src/modules/tldraw/service/tldraw.ws.service.ts index 660f5258fa8..ff455454d8b 100644 --- a/apps/server/src/modules/tldraw/service/tldraw.ws.service.ts +++ b/apps/server/src/modules/tldraw/service/tldraw.ws.service.ts @@ -8,6 +8,7 @@ import { Persitence, WSConnectionState, WSMessageType } from '../types'; import { TldrawConfig } from '../config'; import { WsSharedDocDo } from '../domain/ws-shared-doc.do'; import { TldrawBoardRepo } from '../repo'; +import { MetricsService } from '../metrics'; @Injectable() export class TldrawWsService { @@ -19,7 +20,8 @@ export class TldrawWsService { constructor( private readonly configService: ConfigService, - private readonly tldrawBoardRepo: TldrawBoardRepo + private readonly tldrawBoardRepo: TldrawBoardRepo, + private readonly metricsService: MetricsService ) { this.pingTimeout = this.configService.get('TLDRAW_PING_TIMEOUT'); } @@ -47,7 +49,9 @@ export class TldrawWsService { }) .catch(() => {}); this.docs.delete(doc.name); + this.metricsService.decrementNumberOfBoardsOnServerCounter(); } + this.metricsService.decrementNumberOfUsersOnServerCounter(); } try { @@ -106,6 +110,7 @@ export class TldrawWsService { this.persistence.bindState(docName, doc).catch(() => {}); } this.docs.set(docName, doc); + this.metricsService.incrementNumberOfBoardsOnServerCounter(); return doc; }); } @@ -197,6 +202,7 @@ export class TldrawWsService { this.send(doc, ws, encoding.toUint8Array(encoder)); } } + this.metricsService.incrementNumberOfUsersOnServerCounter(); } public async updateDocument(docName: string, ydoc: WsSharedDocDo): Promise { diff --git a/apps/server/src/modules/tldraw/tldraw-test.module.ts b/apps/server/src/modules/tldraw/tldraw-test.module.ts index 49f8bd5c820..3e3cd60396e 100644 --- a/apps/server/src/modules/tldraw/tldraw-test.module.ts +++ b/apps/server/src/modules/tldraw/tldraw-test.module.ts @@ -5,6 +5,7 @@ import { LoggerModule } from '@src/core/logger'; import { AuthenticationModule } from '@modules/authentication/authentication.module'; import { AuthorizationModule } from '@modules/authorization'; import { Course, User } from '@shared/domain/entity'; +import { MetricsService } from '@modules/tldraw/metrics'; import { AuthenticationApiModule } from '../authentication/authentication-api.module'; import { TldrawWsModule } from './tldraw-ws.module'; import { TldrawWs } from './controller'; @@ -20,7 +21,7 @@ const imports = [ CoreModule, LoggerModule, ]; -const providers = [TldrawWs, TldrawBoardRepo, TldrawWsService]; +const providers = [TldrawWs, TldrawBoardRepo, TldrawWsService, MetricsService]; @Module({ imports, providers, diff --git a/apps/server/src/modules/tldraw/tldraw-ws-test.module.ts b/apps/server/src/modules/tldraw/tldraw-ws-test.module.ts index 6e3c5a58479..815f09cbccd 100644 --- a/apps/server/src/modules/tldraw/tldraw-ws-test.module.ts +++ b/apps/server/src/modules/tldraw/tldraw-ws-test.module.ts @@ -3,13 +3,14 @@ import { MongoMemoryDatabaseModule, MongoDatabaseModuleOptions } from '@infra/da import { CoreModule } from '@src/core'; import { ConfigModule } from '@nestjs/config'; import { createConfigModuleOptions } from '@src/config'; +import { MetricsService } from '@modules/tldraw/metrics'; import { TldrawBoardRepo } from './repo'; import { TldrawWsService } from './service'; import { config } from './config'; import { TldrawWs } from './controller'; const imports = [CoreModule, ConfigModule.forRoot(createConfigModuleOptions(config))]; -const providers = [TldrawWs, TldrawBoardRepo, TldrawWsService]; +const providers = [TldrawWs, TldrawBoardRepo, TldrawWsService, MetricsService]; @Module({ imports, providers, diff --git a/apps/server/src/modules/tldraw/tldraw-ws.module.ts b/apps/server/src/modules/tldraw/tldraw-ws.module.ts index 98e91b5b3e6..183c579296f 100644 --- a/apps/server/src/modules/tldraw/tldraw-ws.module.ts +++ b/apps/server/src/modules/tldraw/tldraw-ws.module.ts @@ -3,6 +3,7 @@ import { ConfigModule } from '@nestjs/config'; import { createConfigModuleOptions } from '@src/config'; import { CoreModule } from '@src/core'; import { Logger } from '@src/core/logger'; +import { MetricsService } from '@modules/tldraw/metrics'; import { TldrawBoardRepo } from './repo'; import { TldrawWsService } from './service'; import { TldrawWs } from './controller'; @@ -10,6 +11,6 @@ import { config } from './config'; @Module({ imports: [CoreModule, ConfigModule.forRoot(createConfigModuleOptions(config))], - providers: [Logger, TldrawWs, TldrawWsService, TldrawBoardRepo], + providers: [Logger, TldrawWs, TldrawWsService, TldrawBoardRepo, MetricsService], }) export class TldrawWsModule {}