Skip to main content

OpenTelemetry Nestjs Instrumentation

Book meeting

Introduction to SigNoz for Nestjs

SigNoz can help you monitor your Nestjs applications for application related metrics like latency, request per second, error rates, etc. It can also monitor infrastructure metrics like CPU utilization and memory usage.

You can set alerts on metrics of your choice to stay on top of any issues arising in your deployed application.

Getting started for Nestjs with SigNoz

SigNoz uses OpenTelemetry for enabling your application code to generate telemetry data. OpenTelemetry provides a vendor-neutral specification  to instrument your application so that you can export data to any backend of your choice, such as SigNoz.

Let us see how to instrument your application with OpenTelemetry, so that you can visualize the data with SigNoz.

Instrumenting a sample Nestjs application with OpenTelemetry

  1. Install below dependencies

    npm install --save @opentelemetry/api
    npm install --save @opentelemetry/sdk-node
    npm install --save @opentelemetry/auto-instrumentations-node
    npm install --save @opentelemetry/exporter-trace-otlp-[email protected]0.27.0
  2. Create a tracer.ts file

    The IP of SIgNoz will be localhost if you are running SigNoz on local. If you are not running SigNoz on local machine, then please use the IP of the machine where SigNoz is installed.

    // tracing.ts

    'use strict'

    const opentelemetry = require('@opentelemetry/sdk-node');
    const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
    const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto');
    const { Resource } = require('@opentelemetry/resources');
    const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');

    // configure the SDK to export telemetry data to the console
    // enable all auto-instrumentations from the meta package
    const exporterOptions = {
    url: 'http://<IP of SigNoz>:55681/v1/trace',
    }
    const traceExporter = new OTLPTraceExporter(exporterOptions);
    const sdk = new opentelemetry.NodeSDK({
    resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: 'sampleNestJsApp'
    }),
    traceExporter,
    instrumentations: [getNodeAutoInstrumentations()]
    });

    // initialize the SDK and register with the OpenTelemetry API
    // this enables the API to record telemetry
    sdk.start()
    .then(() => console.log('Tracing initialized'))
    .catch((error) => console.log('Error initializing tracing', error));

    // gracefully shut down the SDK on process exit
    process.on('SIGTERM', () => {
    sdk.shutdown()
    .then(() => console.log('Tracing terminated'))
    .catch((error) => console.log('Error terminating tracing', error))
    .finally(() => process.exit(0));
    });

    module.exports = sdk
  1. Import the tracer module where your app starts

    On main.ts file or file where your app starts import tracer using below command:

    const tracer = require('./tracer')
  1. Start the tracer

    await tracer.start();

You can now run your Nestjs application. The data captured with OpenTelemetry from your application should start showing on the SigNoz dashboard.

Testing with sample Nestjs application

If you want to test out how SigNoz works with a sample Nestjs application, check out a sample Nestjs application at this GitHub repo.