1

I'm struggling to disable dependency tracking in a project that uses Azure Functions (v4) using .NET 9 as an Isolated Process. I want to do this as these entries are clogging up our log files with information we don't use, which is ultimately costing us money.

Our host.json files looks as follows:

{
    "version": "2.0",
    // By default these new logging filters are filtered out by the function logs. You need to modify the host.json file
    // to opt-in for additional filters and categories.
    "logging": {
      "applicationInsights": {
        "samplingSettings": {
          "isEnabled": true
        },
        "enableDependencyTracking": false
      },
        "logLevel": {
            "Witness.Io": "Information"
        }
    },
    "extensions": {
        "queues": {
            "maxPollingInterval": "00:00:10.000",
            "batchSize": 1 // The number of queue messages that the Functions runtime retrieves simultaneously and processes in parallel.
        }
    },
    // Set functionTimeout to 10 minutes
    "functionTimeout": "00:10:00"
}

This didn't resolve the issue so I tried writing a telemetry processor based on a blog post I found here - https://github.com/RohitRanjanMS/ApplicationInsightsDemo/blob/main/Isolated/IsolatedApplicationInsightsDemo/CustomTelemetryProcessor.cs.

The Main method of our Program.cs looks like this:

public static void Main(string[] args)
{
    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication(config =>
        {
            config.UseNewtonsoftJson();
        })
        .ConfigureServices(services =>
        {
            // Disable dependency tracking - based on approach detailed here - https://github.com/RohitRanjanMS/ApplicationInsightsDemo/blob/main/Isolated/IsolatedApplicationInsightsDemo/CustomTelemetryProcessor.cs.
            services.AddApplicationInsightsTelemetryProcessor<DependencyTrackingProcessor>();
            services.AddApplicationInsightsTelemetryWorkerService();
            services.ConfigureFunctionsApplicationInsights();

            var keyVaultService = new KeyVaultService(new Uri(Environment.GetEnvironmentVariable("KEY_VAULT_URI") ?? throw new InvalidOperationException($"Missing environment variable: '{"KEY_VAULT_URI"}'.")));
            services.AddKeyVaultService(keyVaultService);

            services.AddTransient<IFunctionEnvironment, FunctionEnvironment>();
            services.AddTransient<IAuditEntityRepository, AuditEntityRepository>();
            services.AddConfigRepository<IUserConfigurationEntityRepository, UserConfigurationEntityRepository>();
            services.AddTransient<IAutoDeleteExperimentMessageRepository, AutoDeleteExperimentMessageRepository>();
            services.AddTransient<IExperimentEntityRepository, ExperimentEntityRepository>();
            services.AddTransient<IExperimentBlobRepository, ExperimentBlobRepository>();
            services.AddTransient<IExperimentMessageQueueRepository, ExperimentMessageQueueRepository>();
            services.AddTransient<IExperimentCompletedQueueRepository, ExperimentCompletedQueueRepository>();
            services.AddTransient<IScenarioEntityRepository, ScenarioEntityRepository>();
            services.AddTransient<IScenarioCompletedQueueRepository, ScenarioCompletedQueueRepository>();
            services.AddTransient<IReplicationEntityRepository, ReplicationEntityRepository>();
            services.AddTransient<IReplicationServicesScalingQueueRepository, ReplicationServicesScalingQueueRepository>();
            services.AddTransient<IReplicationQueueRepository, ReplicationQueueRepository>();
            services.AddTransient<IReplicationCompletedQueueRepository, ReplicationCompletedQueueRepository>();
            services.AddTransient<IShutdownServicesMessageQueueRepository, ShutdownServicesMessageQueueRepository>();
            services.AddTransient<IRunningCoresInfoEntityRepository, RunningCoresInfoEntityRepository>();
            services.AddTransient<ILicenseCostUsage, LicenseCostUsage>();
            services.AddTransient<ILicenseCostUsageEntityRepository, LicenseCostUsageEntityRepository>();

            services.AddTransient<Experiment>();
            services.AddTransient<Scenario>();
            services.AddTransient<Replication>();

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            LogConfiguration(keyVaultService);
        })
        .ConfigureLogging(logging =>
        {
            // Allow all Information-level App Insights logs through (this defaults to Warning).
            RemoveApplicationInsightsFilter(logging.Services);
        })
        .Build();

    host.Run();
}

The processor looks as follows:

/// <summary>
/// Dependency tracking processor.
/// </summary>
/// <remarks>This is used to filter out the dependency tracking logs that we don't want.</remarks>
public class DependencyTrackingProcessor : ITelemetryProcessor
{
    /// <summary>
    /// Initializes a new instance of the <see cref="DependencyTrackingProcessor"/> class. Link processors to each other in the chain.
    /// </summary>
    /// <param name="nextProcessor">The next telemetry processor.</param>
    public DependencyTrackingProcessor(ITelemetryProcessor nextProcessor)
    {
        NextProcessor = nextProcessor;
    }

    private ITelemetryProcessor NextProcessor { get; }

    /// <inheritdoc/>
    public void Process(ITelemetry item)
    {
        if (item is DependencyTelemetry)
        {
            // Do not send telemetry for dependency tracking.
            return;
        }

        // Call the next processor in the chain.
        NextProcessor.Process(item);
    }
}

None of this seems to have any impact. Is anyone able to tell me what I'm doing wrong?

Thanks

Kevin

2
  • Do you want to exclude dependency logs? Can you try with host.json { "version": "2.0", "logging": { "applicationInsights": { "enableDependencyTracking": false, "samplingSettings": { "isEnabled": true } } } } Commented Jun 13 at 12:53
  • @PravallikaKV Thanks for your suggestion. I've tried it and I still have the same issue. Commented Jun 16 at 6:36

1 Answer 1

0

Disabling dependency tracking in isolated Azure Functions using host.json settings often may not take full effect as mentioned in the article.

Use below custom ITelemetryProcessor to filter out dependency telemetry.

DependencyTrackingProcessor.cs:

public class DependencyTrackingProcessor : ITelemetryProcessor
{
    private readonly ITelemetryProcessor _next;

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

    public void Process(ITelemetry item)
    {
        if (item is DependencyTelemetry)
        {
            return;
        }

        _next.Process(item);
    }
}

In Program.cs, register the processor and configure Application Insights to disable Dependency Tracking.

Program.cs:

var builder = FunctionsApplication.CreateBuilder(args);

builder.ConfigureFunctionsWebApplication();

builder.Services
    .AddApplicationInsightsTelemetryWorkerService()
    .ConfigureFunctionsApplicationInsights()
    .AddApplicationInsightsTelemetryProcessor<DependencyTrackingProcessor>();

builder.Services.Configure<LoggerFilterOptions>(options =>
{
    var toRemove = options.Rules.FirstOrDefault(rule =>
        rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
    if (toRemove != null)
    {
        options.Rules.Remove(toRemove);
    }
});

builder.Build().Run();

Function.cs:

[Function("Function1")]
public IActionResult Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
    _logger.LogInformation("HTTP trigger function received a request at {Time}.", DateTime.UtcNow);

    var response = new OkObjectResult("Welcome to Azure Functions!");
    return response;
}

host.json:

{
    "version": "2.0",
    "logging": {
      "applicationInsights": {
        "samplingSettings": {
          "isEnabled": true,
          "excludedTypes": "Request"
        },
        "enableLiveMetricsFilters": true,
        "enableDependencyTracking": false
      }
    }
}

Navigate to Function App=>Monitoring=>Logs, run below query:

dependencies 
| where timestamp > ago(1h)

If this returns no results, it means that dependency telemetry is not being collected.

enter image description here

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.