Export from instrumentation
Unanswered
Norwegian Forest Cat posted this in #help-forum
Norwegian Forest CatOP
Hello,
I'm trying to use instrumentation in my nextjs 13.5.6 app. Here's my very simple code of what I'm trying to achieve: (
Now, in my pages or API routes, I would like to
However, the instance of appInsights is undefined, so I get an exception.
Please help me understand how can I handle such scenario. Thanks
I'm trying to use instrumentation in my nextjs 13.5.6 app. Here's my very simple code of what I'm trying to achieve: (
instrumentation.ts)let appInsights: typeof import('applicationinsights')
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
appInsights = await import('applicationinsights');
appInsights.setup(process.env.APPLICATIONINSIGHTS_CONNECTION_STRING).start()
}
export { appInsights }Now, in my pages or API routes, I would like to
import { appInsights } from '@/instrumentation' and use it for tracking some custom exceptions, for example:appInsights.defaultClient.trackException({ exception: new Error('oh NOOOOOOOOOOOOOOOOOOOOOOOOO') })However, the instance of appInsights is undefined, so I get an exception.
Please help me understand how can I handle such scenario. Thanks
2 Replies
Cinnamon
I'm experiencing a similar issue.
I've been trying to gather system information from the server in a specific interval. Starting the task (that gathers the system information) within the instrumentation in the nodejs runtime works fine, but getting the data in any other file feels impossible.
I've tried exporting the collected data from instrumentation.ts directly and importing it in another file.
I've also tried creating a file that the instrumentation.ts passed the data to and reading from that file.
I've tried basically anything I could've imagined, but it just seems like every import that instrumentation.ts has, is creating some sort of a new instance, that none of the other files can access.
One thing that doesn't make it any easier, is that I'm trying to pass the data to server actions, but that doesn't seem to be the issue in the first place, since ive tried it without the server actions and it didnt work either.
Either I'm completely misunderstanding what the instrumentation.ts is for, or I'm just doing something wrong.
I've been trying to gather system information from the server in a specific interval. Starting the task (that gathers the system information) within the instrumentation in the nodejs runtime works fine, but getting the data in any other file feels impossible.
I've tried exporting the collected data from instrumentation.ts directly and importing it in another file.
I've also tried creating a file that the instrumentation.ts passed the data to and reading from that file.
I've tried basically anything I could've imagined, but it just seems like every import that instrumentation.ts has, is creating some sort of a new instance, that none of the other files can access.
One thing that doesn't make it any easier, is that I'm trying to pass the data to server actions, but that doesn't seem to be the issue in the first place, since ive tried it without the server actions and it didnt work either.
Either I'm completely misunderstanding what the instrumentation.ts is for, or I'm just doing something wrong.
Cinnamon