Here is how to integrate Opentelemetry with the Datadog agent in NestJS:
npm install @opentelemetry/api @opentelemetry/core @opentelemetry/node @opentelemetry/tracing @opentelemetry/exporter-datadog datadog-api-client dotenv
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();
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();
.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.
docker run -p 8126:8126 -p 8125:8125/udp -e DD_API_KEY=<Your Datadog API key> datadog/agent:latest
npm start
Now your NestJS app should be sending traces to the Datadog agent using OpenTelemetry!
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
Asked: 2022-07-28 11:00:00 +0000
Seen: 7 times
Last updated: Jul 03 '21
How do you log Python data into a database?
Is it possible to query a unique index directly instead of querying a collection in MongoDB?
What is the process of integrating API data into MongoDB using Spark/Python?
Please help me with connecting my MongoDB to my JS file as I am struggling to do so.
How can additional fields that have been transformed be queried in MongoDB?