Ask Your Question
1

How to configure Opentelemetry with the Datadog agent in NestJS?

asked 2022-07-28 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-07-03 11:00:00 +0000

david gravatar image

Here is how to integrate Opentelemetry with the Datadog agent in NestJS:

  1. Install the necessary dependencies:
npm install @opentelemetry/api @opentelemetry/core @opentelemetry/node @opentelemetry/tracing @opentelemetry/exporter-datadog datadog-api-client dotenv
  1. Create a new opentelemetry.js file in the root of your project with the following content:
const { NodeTracerProvider } = require('@opentelemetry/node');
const { SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { ConsoleSpanExporter, CollectorTraceExporter } = require('@opentelemetry/exporter-collector-proto');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');
const { MongoClientInstrumentation } = require('@opentelemetry/instrumentation-mongodb');

const { DatadogExporter } = require('@opentelemetry/exporter-datadog');

const provider = new NodeTracerProvider({
  plugins: {
    mongodb: {
      enabled: true,
      // Optional: Specify the service name you want to see in the Datadog UI
      // Default: 'mongodb'
      // serviceName: 'my-mongodb-service'
    },
  },
});

registerInstrumentations({
  instrumentations: [
    new ExpressInstrumentation(),
    new MongoClientInstrumentation(),
  ],
});

const exporter = new DatadogExporter({
  serviceName: process.env.DD_SERVICE_NAME,
  apiKey: process.env.DD_API_KEY,
});

provider.addSpanProcessor(
  new SimpleSpanProcessor(new CollectorTraceExporter({
    serviceName: process.env.DD_SERVICE_NAME,
    // Optional: Export traces through log messages (stdout)
    // exporter: new ConsoleSpanExporter(),
    // Optional: Specify sampler
    // sampler: new AlwaysOnSampler(),
  }))
);

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register();
  1. Modify your main.ts file to initialize the OpenTelemetry tracer:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

import { tracer } from './opentelemetry';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.use((req, res, next) => {
    tracer.startRootSpan({ name: 'HTTP Request', kind: 1 }, (span) => {
      span.setAttribute('component', 'http');
      span.setAttribute('http.method', req.method);
      span.setAttribute('http.url', req.url);
      next();
      span.end();
    });
  });

  await app.listen(3000);
}
bootstrap();

  1. Add the Datadog agent configuration to your .env file:
# Agent configuration
DD_API_KEY=<Your Datadog API key>
DD_SERVICE_NAME=<Your service name>
DD_ENV=<Your environment>
DD_TAGS=<Custom tags>

Make sure to replace <Your Datadog API key>, <Your service name>, <Your environment> and <Custom tags> with the appropriate values.

  1. Start the Datadog agent:
docker run -p 8126:8126 -p 8125:8125/udp -e DD_API_KEY=<Your Datadog API key> datadog/agent:latest
  1. Start your NestJS app:
npm start

Now your NestJS app should be sending traces to the Datadog agent using OpenTelemetry!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-07-28 11:00:00 +0000

Seen: 7 times

Last updated: Jul 03 '21