in src/DotNetWorker.ApplicationInsights/FunctionsApplicationInsightsExtensions.cs [22:66]
public static IServiceCollection ConfigureFunctionsApplicationInsights(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.ConfigureOptions<TelemetryConfigurationSetup>();
services.AddSingleton<IConfigureOptions<AppServiceOptions>, AppServiceOptionsInitializer>();
services.AddSingleton<AppServiceEnvironmentVariableMonitor>();
services.AddSingleton<IOptionsChangeTokenSource<AppServiceOptions>>(p => p.GetRequiredService<AppServiceEnvironmentVariableMonitor>());
services.AddSingleton<IHostedService>(p => p.GetRequiredService<AppServiceEnvironmentVariableMonitor>());
services.AddSingleton<FunctionsRoleInstanceProvider>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITelemetryInitializer, FunctionsTelemetryInitializer>());
services.AddSingleton<ITelemetryInitializer>(provider =>
{
// To match parity with the Host, we need to update the QuickPulseTelemetryModule.ServerId. We don't want to reference the
// top-level WorkerService or AspNetCore packages, so we cannot use ConfigureTelemetryModules().
//
// Nesting this setup inside this ITelemetryInitializer factory as it guarantees it will be run before
// any ITelemetryModules are initialized.
var modules = provider.GetServices<ITelemetryModule>();
var quickPulseModule = modules.OfType<QuickPulseTelemetryModule>().SingleOrDefault();
if (quickPulseModule is not null)
{
var roleInstanceProvider = provider.GetRequiredService<FunctionsRoleInstanceProvider>();
quickPulseModule.ServerId = roleInstanceProvider.GetRoleInstanceName();
}
return ActivatorUtilities.CreateInstance<FunctionsRoleEnvironmentTelemetryInitializer>(provider);
});
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITelemetryModule, FunctionsTelemetryModule>());
services.AddOptions<FunctionsApplicationInsightsOptions>()
.Validate<IServiceProvider>(
(_, sp) => sp.GetService<TelemetryClient>() is not null,
"Application Insights SDK has not been added. Please add and configure the Application Insights SDK. See https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service for more information.");
services.AddHostedService<ApplicationInsightsValidationService>();
// Lets the host know that the worker is sending logs to App Insights. The host will now ignore these.
services.Configure<WorkerOptions>(workerOptions => workerOptions.Capabilities["WorkerApplicationInsightsLoggingEnabled"] = bool.TrueString);
return services;
}