Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tldraw application level metrics #4663

Merged
merged 10 commits into from
Jan 9, 2024
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 {}
Loading