I am trying to setup dd-trace
to auto instrument next and graphql spans in my nextjs app.
This works correctly in a dev build (graphql spans are uploaded to datadog), however, in production build the graphql plug is not initialised.
instrumentation.ts
export async function register(){
if(process.env.NEXT_RUNTIME === 'nodejs') {
const tracerLib = await import('dd-trace')
tracerLib.default.init({ ... })
tracer.use('next', { enabled: true, measured: true })
tracer.use('graphql', { enabled: true, measured: true })
}
}
next.config.js
{
experimental: {
instrumentationHook: true
}
}
I then moved the instrumentation code to a server-preload.js
file and ran node -r ./server-preload.js node_modules/.bin/next start
and everything worked as expected in production build.
According to the docs, dd-trace
must be imported before anything else.
My guess is that in the production build dd-trace
is not being imported early enough.
Is there any way to guarantee that a package will be imported before initialising the nextjs server?
Note: I have tried inspecting the unminified bundle but that has not been too easy to figure out the order of imports.