
How to instrument a containerized Next.js application using dd-trace
/ 1 min read
Table of Contents
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:
Terminal window 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 nor the
src/datadog.jsfile will be included in the final build.To achieve this, start by adding 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:
Dockerfile COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/staticCOPY ./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
Terminal window node --require src/datadog.js server.js
Your Next.js application is now running with tracing enabled !
Credits
Banner: tech.gc.com