Azure / azure-webjobs-sdk

Azure WebJobs SDK
MIT License
738 stars 358 forks source link

Customization of the DependencyTrackingTelemetryModule configuration to add the "Microsoft.Azure.EventHubs" diagnostic source activiy. #2023

Open RomainLasso opened 5 years ago

RomainLasso commented 5 years ago

Context : Log external activities to Application Insights in an Azure Function v2.

There is no "built-in" way to customize the DependencyTrackingTelemetryModule class configuration.

By default the DependencyTrackingTelemetryModule class is only configured to track the "Microsoft.Azure.ServiceBus" diagnostic source activies, while it could also handle the "Microsoft.Azure.EventHubs" diagnostic source.

In my case, I want to log both ServiceBus and EventHub activities.

I had to change the DependencyTrackingTelemetryModule class instanciation with a new one :

ServiceDescriptor serviceDescriptorToDelete = null;
var serviceDescriptors = serviceCollection.Where(s => s.ServiceType == typeof(ITelemetryModule)).ToList();
foreach (var service in serviceDescriptors)
{
    if (service.ImplementationFactory != null &&
        service.ImplementationFactory.Method.ReturnType == typeof(DependencyTrackingTelemetryModule))
    {
        serviceDescriptorToDelete = service;
    }
}

if (serviceDescriptorToDelete != null)
{
    serviceCollection.Remove(serviceDescriptorToDelete);
}

serviceCollection.AddSingleton<ITelemetryModule, DependencyTrackingTelemetryModule>(provider =>
{
    var dependencyCollector = new DependencyTrackingTelemetryModule();
    var excludedDomains = dependencyCollector.ExcludeComponentCorrelationHttpHeadersOnDomains;
    excludedDomains.Add("core.windows.net");
    excludedDomains.Add("core.chinacloudapi.cn");
    excludedDomains.Add("core.cloudapi.de");
    excludedDomains.Add("core.usgovcloudapi.net");
    excludedDomains.Add("localhost");
    excludedDomains.Add("127.0.0.1");

    var includedActivities = dependencyCollector.IncludeDiagnosticSourceActivities;
    includedActivities.Add("Microsoft.Azure.ServiceBus");
    includedActivities.Add("Microsoft.Azure.EventHubs"); // <- Change here

    return dependencyCollector;
});

Are there any reasons for excluding the "Microsoft.Azure.EventHubs" diagnostics sources ?

Is there a better way to implement this behavior ?

Thanks,

Romain

RomainLasso commented 5 years ago

I forgot to mention that these changes were made in a custom Azure Function extension.

vanwx commented 2 years ago

I think it's fixed in 2022?

https://github.com/microsoft/ApplicationInsights-dotnet/blob/0a3ef80a7369b4014b889f0bbfdd872f2bc4e8ad/WEB/Src/DependencyCollector/DependencyCollector/DependencyTrackingTelemetryModule.cs#L153