Skip to content
v1
Docs

Node.js SDK

WatchTower15 in Fastify

Terminal window
npm install @watchtower15/sdk @watchtower15/pino fastify

This recipe is the app in the SDK’s examples/fastify/, which the SDK’s test suite runs on Node 24 against the test fakes.

import Fastify from 'fastify';
import { Aggregator, WatchTower15 } from '@watchtower15/sdk';
const metrics = new Aggregator({ client: WatchTower15.fromEnvironment(), allowList: { 'orders.created': 'count' } });
const app = Fastify({
logger: {
transport: {
targets: [
{ target: 'pino/file', options: { destination: 1 } },
{ target: '@watchtower15/pino', options: { stream: 'orders-api' } },
],
},
},
});
app.addHook('onClose', async () => {
await metrics.close();
});
app.post('/orders', async (_request, reply) => {
metrics.increment('orders.created');
return reply.code(201).send({ created: true });
});
  • Logs. Fastify uses pino, so @watchtower15/pino is a second target beside the one that writes to standard output; it reads WT15_LOGS_TOKEN and runs in pino’s worker thread. See Logs.
  • Metrics. The onClose hook closes the aggregator, which sends what was counted.