-
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
Nextjs instrumentation + pino + dd-trace log injection not working #3524
Comments
TypeScript 5 now builds as ESM by default. I'd recommend checking how you have it configured and see if it's outputting ESM files. If so, you will need to use the loader hook. |
@zomgbre can you confirm if #3524 (comment) worked for you? |
I'm also having this issue @tlhunter. I changed my imports to CJS syntax instead of the loader-hook, which I think should work according to the docs: https://www.npmjs.com/package/dd-trace#experimental-esm-support |
@Qard I'm not sure that's trivial/possible with Next.js. In my case, using the app router, standalone output and the following instrumentation setup: export async function register(): {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const tracerLib = await import('dd-trace');
const tracer = tracerLib.default;
tracer.init({ logInjection: true });
}
} using
Presumably, this is due to Next.js's tree-shaking. There is a sizable difference in the contents of the |
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? |
To link/correlate your logs and traces in DD. Something like this:
DD documentation link: https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/nodejs/ |
I had the same problem express in this thread. The one thing that did occur to me was that even if we use pino to log to the console, we wrap Pino in our own internal lib. Not sure if that makes a difference but once I updated our internal lib and applied the mixin in pino as per @vipul0092 example all started to work nicely. The one thing that is still not working is error stacks in Trace results. For a trace in my DD dashboard I can see the error, the logs, everything but under the Errors tab it says missing error message and stack trace. I can see the logs associated under the Logs tab and opening that has the stack trace. |
Circling back to this issue I believe it mostly boils down to Next.js code loading before the tracer. Here's a document on complex framework usage which I believe will alleviate the core issues. @radum's workaround has a similar effect. https://docs.datadoghq.com/tracing/trace_collection/compatibility/nodejs/#complex-framework-usage @zomgbre can you let me know if this solves the issue for you? |
Hi,
I've been trying to get Nextjs instrumentation + pino + dd-trace log injection working to no avail. Looking for some help or maybe it's a bug?
Expected behaviour
I would expect to see logs and traces correlated in Datadog, but they are not. Datadog docs claim to support pino out of the box automatically on this page: https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/nodejs/
There is a note about it having to be in JSON format. From what I can tell, pino is by default JSON format. The logs in datadog appear as JSON in my account.
Actual behaviour
I am getting traces and logs, but they are not correlated. :(
Steps to reproduce
src/instrumentation.ts
src/logger.ts
Logging is invoked in middleware.ts, server actions, and routes:
src/middleware.ts
app/api/auth/signout.ts
We are also using some server actions, but I realize that is alpha :) but here is the gist of it:
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
Overall, seems to not be working with my implementation. Looking for guidance or bug fix.
Environment
The text was updated successfully, but these errors were encountered: