-
Notifications
You must be signed in to change notification settings - Fork 294
Description
Is your feature request related to a problem?
In production environments, observability is often considered critical — the system should fail to start if telemetry is not properly configured. However, currently AddApplicationInsightsTelemetry*
and OpenTelemetry registration fail silently on configuration errors (e.g., missing or invalid connection string), which can lead to applications running without monitoring or visibility.
As an example, this makes it hard to detect when TelemetryClient
is not available in DI, causing runtime exceptions in code that depends on it (e.g., decorators, middlewares).
Describe the solution you'd like.
A boolean option like FailOnConfigurationError or ThrowOnRegistrationFailure, that:
- Logs the original error (as today),
- But then throws an exception, stopping the application startup,
- Is opt-in, so the current behavior remains the default for backward compatibility.
Describe alternatives you've considered.
- Manually calling GetRequiredService() in Program.cs to detect failure at startup.
- Implementing a StartupValidator pattern.
- Wrapping TelemetryClient in a NullObject pattern.
While these workarounds are possible, they are not ideal - the telemetry libraries already detect the problem, but do not expose a way to escalate it.
Additional context.
Other frameworks and libraries (like ASP.NET Core DI, Serilog, Entity Framework) offer similar fail-fast options for critical services. This would bring parity to observability setup and make it easier to ensure production readiness.
Suggested API
builder.Services.AddApplicationInsightsTelemetryWorkerService(options =>
{
options.ConnectionString = "...";
options.FailOnConfigurationError = true;
});
Lines 88 to 115 in 98bc6c5
public static IServiceCollection AddApplicationInsightsTelemetryWorkerService(this IServiceCollection services) | |
{ | |
try | |
{ | |
if (!IsApplicationInsightsAdded(services)) | |
{ | |
services.AddSingleton<ITelemetryInitializer, AzureWebAppRoleEnvironmentTelemetryInitializer>(); | |
AddCommonInitializers(services); | |
AddCommonTelemetryModules(services); | |
AddTelemetryChannel(services); | |
services | |
.TryAddSingleton<IConfigureOptions<ApplicationInsightsServiceOptions>, | |
DefaultApplicationInsightsServiceConfigureOptions>(); | |
AddDefaultApplicationIdProvider(services); | |
AddTelemetryConfigAndClient(services); | |
AddApplicationInsightsLoggerProvider(services); | |
} | |
return services; | |
} | |
catch (Exception e) | |
{ | |
WorkerServiceEventSource.Instance.LogError(e.ToInvariantString()); | |
return services; | |
} | |
} |