-
Notifications
You must be signed in to change notification settings - Fork 291
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鈥檒l 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. |
Env :
Hey 馃憢馃徏 !
I鈥檓 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鈥檝e made multiple attempts to integrate dd-trace next鈥檚 plugin but doesn鈥檛 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: