UK-1 Datacenter
|
Back to Knowledgebase
Node
Official NPM @trusportidentity/beacon-node v1.0.0

Node.js & NestJS SDK Integration Guide

The TrustPort Beacon Node.js SDK (@trusportidentity/beacon-node) provides fast performance monitoring for Express, Fastify, and NestJS applications. The middleware automatically captures every HTTP request and response; wrap database queries and outbound calls in beacon.startSpan() to trace them too.

1Install Package

Install via npm, yarn, or pnpm:

npm install @trusportidentity/beacon-node

2Express.js Middleware Blueprint

server.tsTypeScript
import express from 'express';
import { BeaconSDK } from '@trusportidentity/beacon-node';

const app = express();

// Initialize SDK singleton
const beacon = new BeaconSDK({
  ingestUrl: process.env.BEACON_INGEST_URL || 'https://beacon-api.trustportidentity.com',
  apiKey: process.env.BEACON_API_KEY!, // tb_live_...
  serviceName: 'billing-api',
  environment: 'production',
  sanitizePii: true, // Automatically masks credit cards and auth headers
});

// Register global telemetry middleware before any routes
app.use(beacon.expressMiddleware());

app.get('/api/v1/invoices', async (req, res) => {
  const span = beacon.startSpan('db.prisma.findMany_invoices');
  try {
    // Database query execution...
    span.setTag('tenant_id', req.headers['x-tenant-id']);
    res.json({ success: true, count: 12 });
  } finally {
    span.end();
  }
});

app.listen(3000, () => console.log('Server running with Beacon APM'));

3NestJS Global Interceptor Setup

app.module.tsNestJS v10+
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { BeaconInterceptor, BeaconModule } from '@trusportidentity/beacon-node/nestjs';

@Module({
  imports: [
    BeaconModule.forRoot({
      ingestUrl: process.env.BEACON_INGEST_URL,
      apiKey: process.env.BEACON_API_KEY,
      serviceName: 'user-service-nestjs',
    }),
  ],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: BeaconInterceptor,
    },
  ],
})
export class AppModule {}

4Control Ingest Volume with sampleRate

Every request is already batched (batchSize/flushIntervalMs) instead of one network call per request. On top of that, set sampleRate (0–1, default 1) to trace only a fraction of requests in high-traffic services β€” this is what actually keeps you inside your plan's monthly quota. Exceptions are always sent regardless of sampling.

new BeaconSDK({ ..., sampleRate: 0.2 }) // trace ~20% of requests
Need help configuring Prisma or TypeORM query timing?

Our engineering support team is ready to assist.

Contact Telemetry Team