OpenTelemetry with NextJS

I’m trying to set up tracing for our Netlify site which is using NextJS. If I were deploying the site in a docker container I would just make sure that the tracing module was the first import in the application since it hooks into the node runtime to do auto instrumentation. It’s also possible to use the --require flag for node to require the tracing setup before the application is started.

An example of the setup can be seen here: https://docs.honeycomb.io/getting-data-in/opentelemetry-overview/

I’m having a hard time figuring out a way to make tracing work in Netlify.

I’ve tried various approaches but none of them work. E.g. i’ve tried requiring the tracer module at the top of the functions and I’ve also tried adding NODE_OPTIONS=--require tracer.js(which is supported in AWS Lambda) but the build fails because unfortunately all Netlify environment variables are also loaded during build instead of only at runtime and therefore the yarn install command fails because it is run with --require=tracer.js

I’m not sure if there are any other ways to get this working and was hoping that I could get some fresh ideas.

Hi @purkhusid,

I’m not familiar with what you’re trying to please take my advice with a pinch of salt.

I’m trying to understand what that example setup you’ve linked to is trying to do. To me, it looks like it’s a Node.js alternative for a Shell script as it’s returning something with process exit code. But, Netlify Functions are and can be used only for returning HTTP responses. Are you adapting your code to incorporate that change?

Netlify Environment Variables defined in the UI are available during runtime too. So, does that change something for you?

@hrishikesh OpenTelemetry is a open standard for tracing applications. It works by hooking into into the NodeJS runtime and adds auto instrumentation of your code. For the auto instrumentation to work the tracing setup has to be the first thing that is loaded by the Lambda and if I were using AWS Lambdas directly I would add the NODE_OPTIONS="-r tracing.js" to make sure that the tracing setup is required before everything else.

I did not manage to make this work on Netlify my question boils down to if I’m able to use the NODE_OPTIONS environment variable in the Netlify functions to add flags for the runtime?

I believe that’s not possible.

However, I think there’s still a way you could get this working. You could bundle your actual tracing.js file with a Netlify serverless function and inside the function, you could use Child Process which I believe can let you send arguments to execute the tracing.js file.

Could that work?