Skip to content

Commit

Permalink
add tldraw application level metrics (#4663)
Browse files Browse the repository at this point in the history
* add tldraw application level metrics

---------

Co-authored-by: Tomasz Wiaderek <[email protected]>
  • Loading branch information
wiaderwek and Tomasz Wiaderek authored Jan 9, 2024
1 parent e609d31 commit f335c98
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/server/src/modules/tldraw/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './metrics.service';
40 changes: 40 additions & 0 deletions apps/server/src/modules/tldraw/metrics/metrics.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { Gauge, register } from 'prom-client';

@Injectable()
export class MetricsService {
private numberOfUsersOnServerCounter: Gauge<string>;

private numberOfBoardsOnServerCounter: Gauge<string>;

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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>(TldrawWsService);
Expand Down
8 changes: 7 additions & 1 deletion apps/server/src/modules/tldraw/service/tldraw.ws.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,7 +20,8 @@ export class TldrawWsService {

constructor(
private readonly configService: ConfigService<TldrawConfig, true>,
private readonly tldrawBoardRepo: TldrawBoardRepo
private readonly tldrawBoardRepo: TldrawBoardRepo,
private readonly metricsService: MetricsService
) {
this.pingTimeout = this.configService.get<number>('TLDRAW_PING_TIMEOUT');
}
Expand Down Expand Up @@ -47,7 +49,9 @@ export class TldrawWsService {
})
.catch(() => {});
this.docs.delete(doc.name);
this.metricsService.decrementNumberOfBoardsOnServerCounter();
}
this.metricsService.decrementNumberOfUsersOnServerCounter();
}

try {
Expand Down Expand Up @@ -106,6 +110,7 @@ export class TldrawWsService {
this.persistence.bindState(docName, doc).catch(() => {});
}
this.docs.set(docName, doc);
this.metricsService.incrementNumberOfBoardsOnServerCounter();
return doc;
});
}
Expand Down Expand Up @@ -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<void> {
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/modules/tldraw/tldraw-test.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,7 +21,7 @@ const imports = [
CoreModule,
LoggerModule,
];
const providers = [TldrawWs, TldrawBoardRepo, TldrawWsService];
const providers = [TldrawWs, TldrawBoardRepo, TldrawWsService, MetricsService];
@Module({
imports,
providers,
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/modules/tldraw/tldraw-ws-test.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/modules/tldraw/tldraw-ws.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ 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';
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 {}

0 comments on commit f335c98

Please sign in to comment.