UK-1 Datacenter
|
Back to Knowledgebase
Node
Official NPM @trustport/beacon-node v2.1.0

Node.js & NestJS SDK Integration Guide

The TrustPort Beacon Node.js SDK offers zero-config auto-instrumentation for Express, Fastify, and NestJS applications, automatically timing database queries (Prisma, TypeORM, Mongoose) and HTTP fetch calls.

1Install Package

Install via npm, yarn, or pnpm:

npm install @trustport/beacon-node

2Express.js Middleware Blueprint

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

const app = express();

// Initialize SDK singleton
const beacon = new BeaconSDK({
  ingestUrl: process.env.BEACON_INGEST_URL || 'http://localhost:8443',
  apiKey: process.env.BEACON_API_KEY || 'tp_live_9f8a3c4e12',
  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 '@trustport/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 {}
Need help configuring Prisma or TypeORM query timing?

Our engineering support team is ready to assist.

Contact Telemetry Team