azureazure-application-insightsopen-telemetryazure-monitordotnet-aspire

How to avoid OpenTelemetry export to Azure Monitor exporting all dependencies of its own service


I am using .NET Aspire and have OpenTelemetry export enabled to Azure Monitor.
I have also enabled tracing, dependencies, etc. The code can be seen below. Now, I see all data of the ILogger arriving in Application Insights (Azure Monitor). I also use TelemetryClient, to trace custom events and metrics.

Now, when I look at my applicatoin insights, I see there are many dependencies tracked that I actually want to exclude. These events seem to be related to the OpenTelemetry export itself. And my question is how to exclude those events to appear in my logs ?

The screenshot below shows an example of such entries

application insights screenshot

public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder)
    where TBuilder : IHostApplicationBuilder
{
    builder.Logging.AddOpenTelemetry(logging =>
    {
        logging.IncludeFormattedMessage = true;
        logging.IncludeScopes = true;
    });
    builder.Logging.AddFilter<OpenTelemetryLoggerProvider>("*", LogLevel.Information);

    if (!string.IsNullOrEmpty(builder.Configuration[ServiceNames.AppInsightsEnvironmentVariable]))
    {
        builder.Services.AddOpenTelemetry().UseAzureMonitor();
    }

    return builder;
}

Solution

  • After a thorough search, I finally discovered where the logs were coming from. (and that was not visible in the above code).

    In another library, I needed to inject the Azure Application Insights TelemetryClient , as I need to access that one for CustomEvents, etc. I was using the following code for this, which seems to default to those detailed dependency traces.

    services.AddApplicationInsightsTelemetryWorkerService();
    

    (If you want, you can set the options in the method, to exclude those logs, but I decided I only needed the default TelemetryClient, so therefore, I inserted it manually, using the following code instead.

    // Register TelemetryConfiguration manually
    services.AddSingleton<TelemetryConfiguration>(sp =>
    {
        var config = TelemetryConfiguration.CreateDefault();
        if(!string.IsNullOrEmpty( options?.LoggingOptions?.ApplicationInsightsConnectionString))
        {
            config.ConnectionString = options.LoggingOptions.ApplicationInsightsConnectionString;
        }
        return config;
    });
    
    // Register TelemetryClient
    services.AddSingleton<TelemetryClient>();