Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrumentation.js gets called 3 times #51450

Open
1 task done
john-james-gh opened this issue Jun 17, 2023 · 15 comments
Open
1 task done

Instrumentation.js gets called 3 times #51450

john-james-gh opened this issue Jun 17, 2023 · 15 comments
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. Instrumentation Related to Next.js Instrumentation. linear: next Confirmed issue that is tracked by the Next.js team.

Comments

@john-james-gh
Copy link

john-james-gh commented Jun 17, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Home
    Binaries:
      Node: 18.16.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.4.6
      eslint-config-next: 13.4.6
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3

btw Windows version is 11 Home. Not sure why next info isn't able to identify that.

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue or a replay of the bug

/

To Reproduce

There should be 2-3 console.logs, seemingly register function gets called multiple times.

Describe the Bug

register function of instrumentation.js gets called multiple times. 3 times in Azure Web Services. 2 times locally.

Azure App Service logs
Screenshot 2023-06-17 170431

Expected Behavior

register function should be called only once.

Which browser are you using? (if relevant)

Chrome latest

How are you deploying your application? (if relevant)

Azure App Services - Linux Single Container but the issue happens locally too.

NEXT-1600

@john-james-gh john-james-gh added the bug Issue was opened via the bug report template. label Jun 17, 2023
@github-actions github-actions bot added the area: app App directory (appDir: true) label Jun 17, 2023
@john-james-gh
Copy link
Author

john-james-gh commented Jun 18, 2023

Even when using global variables to store the invoke condition, doesn't seem to work. Global variables seem to get completely wiped.

export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {

    console.log(global.shouldInvoke)

    if (!global.shouldInvoke) {

      global.shouldInvoke = true

      console.log("`register` function was invoked.")

      console.log(global.shouldInvoke)
    }
  }
}

That printed this

Listening on port 3000 url: http://localhost:3000
undefined
`register` function was invoked.
true
undefined
`register` function was invoked.
true

@emondpph
Copy link

Having the same issue using the app router. It seems global variables are wiped, so I can't know if the instrumentation has been called yet

@neotrow
Copy link

neotrow commented Jul 21, 2023

+1, having the same issue.

For me it's without the app router but the traditional one.

@antonio-magicschool
Copy link

+1, having the same issue.

1 similar comment
@pawelkaliniakit
Copy link

+1, having the same issue.

@huozhi huozhi added the linear: next Confirmed issue that is tracked by the Next.js team. label Sep 6, 2023
@huozhi
Copy link
Member

huozhi commented Sep 6, 2023

Hey I couldn't repro this based on the latest canary. is there a reproduction based on the latest canary that you can provide? Thanks

@YazaHealth

This comment has been minimized.

@danieljee
Copy link

danieljee commented Sep 19, 2023

Having the same issue for nextjs: 13.4.13.
In my case, it's called twice in both dev and prod env

@ganicus
Copy link

ganicus commented Oct 4, 2023

Is there a way to add another hook that will be called AFTER bootstrapping/setting env and wiping out globals? Would love to see the ability to patch mods and set globals.

@matejfalat
Copy link

@huozhi It runs twice if middleware.ts is used. Repro here https://codesandbox.io/p/sandbox/cool-darkness-dh2wgt

@panda-geant
Copy link

@matejfalat did you find a way to bypass this behavior ? I face the exact same issue

@mdjfs
Copy link

mdjfs commented Dec 13, 2023

Having the same issue for next 14.0.3

@huozhi
Copy link
Member

huozhi commented Dec 13, 2023

@matejfalat

From the instrumentation docs:

We call register in all environments, so it's necessary to conditionally import any code that doesn't support both edge and nodejs. You can use the environment variable NEXT_RUNTIME to get the current environment. Importing an environment-specific code would look like this:

export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    await import('./instrumentation-node')
  }
 
  if (process.env.NEXT_RUNTIME === 'edge') {
    await import('./instrumentation-edge')
  }
}

Middleware is running based on edge runtime and page is on nodejs runtime, you could use above code to separate the instrumentation

@mdjfs
Copy link

mdjfs commented Dec 13, 2023

@huozhi Yes thats true but in the example

Even when using global variables to store the invoke condition, doesn't seem to work. Global variables seem to get completely wiped.

export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {

    console.log(global.shouldInvoke)

    if (!global.shouldInvoke) {

      global.shouldInvoke = true

      console.log("`register` function was invoked.")

      console.log(global.shouldInvoke)
    }
  }
}

That printed this

Listening on port 3000 url: http://localhost:3000
undefined
`register` function was invoked.
true
undefined
`register` function was invoked.
true
  1. Global vars are cleaned randomly
  2. Instrumentation in nodejs runtime is called more than once

@thomasmillercf
Copy link

@matejfalat you are right the middleware seems to cause the second register for some reason

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. Instrumentation Related to Next.js Instrumentation. linear: next Confirmed issue that is tracked by the Next.js team.
Projects
None yet
Development

No branches or pull requests