-
Notifications
You must be signed in to change notification settings - Fork 309
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
Cannot plug dd-trace to NextJS instrumentation module #3457
Comments
You should init dd-trace conditionally like this: if (process.env.NEXT_RUNTIME === "nodejs") {
// init dd-trace
} That being said, I've noticed that when you instrument dd-trace like this, |
Thanks for advising, but sadly it still doesn't work (updated the repro repo). |
I was able to have success with this:
There are still weird things about it which I will open a separate ticket for. (Log injection for pino not quite working right. Not sure if I need the tracer.use piece. Also resource name in APM showing as GET/POST and not the path.) It is also important to have ENV variables set appropriately for your configuration. |
Thanks @zomgbre, it works indeed. 👍🏻 However, the spans traces are fine with correct URL. |
I have the same issue. Are there some workarounds for this? |
hey, we're also having the same issue after upgrading to Next 13.5, are there any workarounds for this? |
Any updates on this? |
As suggested by @Qard here, one possible solution might be using the loader hook provided by dd-trace to support ESM: I have, however, found that the loader-hook.mjs file is being tree-shaken during the standalone build (or something similar), and have yet to find a way to get it to stick around 😅 If anyone does get this working, please let us know! |
A workaround I've just found to have the resource names set correctly is to set it via the http plugin: export async function register(): Promise<void> {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const tracerLib = await import('dd-trace');
const tracer = tracerLib.default;
tracer.init({
appsec: true,
logInjection: true,
runtimeMetrics: true,
});
tracer.use('http', {
hooks: {
request(span, req) {
if (span && req) {
const url = 'path' in req ? req.path : req.url;
if (url) {
const method = req.method;
span.setTag('resource.name', method ? `${method} ${url}` : url);
}
}
},
},
});
}
} |
After MONTHS of not being able to get log injection with winston working, adding it to this next.config.js configuration worked! https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackages |
hey @jamesbrooks94 I saw yesterday you had a public repo with your next/datadog/winston implementation that is now private. Do you mind sharing how you got it working? I also am using a standalone build and saw that you were doing a couple things I think.
Was this right? |
|
Hello everyone, I managed to hit the same dead end like most of you here. I am running Next.js 14 with app router. The only way I managed to get it working (although not sure if it is fully working yet) is to create a JS file const packageJSON = require('../package.json');
function setUpDatadogTracing() {
const tracer = require('dd-trace');
tracer.init({
runtimeMetrics: true,
logInjection: true,
env: 'dev',
service: `myapp`,
version: packageJSON?.version ?? 'unknown'
});
}
setUpDatadogTracing(); And load it within package.json I am also getting the versioning coming through for each new release I make and also the dev envs are set properly. Logs are ingested also but only the ones that I am logging via an internal logger I made via Pino. The other ones are not coming in as they are not in JSON format. There is a way in the file above to patch the console log and make it spit out JSON but that is a can of worms because there is lots of cleaning up that needs to be done to make it work and also it could break at any Next update. Using the instrumentation hook I never managed to get it working, and using the telemetry from Vercel plus DD I always got undefined errors looking for the _traceID in an object. Even with this setup I am not sure if I can see any spans and I need to check more. For sourcemaps I am thinking to generate them and load them via the CI before I remove them from the deployed app. Has anyone found a better way that works with most DD features and can share their setup? |
Very curios that: if (process.env.NEXT_RUNTIME === 'nodejs') { /* WORKS */ } And: if (process.env.NEXT_RUNTIME !== 'nodejs') return
// NOT WORK This happens with Next.js 13.5.4. How is it possible that with early return it doesn't work? Are you parsing the AST instead of letting JS do its job? I don't understand the error, something is missing. |
/* next.config.js */
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
instrumentationHook: true,
serverComponentsExternalPackages: ['dd-trace'],
},
}
module.exports = nextConfig /* src/instrumentation.ts */
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
await import('./tracer')
}
} /* src/tracer.ts */
import ddtrace from 'dd-trace'
ddtrace
.init({
service: 'my-nextjs-app',
// other settings,,,
})
.use('next')
.use('fetch', {
hooks: {
// The following hook is for confirming `fetch` instruments
request: (span, req: any, res) => {
console.log(`[trace][fetch] ${req?.method} ${req?.url}`)
},
},
}) When I checked several months ago, there was a problem with missing spans for the |
@Quramy do you get correct resource names? When I try the same with NextJS 14.2.7 and dd-trace 5.12.0 I only get resources names |
I didn't check the resource names. To change |
Hey @Quramy, When you initialize the tacer with |
I tried Instead of insrumentation.ts, Using It may be still tough to use |
After hours of debugging our integration after we upgraded Next.js due to a vulnerability in Next... it turns out that in the latest version of It sounds like tests for the plugin were failing when Next 14.2.7 came out so they just disabled the plugin for that and all later Next versions and there have been no updates since. Here's the PR that introduced this: #4636 Here are the changes in the release for v5.22.0 and v4.46.0 that show that the PR above was included in the release. And finally, here's the open PR that reverts that change that was opened when the PR was first introduced - it appears the hasn't been any progress since then: #4637 |
Hey @tlhunter, or anyone else, do you know when the issue with the Next.js versions (>= 14.2.7) and the dd-trace-js plugin will be resolved? It's currently blocking our ability to update to the latest Next.js versions due to the vulnerability patches. Any updates would be greatly appreciated! |
We do see errors in tracing with a Next.js version > I've contacted DD support about this issue. In some previous threads, DD reps have advised to contact their support as they are much more active in that channel and might not always be able to track the discussion here. To get more attention for this issue, consider opening a support ticket. |
@scottbartell @EmilioAiolfi @Jokinen the compatibility with Next.js >= 14.2.7 - 14.x should be ready once this PR is merged and released: #4916 |
@tlhunter Thank you! I gave it a test. The plugin is now initialised as expected and traces looked to be in better order 👍 We've have to setup some customisations that didn't appear to be compatible with the new version so I can't yet confirm whether the fix works for us in full. To be clear, at this point I am not suspecting any issue in the library, but in our custom integration code instead. Thank you for the fix 👍 |
@tlhunter I can now confirm that the fix resolves the problem our team had. Thank you again 👍 |
@thollander are you still having these issues? Note that you can throw Also, my previous comment about internal node modules missing and the tracer accidentally being loaded in a browser context also applies to your original post. Finally, since this issue was opened we also created this document about complex framework usage and that applies here: Basically, you'll need the |
Hello,
I'd like to use the predefined configuration of NextJS instrumentation (experimental feature) to plug
dd-trace
.This way of working permits to have the dependency really used inside the application and to benefit from NextJS Standalone feature (because if we pass through the
NODE_OPTIONS="-r dd-trace/init"
,dd-trace
gets removed on production Docker build as NextJS considers it's not "unused").Not really sure on if this issue should be open on
dd-trace
side or NextJS, but as the error seems to be coming fromdd-trace
file, I begin to open it here.Expected behaviour
I would like to be able to plug instrumentation without error.
Actual behaviour
We currently have issues on requiring some "nodejs" module inside
dd-trace
.Steps to reproduce
Repro : https://github.com/thollander/repro-datadog-nextjs
Here is the stack trace :
Environment
The text was updated successfully, but these errors were encountered: