From 08a9e85649964b5116f0fe7e309df3b16ff286a9 Mon Sep 17 00:00:00 2001 From: Saar Shen Date: Sun, 29 Aug 2021 13:00:04 -0700 Subject: [PATCH] Avoid building pre-mature service provider --- ...ubernetesDebuggingServiceCollectionBuilder.cs | 5 ++--- .../Extensions/ApplicationInsightsExtensions.cs | 4 +--- .../KubernetesServiceCollectionBuilder.cs | 16 ++-------------- ...mplePerformanceCounterTelemetryInitializer.cs | 14 +++++++++++++- .../KubernetesTestServiceCollectionBuilder.cs | 2 +- 5 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/ApplicationInsights.Kubernetes/Debuggings/KubernetesDebuggingServiceCollectionBuilder.cs b/src/ApplicationInsights.Kubernetes/Debuggings/KubernetesDebuggingServiceCollectionBuilder.cs index 44ec11f3..edde1041 100644 --- a/src/ApplicationInsights.Kubernetes/Debuggings/KubernetesDebuggingServiceCollectionBuilder.cs +++ b/src/ApplicationInsights.Kubernetes/Debuggings/KubernetesDebuggingServiceCollectionBuilder.cs @@ -1,5 +1,4 @@ -using System; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace Microsoft.ApplicationInsights.Kubernetes.Debugging @@ -13,7 +12,7 @@ public sealed class KubernetesDebuggingServiceCollectionBuilder : KubernetesServ /// Constructor for . /// public KubernetesDebuggingServiceCollectionBuilder(IOptions options) - : base(isRunningInKubernetes: () => true, options) { } + : base(isRunningInKubernetes: () => true) { } /// /// Registers setttings provider for querying K8s proxy. diff --git a/src/ApplicationInsights.Kubernetes/Extensions/ApplicationInsightsExtensions.cs b/src/ApplicationInsights.Kubernetes/Extensions/ApplicationInsightsExtensions.cs index 7a20b2a8..8906a178 100644 --- a/src/ApplicationInsights.Kubernetes/Extensions/ApplicationInsightsExtensions.cs +++ b/src/ApplicationInsights.Kubernetes/Extensions/ApplicationInsightsExtensions.cs @@ -124,9 +124,7 @@ private static void BuildK8sServiceCollection( IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null) { detectKubernetes ??= IsRunningInKubernetes; - using ServiceProvider provider = services.BuildServiceProvider(); - var options = provider.GetRequiredService>(); - kubernetesServiceCollectionBuilder ??= new KubernetesServiceCollectionBuilder(detectKubernetes, options); + kubernetesServiceCollectionBuilder ??= new KubernetesServiceCollectionBuilder(detectKubernetes); kubernetesServiceCollectionBuilder.RegisterServices(services); } diff --git a/src/ApplicationInsights.Kubernetes/Extensions/KubernetesServiceCollectionBuilder.cs b/src/ApplicationInsights.Kubernetes/Extensions/KubernetesServiceCollectionBuilder.cs index 9f711794..08b60c5b 100644 --- a/src/ApplicationInsights.Kubernetes/Extensions/KubernetesServiceCollectionBuilder.cs +++ b/src/ApplicationInsights.Kubernetes/Extensions/KubernetesServiceCollectionBuilder.cs @@ -4,7 +4,6 @@ using Microsoft.ApplicationInsights.Kubernetes; using Microsoft.ApplicationInsights.Kubernetes.Debugging; using Microsoft.ApplicationInsights.Kubernetes.Utilities; -using Microsoft.Extensions.Options; namespace Microsoft.Extensions.DependencyInjection { @@ -14,20 +13,16 @@ namespace Microsoft.Extensions.DependencyInjection public class KubernetesServiceCollectionBuilder : IKubernetesServiceCollectionBuilder { private readonly Func _isRunningInKubernetes; - private readonly AppInsightsForKubernetesOptions _options; private readonly ApplicationInsightsKubernetesDiagnosticSource _logger = ApplicationInsightsKubernetesDiagnosticSource.Instance; /// /// Construction for . /// /// A function that returns true when running inside Kubernetes. - /// public KubernetesServiceCollectionBuilder( - Func isRunningInKubernetes, - IOptions options) + Func isRunningInKubernetes) { _isRunningInKubernetes = isRunningInKubernetes ?? throw new ArgumentNullException(nameof(isRunningInKubernetes)); - _options = options?.Value ?? throw new ArgumentNullException(nameof(options)); } /// @@ -61,14 +56,7 @@ public IServiceCollection RegisterServices(IServiceCollection serviceCollection) private void RegisterPerformanceCounterTelemetryInitializer(IServiceCollection serviceCollection) { - if (!_options.DisablePerformanceCounters) - { - serviceCollection.AddSingleton(); - } - else - { - _logger.LogDebug("{initializerName} is disabled by configuration.", nameof(SimplePerformanceCounterTelemetryInitializer)); - } + serviceCollection.AddSingleton(); } private static void RegisterCommonServices(IServiceCollection serviceCollection) diff --git a/src/ApplicationInsights.Kubernetes/TelemetryInitializers/SimplePerformanceCounterTelemetryInitializer.cs b/src/ApplicationInsights.Kubernetes/TelemetryInitializers/SimplePerformanceCounterTelemetryInitializer.cs index 10009752..d4958cb2 100644 --- a/src/ApplicationInsights.Kubernetes/TelemetryInitializers/SimplePerformanceCounterTelemetryInitializer.cs +++ b/src/ApplicationInsights.Kubernetes/TelemetryInitializers/SimplePerformanceCounterTelemetryInitializer.cs @@ -3,6 +3,8 @@ using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using static Microsoft.ApplicationInsights.Kubernetes.TelemetryProperty; namespace Microsoft.ApplicationInsights.Kubernetes @@ -18,15 +20,25 @@ internal sealed class SimplePerformanceCounterTelemetryInitializer : ITelemetryI private Process _currnetProcess; private static readonly int ProcessorCount = Environment.ProcessorCount <= 0 ? 1 : Environment.ProcessorCount; + private readonly AppInsightsForKubernetesOptions _options; private readonly ITelemetryKeyCache _telemetryKeyCache; - public SimplePerformanceCounterTelemetryInitializer(ITelemetryKeyCache telemetryKeyCache) + public SimplePerformanceCounterTelemetryInitializer( + IOptions options, + ITelemetryKeyCache telemetryKeyCache) { + _options = options?.Value ?? throw new ArgumentNullException(nameof(options)); _telemetryKeyCache = telemetryKeyCache ?? throw new ArgumentNullException(nameof(telemetryKeyCache)); } public void Initialize(ITelemetry telemetry) { + // Short circuit. + if (_options.DisablePerformanceCounters) + { + return; + } + if (telemetry is ISupportMetrics metricsTelemetry) { // Write CPU metrics diff --git a/tests/UnitTests/KubernetesTestServiceCollectionBuilder.cs b/tests/UnitTests/KubernetesTestServiceCollectionBuilder.cs index 9a01268a..948ba192 100644 --- a/tests/UnitTests/KubernetesTestServiceCollectionBuilder.cs +++ b/tests/UnitTests/KubernetesTestServiceCollectionBuilder.cs @@ -7,7 +7,7 @@ namespace Microsoft.ApplicationInsights.Kubernetes internal class KubernetesTestServiceCollectionBuilder : KubernetesServiceCollectionBuilder { public KubernetesTestServiceCollectionBuilder() - : base(isRunningInKubernetes: () => true, Options.Create(new AppInsightsForKubernetesOptions())) + : base(isRunningInKubernetes: () => true) { }