microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
324 stars 141 forks source link

HTTP Triggered Function Response is undefined #1149

Closed GravlLift closed 1 year ago

GravlLift commented 1 year ago

I've got an http triggered azure function of mine that's throwing an unhandled exception. Terrible and whatnot, but very fixable. However, I noticed that whenever my function throws said unhandled exception, I get this lovely AI error in my logs:

[2023-05-22T19:58:28.557Z] ApplicationInsights:Error creating automatic incoming request in Azure Functions [
[2023-05-22T19:58:28.559Z]   TypeError: Cannot read properties of null (reading 'statusCode')
[2023-05-22T19:58:28.561Z]       at AzureFunctionsHook2.AzureFunctionsHook2._createIncomingRequestTelemetry (\node_modules\applicationinsights\AutoCollection\AzureFunctionsHook.ts:112:39)
[2023-05-22T19:58:28.562Z]       at <anonymous> (\node_modules\applicationinsights\AutoCollection\AzureFunctionsHook.ts:96:46)
[2023-05-22T19:58:28.563Z]       at Function.CorrelationContextManager2.runWithContext (\node_modules\applicationinsights\AutoCollection\CorrelationContextManager.ts:120:16)
[2023-05-22T19:58:28.564Z]       at AzureFunctionsHook2.<anonymous> (\node_modules\applicationinsights\AutoCollection\AzureFunctionsHook.ts:95:63)
[2023-05-22T19:58:28.566Z]       at step (\node_modules\applicationinsights\AutoCollection\NativePerformance.ts:19:14)
[2023-05-22T19:58:28.567Z]       at Object.next (\node_modules\applicationinsights\AutoCollection\NativePerformance.ts:19:14)
[2023-05-22T19:58:28.569Z]       at <anonymous> (\node_modules\applicationinsights\AutoCollection\NativePerformance.ts:19:14)
[2023-05-22T19:58:28.570Z]       at new Promise (<anonymous>)
[2023-05-22T19:58:28.571Z]       at __awaiter (\node_modules\applicationinsights\AutoCollection\NativePerformance.ts:19:14)
[2023-05-22T19:58:28.573Z]       at <anonymous> (\node_modules\applicationinsights\AutoCollection\AzureFunctionsHook.ts:81:148)
[2023-05-22T19:58:28.575Z] ]

Relevant library code: https://github.com/microsoft/ApplicationInsights-node.js/blob/45e06c608a291442ad2a100dee771fc1e8908d74/AutoCollection/AzureFunctionsHook.ts#L79-L134

Specifically, the issue is that on line 89, _getAzureFunctionResponse returns an undefined response. This makes complete sense, as _getAzureFunctionResponse looks like this: https://github.com/microsoft/ApplicationInsights-node.js/blob/45e06c608a291442ad2a100dee771fc1e8908d74/AutoCollection/AzureFunctionsHook.ts#L136-L145

... and given that the function didn't generate a response due to the unhandled exception, there's going to be an issue getting the response.

The azure functions framework itself has handling for this and returns a 500 response, but the AI request telemetry is lost due to the exception.