microsoft / ApplicationInsights-node.js

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

ApplicationInsights:Failed to propagate context in Azure Functions #1111

Closed Codesleuth closed 1 year ago

Codesleuth commented 1 year ago

I'm getting the following error showing in our Azure Function log stream:

[Error]   ApplicationInsights:Failed to propagate context in Azure Functions [
[Error]     TypeError: Cannot read properties of null (reading 'customProperties')
[Information]         at AzureFunctionsHook.<anonymous> (/home/site/wwwroot/node_modules/applicationinsights/out/AutoCollection/AzureFunctionsHook.js:84:42)
[Information]         at step (/home/site/wwwroot/node_modules/applicationinsights/out/AutoCollection/AzureFunctionsHook.js:33:23)
[Information]         at Object.next (/home/site/wwwroot/node_modules/applicationinsights/out/AutoCollection/AzureFunctionsHook.js:14:53)
[Information]         at /home/site/wwwroot/node_modules/applicationinsights/out/AutoCollection/AzureFunctionsHook.js:8:71
[Information]         at new Promise (<anonymous>)
[Information]         at __awaiter (/home/site/wwwroot/node_modules/applicationinsights/out/AutoCollection/AzureFunctionsHook.js:4:12)
[Information]         at /home/site/wwwroot/node_modules/applicationinsights/out/AutoCollection/AzureFunctionsHook.js:78:136
[Information]         at t.WorkerChannel.<anonymous> (/azure-functions-host/workers/node/dist/src/worker-bundle.js:2:16100)
[Information]         at Generator.next (<anonymous>)
[Information]         at /azure-functions-host/workers/node/dist/src/worker-bundle.js:2:14637
[Information]   ]

This is using v2.5. A key thing to note is that we are not enabling any auto collection:

// note that we do not call `.start()`
appInsights.setup();

Even if we don't use the client directly, this still throws this error in the logs.

The Azure Function which is running here is an eventHubTrigger. These errors seem to align directly with each function invocation, so it suggests to me that auto collection is making an attempt to do something and it really shouldn't be.

Any ideas?

willdkp commented 9 months ago

Hi I am getting this error as well, what was the resolution?

hectorhdzg commented 9 months ago

@willdkp can you add more details about your environment? this should be fixed in latest version of the SDK, if you are already using it then most likely you are hitting a different error, we can help with that if you provide more details about it.

willdkp commented 9 months ago

I see this every minute or so in my azure functions app, either run on the cloud or locally emulated:

[2023-10-03T19:27:23.602Z] ApplicationInsights:Failed to propagate context in Azure Functions [ [2023-10-03T19:27:23.603Z] TypeError: Cannot read properties of null (reading 'customProperties') [2023-10-03T19:27:23.604Z] at AzureFunctionsHook.<anonymous> (*******************\node_modules\applicationinsights\out\AutoCollection\AzureFunctionsHook.js:84:42) [2023-10-03T19:27:23.608Z] at step (*******************\node_modules\applicationinsights\out\AutoCollection\AzureFunctionsHook.js:33:23) [2023-10-03T19:27:23.609Z] at Object.next (*******************\node_modules\applicationinsights\out\AutoCollection\AzureFunctionsHook.js:14:53) [2023-10-03T19:27:23.611Z] at *******************\node_modules\applicationinsights\out\AutoCollection\AzureFunctionsHook.js:8:71 [2023-10-03T19:27:23.612Z] at new Promise (<anonymous>) [2023-10-03T19:27:23.613Z] at __awaiter (*******************\node_modules\applicationinsights\out\AutoCollection\AzureFunctionsHook.js:4:12) [2023-10-03T19:27:23.614Z] at *******************\node_modules\applicationinsights\out\AutoCollection\AzureFunctionsHook.js:78:136 [2023-10-03T19:27:23.615Z] at t.WorkerChannel.<anonymous> (*******************\AppData\Roaming\nvm\v18.14.1\node_modules\azure-functions-core-tools\bin\workers\node\dist\src\worker-bundle.js:2:17020) [2023-10-03T19:27:23.617Z] at Generator.next (<anonymous>) [2023-10-03T19:27:23.618Z] at *******************\AppData\Roaming\nvm\v18.14.1\node_modules\azure-functions-core-tools\bin\workers\node\dist\src\worker-bundle.js:2:15490

From package.json

"dependencies": { "@azure/identity": "^3.0.0", "@azure/keyvault-secrets": "^4.6.0", "@azure/storage-queue": "^12.10.0", "applicationinsights": "^2.3.4", "durable-functions": "^2.0.2", "moment": "^2.29.4", "mssql": "^9.0.1", "node-fetch": "^2.6.7", "parse-link-header": "^2.0.0" }

In code I do:

const appInsights = require('applicationinsights'); appInsights.setup(); const aiClient = appInsights.defaultClient;

hectorhdzg commented 9 months ago

@willdkp the original issue was fixed in version 2.5.1, I recommend you to update to latest applicationinsights (currently 2.8.0), let me know if this is still an issue after updating.