Skip to content
v1
Docs

Node.js SDK

NestJS and the Node.js SDK

Terminal window
npm install @watchtower15/sdk @watchtower15/nestjs
import { Module } from '@nestjs/common';
import { WatchTower15Module } from '@watchtower15/nestjs';
@Module({
imports: [WatchTower15Module.forRoot({ metrics: { allowList: { 'orders.created': 'count' } } })],
})
export class AppModule {}

WatchTower15Module is global. forRoot(options) takes the client’s options plus metrics (the aggregator’s options) and logs; forRootAsync({ imports, inject, useFactory }) builds them from a ConfigService. With no options the client reads WT15_API_KEY and the other variables.

Provider What it is
WatchTower15 the client, from the options, else the environment; it names nestjs/<version> in its User-Agent
Aggregator the metrics aggregator, on its own client holding the metrics token alone, so a bad API key never stops counting
WatchTower15Logger a Nest LoggerService that forwards to an inner logger (ConsoleLogger by default) and ships each line to WatchTower15

Each is registered under its class and under an exported token, WATCHTOWER15 and WATCHTOWER15_AGGREGATOR (@Inject(WATCHTOWER15)), which serve an application that loads the SDK through two builds.

@Injectable()
export class OrdersService {
constructor(private readonly wt15: WatchTower15, private readonly metrics: Aggregator) {}
async create() {
this.metrics.increment('orders.created');
}
}

Declare the keys you count. The aggregator is always behind an allow-list. Without metrics.allowList (forRoot({}), or a forRootAsync factory returning no metrics) it is still provided and empty: every increment() and gauge() is counted not_allowed and nothing is sent.

const app = await NestFactory.create(AppModule, { bufferLogs: true });
app.useLogger(app.get(WatchTower15Logger));
app.enableShutdownHooks();

log ships as info, fatal as critical, verbose as debug. With app.enableShutdownHooks() the module closes the aggregator and the logger within 10 seconds when the application shuts down.

@watchtower15/nestjs/testing exports a WatchTower15Module with forTesting(), which binds a FakeClient and a FakeAggregator under the same classes and tokens. See Testing your integration.