Introduction
Welcome! If you want to improve your Next.js application’s monitoring and debugging capabilities, integrating Datadog tracing is the way to go. In this guide, I’ll show you how to trace your Next.js application using the dd-trace npm package. Let’s get started!
How to ?
-
Install dd-trace: Begin by installing the dd-trace package from npm:
npm install dd-trace
-
Create Datadog Configuration File: Create a datadog.js file in your src directory to load the Datadog tracing module:
// src/datadog.js module.exports = require('dd-trace').init(/* your dd-trace configuration here */);
-
Ensure Standalone Output Mode Compatibility: In projects with standalone output mode, neither the dd-trace package and the
src/datadog.js
file will be included in the final build.To achieve this, start by addding the following import statement to your _document.tsx file:
// src/pages/_document.tsx import 'dd-trace';
Then, to add the src/datadog.js file to the build, we will copy directly the file in our Docker image:
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + COPY ./src/datadog.js ./src/datadog.js
-
Integrate Datadog with Next.js Application: Start your Next.js application with Datadog tracing enabled by running the following command
node --require src/datadog.js server.js
Your Next.js application is now running with tracing enabled !
Credits
Banner: tech.gc.com