Is this Application Insight / Azure Functions Bug?

2019-06-10 08:29发布

问题:

Today I integrate my Azure function with Application insight for application logging, especially to capture error stack-trace.

First I have written Azure function without try-catch block so It was showing correct status/Information in Monitor section and application insight as well.

Later I added try-catch block and logged some more data like

catch(Exception ex)
{
    log.Error(inputData);
    log.Error(ex.Message);
    return req.CreateResponse(HttpStatusCode.InternalServerError);
}

You can see in below attachment, ResultCode is 500 with Green Status... Why? I think because of this issue Application Insight not showing this data in Error/Failed request query.

No record found in Application Insight

exceptions
|where operation_Id == "c5b5a345-fa11-4356-b769-b34d1c6619e5" 
| order by timestamp desc 
| project operation_Id , timestamp 

回答1:

Success check denotes whether your Azure Function call succeeded (= no exception thrown) or failed (= exception thrown).

At your first invocation, an exception occurred, so function invocation didn't terminate normally, thus red checkmark.

When you catch and return 500 manually, that's still OK in terms of function invocation - it finished and returned the result back.

Functions runtime doesn't follow HTTP semantics, the rules are universal for all trigger types.

Application Insight Default metrics won't show handled exception in failed request graph, Developer need build query for handled exception e.g.

requests
| where success == "False" and timestamp >= ago(7d) 
| join kind= inner traces on operation_Id  
| project operation_Id , timestamp, message, severityLevel  
| order  by timestamp, operation_Id

severityLevel :- 1 = Info and 3 = Error