Skip to main content

Nest.js

Send distributed traces to Levitate from an Nest.js app using OpenTelemetry

Introduction

Nest.js is a progressive Node.js framework for building efficient, reliable, scalable server-side applications. This comprehensive guide will help you instrument your Nest.js application with OpenTelemetry, seamlessly sending the traces to Levitate.

You can also check out the example application on GitHub↗.

Pre-requisites

  1. You have a Nest.js application.
  2. You have signed up for Levitate, created a cluster, and obtained the following OTLP credentials from the Integrations page:
    • endpoint
    • auth_header

Install OpenTelemetry packages

To perform the instrumentation, install the following OpenTelemetry packages.

npm install --save @opentelemetry/api
@opentelemetry/instrumentation
@opentelemetry/tracing
@opentelemetry/exporter-trace-otlp-http
@opentelemetry/resources
@opentelemetry/semantic-conventions
@opentelemetry/auto-instrumentations-node
@opentelemetry/sdk-node

To know more about them, you can check: https://opentelemetry.io/docs/languages/js/libraries/

Setup auto-instrumentation using OpenTelemetry

Add the necessary environment variables that can be obtained from the Integrations page.

OTEL_EXPORTER_OTLP_ENDPOINT=<ENDPOINT>
OTEL_EXPORTER_OTLP_HEADERS="Authorization=<BASIC_AUTH_HEADER>"

Image

Next, create a file named instrumentation.ts and add the following code:

import {
NodeTracerProvider,
TracerConfig,
} from "@opentelemetry/sdk-trace-node";
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"; // Use BatchSpanProcessor for better performance
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import {
SEMRESATTRS_DEPLOYMENT_ENVIRONMENT,
SEMRESATTRS_SERVICE_NAME,
} from "@opentelemetry/semantic-conventions";
import { Resource } from "@opentelemetry/resources";

const providerConfig: TracerConfig = {
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: "<your-service-name>",
[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT]: process.env.NODE_ENV,
}),
};

// Initialize and register the tracer provider
const provider = new NodeTracerProvider(providerConfig);
const otlp = new OTLPTraceExporter();

provider.addSpanProcessor(new BatchSpanProcessor(otlp));
provider.register();

// Automatically instrument NestJS (additional instrumentations can be added similarly)
registerInstrumentations({
instrumentations: [getNodeAutoInstrumentations()],
});

This above code performs the following steps:

  1. Set up Trace Provider with the application's name as Service Name.
  2. Set up OTLP Exporter with Levitate OTLP endpoint.
  3. Set up auto instrumentation.

Next, the script should be "required" in the main.ts file to start the instrumentation.

import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";

async function bootstrap() {
require("./instrumentation"); // Add this line to start the instrumentation

const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
tip

You can also use instrumentation.js if you are not using Typescript.

Visualize the traces in Levitate

Once the application runs with the above code, it pushes traces to Levitate. You can see the result in action by looking at the APM dashboard in Levitate.

image

Troubleshooting

Please get in touch with us on Discord or Email if you have any questions.