microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
321 stars 138 forks source link

Dependency requests not logged on node 18 with fetch API #1295

Closed arisewanggithub closed 3 months ago

arisewanggithub commented 3 months ago

We upgraded to node 18 and find that requests telemetry work but dependency telemetry is not logged (with latest version 2.9.5. or with an old version 2.5.2). With node 16, it works well. Is it by design that node 18 and above won't work with appInsights?

JacksonWeber commented 3 months ago

@arisewanggithub Some further information would be helpful here. Can you provide the package.json for the application you're instrumenting and taking a look at the code where you expect dependency telemetry to be generated would be helpful too.

arisewanggithub commented 3 months ago

@JacksonWeber, I've sent you the package.json in private. Please let me know if you need anything else to troubleshoot.

JacksonWeber commented 3 months ago

@arisewanggithub I'm sorry, but I'm not quite sure where you sent the package.json. What method did you use to send it to me?

arisewanggithub commented 3 months ago

@JacksonWeber , I sent the files to your corp email. I also pinged you on teams. Please let me know if you didn't receive it.

cristobal commented 3 months ago

Works fine here with Node 20 and Fastify, not sure if you had the same problem that we had, however we needed todo some changes to our code after upgrading the application insights package. Since it suddenly stopped tracking the http requests to our server application.

Old code:

const app = fastify(fastifyOptions);
...
configureApplicationInsights();

New Code:

configureApplicationInsights();

const app = fastify(fastifyOptions);
...

The issue at hand

One needs to initialize and start the application insights client before the server is initialized, otherwise it seems that the http.createServer wrapper created here will not be initialized and not be able to intercept the incoming requests to the server.

Seems like the changes in the code happened about 6 months ago.

arisewanggithub commented 3 months ago

@cristobal , thanks for the reply. But that doesn't look like to be the cause of my issue. Also, in my case, request telemetry works. What is broken is dependency telemetry. And for the change you quoted, it's for request telemetry, not for dependency telemetry. So, it should be irrelevant, IMHO. Our code is using React. And we use the fetch API to send dependency requests.

JacksonWeber commented 3 months ago

Closing this issue as auto instrumentation of the fetch API is not supported. Thank you for your report.

arisewanggithub commented 3 months ago

A summary of what happened. From node 18, node.js has a native implementation of the fetch API. In node 16, node-fetch was used and app insights still supports it. But app insights doesn't support this native implementation of the fetch API. And in node 18, the native fetch is used by default. That's why the Dependency telemetry breaks. A workaround is to force to use node-fetch in node 18.