Skip to content

NextJS plugin request hook called twice for a single request #3272

Description

@tamarasaurus

With version 13.3.0 of NextJS + dd-trace 4.2.0 I have set up a starter NextJS project from scratch and enabled the NextJS plugin:

// tracer.js
const tracer = require('dd-trace').init();

tracer.use('next', {
  service: 'my-blog',
  hooks: {
    /* Executed when the request is finished but before API handlers are called */
    request: (
      span,
      request,
      response,
    ) => {
      console.log('request', span.context()._tags, request.url)
    },
  },
});

I require the tracer and start the app like this:

node --require ./tracer.js ./node_modules/.bin/next dev

When I make requests to the dev or production server I see multiple logs for a single requests.

Expected behaviour
I expect the request hook to be called once when I make a single request to the server.

Actual behaviour
The request hook that I've passed to the NextJS plugin is called twice.

Steps to reproduce
Clone this repository - https://github.com/tamarasaurus/dd-trace-duplicate

cd nextjs-blog
npm install
npm run dev

The server should start at localhost:3000, then make a request to the server using curl:

curl http://localhost:3000/test

The result is that the console log on this line https://github.com/tamarasaurus/dd-trace-duplicate/blob/main/nextjs-blog/tracer.js#L12 is called twice. In the first span, the next.page span tag is not included while it's there in the second call. E.g.

➜  nextjs-blog git:(main) ✗ npm run dev

> dev
> node --require ./tracer.js ./node_modules/.bin/next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
event - compiled client and server successfully in 243 ms (154 modules)
wait  - compiling /test (client and server)...
event - compiled client and server successfully in 47 ms (166 modules)
request {
  'service.name': 'my-blog',
  service: 'node',
  env: undefined,
  version: undefined,
  'runtime-id': 'c22c1d1b-21c4-4c21-8a17-4bfe943e0eea',
  component: 'next',
  'resource.name': 'GET',
  'span.type': 'web',
  'span.kind': 'server',
  'http.method': 'GET',
  '_dd.measured': true,
  'http.status_code': 200
} /test
request {
  'service.name': 'my-blog',
  service: 'node',
  env: undefined,
  version: undefined,
  'runtime-id': 'f8be125f-eb13-47ee-877f-3290050e39eb',
  component: 'next',
  'resource.name': 'GET /test',
  'span.type': 'web',
  'span.kind': 'server',
  'http.method': 'GET',
  '_dd.measured': true,
  'next.page': '/test',
  'http.status_code': 200
} /test

This issue exists on both the the dev and production servers.

To debug the issue I added some logs in the base server of NextJS and saw that handleRequest is indeed called twice - is this expected behaviour? Am I initialising the tracer incorrectly?

Environment

  • Operation system: MacOS Ventura 13.3.1
  • Node.js version: 18.16.0
  • Tracer version: 4.2.0
  • Agent version: N/A
  • Relevant library versions: NextJS 13.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingintegration-nextjsissues relating to the Next.js framework from Vercel

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions