Skip to content

Commit 6cca660

Browse files
safihamidbrettsam
authored andcommitted
Fix for impact of enabling AppInsights on functions cold startup time.
#4183
1 parent 6e095d2 commit 6cca660

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/WebJobs.Script/ScriptHostBuilderExtensions.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using Microsoft.ApplicationInsights;
7+
using Microsoft.ApplicationInsights.DependencyCollector;
78
using Microsoft.ApplicationInsights.Extensibility;
89
using Microsoft.ApplicationInsights.Extensibility.Implementation;
910
using Microsoft.Azure.WebJobs.Host.Executors;
@@ -234,7 +235,9 @@ public static IHostBuilder SetAzureFunctionsConfigurationRoot(this IHostBuilder
234235
internal static void ConfigureApplicationInsights(HostBuilderContext context, ILoggingBuilder builder)
235236
{
236237
string appInsightsKey = context.Configuration[EnvironmentSettingNames.AppInsightsInstrumentationKey];
237-
if (!string.IsNullOrEmpty(appInsightsKey))
238+
239+
// Initializing AppInsights services during placeholder mode as well to avoid the cost of JITting these objects during specialization
240+
if (!string.IsNullOrEmpty(appInsightsKey) || SystemEnvironment.Instance.IsPlaceholderModeEnabled())
238241
{
239242
builder.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
240243
builder.Services.ConfigureOptions<ApplicationInsightsLoggerOptionsSetup>();
@@ -252,6 +255,19 @@ internal static void ConfigureApplicationInsights(HostBuilderContext context, IL
252255

253256
return client;
254257
});
258+
259+
if (SystemEnvironment.Instance.IsPlaceholderModeEnabled())
260+
{
261+
for (int i = 0; i < builder.Services.Count; i++)
262+
{
263+
// This is to avoid possible race condition during specialization when disposing old AI listeners created during placeholder mode.
264+
if (builder.Services[i].ServiceType == typeof(ITelemetryModule) && builder.Services[i].ImplementationFactory?.Method.ReturnType == typeof(DependencyTrackingTelemetryModule))
265+
{
266+
builder.Services.RemoveAt(i);
267+
break;
268+
}
269+
}
270+
}
255271
}
256272
}
257273

0 commit comments

Comments
 (0)