Skip to content

Latest commit

 

History

History
90 lines (71 loc) · 2.37 KB

File metadata and controls

90 lines (71 loc) · 2.37 KB

DD Instrumentation for Node.js

Refer to part A of this guide for setting up DataKit locally to consume telemetry and forward it to TrueWatch.

Setting Up APM Traces And Log Injection

  1. Install dd trace package with a package manager of your choice.
bun add dd-trace
  1. Install a logging package that supports dd trace. We have chosen Winston for this guide.
bun add winston
  1. Add the following code to your project:
// logger.js

import ddTrace from 'dd-trace';
import { createLogger, format, transports } from 'winston';

ddTrace.init({
    logInjection: true // for this to work, take note that order matters - initialize ddTrace before calling createLogger
}) 

export const logger = createLogger({
  format: combine(timestamp(), json()),
  transports: [
    new transports.Console(),
    // new transports.File({ filename: '<your_log_file_name>.log' }) // uncomment this only if you require it
  ],
});
// server.js

import express from 'express';
import { logger } from './logger.js'; // from above

const app = express();

// request logging middleware example
app.use((req, res, next) => {
  const start = Date.now();
  const { method, originalUrl, statusCode } = req

  res.on('finish', () => {
    const duration = Date.now() - start;
    logger.info('Http request', {
      method,
      url: originalUrl,
      status: statusCode,
      duration,
    });
  });

  next();
});

// example route
app.get('/healthcheck', (req, res) => {
  logger.info('<your_app_name> is up and running!');
  res.send('<your_app_name> is up and running!');
});

app.listen(3000, () => {
  logger.info('Server started on port 3000');
});
  1. Remember to enable the ddtrace module in DataKit.

  2. Trigger some requests to your application and watch your logs and traces come in. The logs output will be of the following format:

{
  "level": "info",
  "message": "Server started on port 3000",
  "timestamp": "2025-11-06T03:25:00.123Z",
  "dd": {
    "trace_id": "123456789",
    "span_id": "987654321"
  }
}

Setting Up Profiling With Pyroscope

At present, DataKit supports one way to collect Node.js profiling data, namely Pyroscope.