H4p7ic
H4p7ic

Reputation: 1923

Why does application insight log 400 bad request as successful request and not log exception

i am encountering a problem i'm not familiar with.

So i'm trying to log exceptions from a test Azure Function but when i throw an exception and return a 400 bad request, application insight registers the log as a successful request.

As i understand it it is probably register the function's successful run but what i don't understand is how i then should log the exception.

So what i have done so far is this.

(I Will be referring to Application Insights as AI from here on out)

I started by created an AI-Resource.

Then i took the instrument key and applied it to the app settings of my function.

After that i installed the AI NUGET to my function bu creating a Projet.json file and then pasting something like this which installed the necessary assemblies and such.

{
  "frameworks": {
  "net46":{
  "dependencies": {
    "Microsoft.ApplicationInsights": "2.4.0"
      }
    }
  }
}

after this i initialize the TelemetryClient in the function and try to log and exception in a catch:

Initiation:

    string key = TelemetryConfiguration.Active.InstrumentationKey = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process);
    TelemetryClient telemetry = new TelemetryClient() { 
    InstrumentationKey = key 
    }; 

Catch:

catch (Exception e)
{
    Dictionary<string,string> properties = new Dictionary<string,string>();

    properties.Add("Function Payload", data.ToString());
    properties.Add("Function Exception", e.ToString());

    telemetry.TrackException(e, properties);

    return req.CreateResponse(HttpStatusCode.BadRequest, e);
}

Test running the function i get:

2018-03-07T14:24:36.171 [Info] Function started (Id=0292b455-314d-4c4c-872a-2b8137a72305)
2018-03-07T14:24:37.092 [Info] Function completed (Success, Id=0292b455-314d-4c4c-872a-2b8137a72305, Duration=931ms)

Test function result window

In Application insights i can can only see bad requests for StatusCode: 500 but 400 Bad requests gets logged as Successful requests.

And also the TrackException functionality doesn't log any of the custom properties...

enter image description here

So what am i missing?

MORE DETAILS ABOUT EXCEPTION LOGGING:

enter image description here enter image description here

Upvotes: 2

Views: 6038

Answers (3)

brettsam
brettsam

Reputation: 2802

@Mikhail is right that we treat this as a success because the function is a success. We didn't want to use status codes to guess whether there was a successful operation or not, so we look for whether the function threw an exception.

Your exception isn't appearing in that screen because it hasn't been property correlated with this function execution. If you go to App Insights Analytics and query for that ExceptionTelemetry, you should see it.

In order to correlate it with a specific function, you'd need to set the OperationId, which is the same as the function's InvocationId. There is a sample that shows how to do this with Events, Metrics, and Dependencies, but doing it for Exceptions is the same (you can ignore the User.Id assignment): https://learn.microsoft.com/en-us/azure/azure-functions/functions-monitoring#custom-telemetry-in-c-functions


Update: From what you've shown of your function above, you may be able to get away with doing something like:

catch (Exception e)
{    
    log.Error("Function Payload " + data.ToString());
    throw;
}

That would return a 500 (rather than a 400), and Functions would log the trace to Application Insights, and then log the exception as well as the Request failure. If you're not using your TelemetryClient anywhere else, you could remove that from your code.

Upvotes: 2

John Gardner
John Gardner

Reputation: 25178

the "for this operation" not showing exceptions implies that the exception that you sent does not have the same operationId as the azure function. operation id is how application insights "links" related telemetry together.

your "exeption logging" screenshot is not an exception, but a request, so the custom properties logged on your exception won't be there.

if you want your azure function to fail, and show as a failed request, and log an exception, why are you catching the exception and logging it yourself? doesn't catching the exception then cause the azure function to succeed? why not just let the exception trickle out and let the function runtime do that part for you? (doesn't it?)

Upvotes: 1

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35154

The server (Azure Function) processed the request without errors, you returned a result from it, so from the point of view of Function App runtime the request was processed successfully. You can see that from the log too:

... Function completed (Success, ...

So, it makes sense that the Function App registers the call as success in Application Insights too. At least, that's how they chose to implement it.

Upvotes: 1

Related Questions