Telemetry sampling without affecting the errors/failures

痞子三分冷 提交于 2019-12-07 15:37:50

问题


I want to log a percentage of the success calls in app insights. I came across this post https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling and I think Fixed-rate sampling is appropriate here. But does this affect all logging equally? Will some errors/failures no longer be logged?

I am looking for a solution that logs a percentage of the success calls, but keeps all failed requests/errors.


回答1:


I don't think this is supported out of the box, but you can write your own ITelemetryProcessor.

See: https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#filtering-itelemetryprocessor

Application Insights in .NET uses a chain of telemetry processors that you can use to filter telemetry, so you can write your own that checks the resultCode (I think that's what Application Insights calls the HTTP status code, but you'll have to double check) of a request telemetry object, and approves it if it's 500 (or 5xx) but only has a 10% chance of sending it if it's 2xx or 3xx. You can override the OKToSend() method to perform the above check on the ITelemetry input, and return true / false accordingly.

Maybe something like (I wrote this in the browser, it won't necessarily work flawlessly as-is):

// Approves 500 errors and 10% of other telemetry objects
private bool OKtoSend (ITelemetry telemetry)
{
    if (telemetry.ResponseCode == 500) {
        return true;
    } else {
        Random rnd = new Random();
        int filter = rnd.Next(1, 11);
        return filter == 1;
    }
}



回答2:


To exclude failed events from being subject to sampling, (while doing sampling for everything else) write a TelemetryInitializer with this logic.

public class PreventSamplingForFailedTelemetryInitializer: ITelemetryInitializer
{
  public void Initialize(ITelemetry telemetry)
  {
        if(failed)
        {
            // Set to 100, so that actual SamplingProcessors ignore this from sampling considerations.
            ((ISupportSampling)telemetry).SamplingPercentage = 100;
        }
   }
}

(Make sure to add this TelemetryInitializer to the TelemetryConfiguration)

Failed or not can be determined from RequestTelemetry and DependencyTelemetry from their `Success` field.

(the last one in FAQ sections has hints to answer your question https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling#frequently-asked-questions)


来源:https://stackoverflow.com/questions/55731036/telemetry-sampling-without-affecting-the-errors-failures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!