Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.94k stars 441 forks source link

How to exclude specific Trace from getting sampled out in AppInsights using in .net 6,7 #9656

Closed inishantmishra closed 11 months ago

inishantmishra commented 1 year ago

Our Log messages either, Info, Warning, or Error sometimes get sampled out. That's okay, but some logs are very important and we don't wanna lose them. I read multiple articles to use TelemeteryProcessor or TelemeteryInitizlizers but it doesn't help.

I want some of my logs which are important, those logs should never get sampled out. When I implemented the telemetry processors and initializers I found that these logs are now even getting more sampled out as compared to other logs.

Please help how to do that.

Here is the repo link. https://github.com/inishantmishra/TestAzureDotnetIsolatedFunction/tree/master I have an Azure Function- LoggingPOCFunction which has 3 logging. 1 should get Sample with whatever logic exists for adaptive sampling and 2 logging should never be sampled.

I created an extension method that uses those 2 Loggings. Kindly review and let me know what am i missing it here?

@kshyju @aishwaryabh @brettsam

inishantmishra commented 12 months ago

Can I get any reply here? @brettsam @kshyju

RohitRanjanMS commented 11 months ago

@inishantmishra You can add an ITelemetryProcessor to set the ProactiveSamplingDecision. https://github.com/RohitRanjanMS/ApplicationInsightsDemo/blob/main/InProcApplicationInsightsDemo/Startup.cs

using InProcApplicationInsightsDemo;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;

[assembly: FunctionsStartup(typeof(Startup))] 
namespace InProcApplicationInsightsDemo
{
    internal class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton<ITelemetryModule, MyModule>();
        }
    }

    internal class MyModule : ITelemetryModule
    {
        public void Initialize(TelemetryConfiguration configuration)
        {
            configuration.TelemetryProcessorChainBuilder.Use(next => new SamplingProcessor(next));
        }
    }

    internal class SamplingProcessor : ITelemetryProcessor
    {
        private readonly ITelemetryProcessor _next;

        public SamplingProcessor(ITelemetryProcessor next)
        {
            _next = next;
        }

        public void Process(ITelemetry item)
        {
            if (item is ISupportProperties propItem)
            {
                propItem.Properties["Processor"] = "SamplingProcessor";
            }

            // Ensure this is logged always
            if (item is TraceTelemetry trace && trace.Message.Contains("Important:"))
            {
                trace.ProactiveSamplingDecision = SamplingDecision.SampledIn;
            }
            _next.Process(item);
        }
    }
}
inishantmishra commented 11 months ago

@RohitRanjanMS What does SampledIn here indicates for samplingdecision? Will it be ignored???

I tried with this my logs are getting ignored. But when I used SampledOut i am getting logs. Please let me know what is correct?

RohitRanjanMS commented 11 months ago

SampledIn means the record would be retained and ingested. You can try running my sample code to check the behavior.