microsoft/ApplicationInsights-Kubernetes

Can't register Enrichers when using KeyedServices

tomaszzmuda opened this issue · 1 comments

Describe the bug
Can't register Enrichers when using KeyedServices when using .NET 8

Package Versions
Application Insights Kubernetes Version: 6.1.1
Application Insights SDK Version: 2.22.0

To Reproduce
Steps to reproduce the behavior:

  1. Create .NET 8 application with dependency injection and keyed services
  2. Try to invoke AddApplicationInsightsKubernetesEnricher
  3. Get an exception

Expected behavior
It is possible to add Kubernetes enrichers when you used keyed services.

Screenshots
Stacktrace:
image

Additional context

AzureAD/microsoft-identity-web#2604

In AddApplicationInsightsKubernetesEnricher method there is a check if initalizer already exists: KubernetesTelemetryInitializerExists. Implementation of that method checks if any ImplementationType from service collection is of type KubernetesTelemetryInitializer but getting ImplementationType property by design throws exception if it's keyed service.

/// <summary>
/// Gets the <see cref="Type"/> that implements the service.
/// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
public Type? ImplementationType
{
    get
    {
        if (IsKeyedService)
        {
            ThrowKeyedDescriptor();
        }
        return _implementationType;
    }
}
//
// Summary:
//     Checks if the KubernetesTelemetryInitializer exists in the service collection.
//
//
// Parameters:
//   serviceCollection:
//     The service collection.
private static bool KubernetesTelemetryInitializerExists(IServiceCollection serviceCollection)
{
    return serviceCollection.Any((ServiceDescriptor t) => t.ImplementationType == typeof(KubernetesTelemetryInitializer));
}