How to disable for automatically collecting the service bus dependency calls by Azure Application Insights .NetCore SDK

Viewed 682

I have developed .NetCore Web Job and enable the application insights. But currently the web job is running continuously and generates excess of service bus dependency calls into Azure Application Insights. So, I want to disable to service bus dependency calls to send to application insights.

Functions.cs

        [NoAutomaticTrigger]
    public async Task ProcessMessagesFromServiceBusQueue()
    {
        QueueRuntimeInfo queueInfo = null;
        while (true)
        {
            try
            {
                queueInfo = await _serviceBusManagementClient.GetQueueRuntimeInfoAsync(queueName);
                var count= queueInfo.MessageCountDetails.ActiveMessageCount > 0;
            }
            catch (ServiceBusTimeoutException)
            {
            }
        }
    }

I have tried to enable Adaptive Sampling by using below code. But still it generates excess of dependency calls.

     s.Configure<TelemetryConfiguration>((o) =>
            {
                o.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
                // Alternately, the following configures adaptive sampling with 2 items per second, and also excludes DependencyTelemetry from being subject to sampling.
                o.TelemetryProcessorChainBuilder.UseAdaptiveSampling(maxTelemetryItemsPerSecond: 1, excludedTypes: "Trace;Exception");
            });

Also, tried with this code to remove all dependency calls. But it is not working as expected.

private static IConfiguration GetConfiguration(ServiceCollection services)
    {
        var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        // build config
        var configuration = new ConfigurationBuilder()
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
           .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
           .AddEnvironmentVariables()
           .Build();
        services.AddApplicationInsightsTelemetry(configuration);
        var dependencyTrackingService = services.FirstOrDefault<ServiceDescriptor>(t => t.ImplementationType == typeof(DependencyTrackingTelemetryModule));
        if (dependencyTrackingService != null)
        {
            services.Remove(dependencyTrackingService);
        }
        return configuration;
    }

So, can anyone suggest how to disable for automatically collecting the service bus dependency calls from .NetCore Web Job.

2 Answers

If you use the Application Insights for Worker Service package this can be achieved using the following code

services.AddApplicationInsightsTelemetryWorkerService(
   new ApplicationInsightsServiceOptions
   {
        EnableDependencyTrackingTelemetryModule = false
   });

Please let us know which version of webjobs SDK you're using, 3.x or 2.x?

For filter out service bus dependency calls, you can try to implement a custom telemetry processor of application insights. Inside the custom function, write your code to determine if the telemetry data is service bus dependency, then determine to keep it or filter out it.

Related