-
Notifications
You must be signed in to change notification settings - Fork 320
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
Next.JS plugin integration #4003
Comments
import { registerOTel } from '@vercel/otel';
export const register = async () => {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { TracerProvider } = (await import('dd-trace')).default.init({
logInjection: true,
startupLogs: true,
});
const provider = new TracerProvider();
registerOTel();
provider.register();
}
}; Would you like to try this? |
@Sh031224 Didn't work for me, I also tried to do |
@Lisenish If you only use datadog, it seems that you cannot fully use the spans provided by next.js. |
@Sh031224 Oh, sorry for the late reply 🙇 Actually I was able to see it after my message here, so yeah it seems this approach works. We still needed to group the tracer.use('http', {
hooks: {
request(span, req) {
if (span && req) {
const urlString = 'path' in req ? req.path : req.url;
if (urlString) {
const url = new URL(urlString, 'http://localhost');
const path = url.pathname + url.search;
const resourceGroup = getPathGroup(url.pathname); // our custom function to generilize the url
const method = req.method;
span.setTag('resource.name', method ? `${method} ${resourceGroup}` : resourceGroup);
span.setTag('http.route', method ? `${method} ${path}` : path);
} It also creates a lot of weird operations (in addition to |
This almost works - I get an exception on the datadog Tracer implementation
That I'm able to get around by monkey patching the provider import { registerOTel } from "@vercel/otel";
export async function register() {
try {
if (process.env.NEXT_RUNTIME === "nodejs") {
console.log("Registering tracing");
process.env.WEIGHTS_SERVICE = "weights-nextjs-serverless";
const tracer = await import("~/tracing");
const { PrismaInstrumentation } = await import("@prisma/instrumentation");
const provider = new tracer.TracerProvider();
const baseTracer = provider.getTracer.bind(provider);
provider.getTracer = (name: string, version?: string) => {
const newTracer = baseTracer(name, version);
// @ts-ignore
newTracer.getSpanLimits = () => ({});
return newTracer;
};
registerOTel({
serviceName: "weights-nextjs-serverless",
instrumentations: ["auto", new PrismaInstrumentation()],
});
// Register the provider globally
provider.register();
}
} catch (e) {
console.error(e);
}
} But then I get an exception with the startSpan method Registering tracing
TypeError: Cannot read properties of undefined (reading '_traceId')
at Tracer.startSpan (/var/task/node_modules/dd-trace/packages/dd-trace/src/opentelemetry/tracer.js:38:25)
at Tracer.startActiveSpan (/var/task/node_modules/dd-trace/packages/dd-trace/src/opentelemetry/tracer.js:112:23)
at /var/task/node_modules/next/dist/server/lib/trace/tracer.js:122:103
at AsyncLocalStorage.run (node:async_hooks:346:14)
at Za.with (file:///var/task/node_modules/@vercel/otel/dist/node/index.js:20:16621)
at ContextAPI.with (/var/task/node_modules/@opentelemetry/api/build/src/api/context.js:60:46)
at NextTracerImpl.trace (/var/task/node_modules/next/dist/server/lib/trace/tracer.js:122:28)
at /var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:3795
at AsyncLocalStorage.run (node:async_hooks:346:14)
at Za.with (file:///var/task/node_modules/@vercel/otel/dist/node/index.js:20:16621)
Error: Runtime exited without providing a reason
Runtime.ExitError |
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? |
@radum your solution seem to inspired by this blog post https://jake.tl/notes/2021-04-04-nextjs-preload-hack I've already tried this solution, and it works fine. Following the root issue, I want to enable Datadog log injection with next.js without preloading any script. I want to manage it directly from |
@Tarektouati I found that article while looking for log ingestion but yeah that one helped validate the fact that doing it via instrumentation is never going to work :) I would like to use the instrumentation hooks but DD is just not working with that or the fact that the hook is still experimental means it has all kinds of issues we don't see. |
This is a incredibly large issue considering Next.js is the largest web framework today. We are heavily relying on server components and no variations of the setups above works correctly. |
I'm in the same boat as everyone in this thread, Next.js 14 + App Router + RSC, set up dd-trace through instrumentation, enabled OTEL etc. only to get my app to die with HTTP 500 on incoming requests because of Preloading script and other hacks like monkey-patching console - no, thank you. If Sentry can add itself into instrumentation in an elegant manner, so should dd-trace. My solution for now is opting out of DataDog for Next.js 14+ apps until there is a sane way of doing this. |
This seems to work for me. However, the The development build works as expected, I'm not sure what I'm doing wrong:
export async function register() {
if (
process.env.NEXT_RUNTIME === 'nodejs' &&
process.env.ENV &&
process.env.SERVICE_NAME
) {
const ddTrace = await import('dd-trace')
const tracer = ddTrace.default.init({
env: process.env.ENV,
service: process.env.SERVICE_NAME,
version: process.env.SERVICE_VERSION,
sampleRate: 1,
profiling: true,
runtimeMetrics: true,
logInjection: true,
dogstatsd: {
hostname: 'localhost',
port: 8125,
},
})
// Monitor GraphQL
tracer.use('graphql', {
enabled: true,
measured: true,
})
// Monitor Next.js
tracer.use('next', {
enabled: true,
measured: true,
})
// Monitor Winston Logger
tracer.use('winston', {
enabled: true,
})
const provider = new tracer.TracerProvider()
provider.register()
}
}
|
In addition to the above, I have solved this with updating my start script: This is obviously a workaround, I'm not sure if it's a prod bundler issue which is leading to |
Hey everyone, here's our documentation on how to use Datadog with Next.js: The approach in the blog post by Jake appears to be the same as what we suggest. You'll need to use the I wouldn't necessarily refer to this as a "workaround" or a "hack". The That said, it sounds like a competing APM tool is able to work by using the instrumentation.js file, so there may be a way to support such an approach. Please create a helpdesk feature request (should be available from the GitHub new issue screen) as this will help prioritize such a feature. |
@tlhunter thanks for the reply, the way I have it set up is: running I think the docs refer to requiring |
@neilkumar-circle you should be fine using The |
@tlhunter I think the docs need to better highlight the steps one needs to take for frameworks. Digging that highlight is a huge pain. But thank you for sharing. So if I have in my package.json function setUpDatadogTracing() {
const tracer = require('dd-trace');
console.log('Setting up Datadog tracing');
tracer.init({
runtimeMetrics: true,
logInjection: true,
profiling: true,
});
tracer.use('next');
}
setUpDatadogTracing(); Is the same as using |
@radum the If your application does not already use If your application does already depend on using /cc @bengl who contributed the most to that part of the tracer. |
Thank you @tlhunter Can you explain what are those |
For those who host on Vercel: I was able to sort of workaround this by sending OpenTelemetry traces to a separate server running dd-agent w/ an API key protected endpoint for submitting traces. This allows collecting traces even on Vercel hosted apps. This can be done just by setting the env vars: |
@meyer9, could you please elaborate a bit on your setup? I've been trying to get our Vercel-hosted app to properly forward traces, and I haven't been having much luck. What does your Vercel configuration look like? Are you using the experimental telemetry hook? Are you importing and using the @vercel/otel package?
Do you mean a separate API key that is specifically used to auth to this server rather than the DD API key used to send traces? If so, what did you use to set that up?
I'm guessing this is where you're specifying that API key that you're sending along. Again, curious if it's separate from the DD API key. |
our architecture is frontend app using nextjs framework with some components in client side and some using server. this app is calling to an API. we want to trace calls done to that api, both calls done from either client components and server components. so far, we have managed to instrument datadog in client side calls by using datadog-rum script along with allowedTracingUrls flag set to the API we want to trace, following https://docs.datadoghq.com/real_user_monitoring/guide/monitor-your-nextjs-app-with-rum/?tab=cdnasync Problem we have: we are trying to instrument dd-trace with the different approaches posted above, but calls made from server side components are not working. for exact same API call, we see it traced correctly in DD APM Explorer if done from client components, but not if done from server side. Approaches we tried:
const ddTrace = await import('dd-trace')
env: 'develop',
any help? Should dd-trace be installed in API's server too? |
This is quite absurd for a company the size of DataDog to have such bare minimum examples of how to set up tracing compared to some of your competitors. This also requires having a additional step in CI to install dd-trace as it will not be copies over with the required compiled node_modules for a next.js project, as next will not detect the usage of dd-trace. |
@olafurns7 likely you'd need |
Sorry for the very late reply. I think I was using the otel package and passing in otel env vars to Vercel. The Otel env vars pointed to a separate server that I ran with a password protected ingestion endpoint for Otel traces. I setup a separate API key (not datadog API key) for this just to protect the endpoint. |
I'm having a similar issue. The plugins seem to sort of work when on version |
@AlexBurkey that's a compat issue which is fixed in #4916 |
I need to log my client and server logs from nextJS app to data-dog. I was using winston before and was getting
Can someone help how this can be integrated in my nextjs to avoid issues |
@gpremnat it seems like you're loading the tracer in browser code. Unfortunately Next.js can make it tricky to know when code loads in one place or another. |
@Tarektouati we've fixed a lot of Next.js bugs recently. Is this still an issue for you? |
Hi, I configured Pino logger to support both client and server on nextJS. While the application is running smoothly. I am trying to lo to Datadog from nextJS. But my logs are not flowing to DD . I cant see any logs received by my pod. I have logger.ts in utils and tracer.init({}) initialized in layout.tsx file. What different I should be doing to get it working? I am not using instrumentation.ts here. |
I'm seeing this issue with dd-trace 5.35.0 and Next 14.2.23 |
use NODE_OPTIONS="--require dd-trace/init" and in next.config.mjs const nextConfig = { this will allow dd-trace with automatic trace id injection into logs |
Env :
Hey 👋🏼 !
I’m working on a Next.JS app with app directory, built in standalone mode, and packaged in a docker image to be deployed on K8s cluster.
I’ve made multiple attempts to integrate dd-trace next’s plugin but doesn’t seem to be working :
I do see some traces popping on DD APM UI, but only see
methods
likeGET | POST
but no path or route information.Once I continued digging these traces, it seems that they were created from
http
plugin instead of next one.We ended up patching the dd-trace dependency (http plugin) to have something working :
Am I missing something in my configuration ?
The text was updated successfully, but these errors were encountered: