Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid building pre-mature service provider #237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Microsoft.ApplicationInsights.Kubernetes.Debugging
Expand All @@ -13,7 +12,7 @@ public sealed class KubernetesDebuggingServiceCollectionBuilder : KubernetesServ
/// Constructor for <see cref="KubernetesDebuggingServiceCollectionBuilder"/>.
/// </summary>
public KubernetesDebuggingServiceCollectionBuilder(IOptions<AppInsightsForKubernetesOptions> options)
: base(isRunningInKubernetes: () => true, options) { }
: base(isRunningInKubernetes: () => true) { }

/// <summary>
/// Registers setttings provider for querying K8s proxy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ private static void BuildK8sServiceCollection(
IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null)
{
detectKubernetes ??= IsRunningInKubernetes;
using ServiceProvider provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<AppInsightsForKubernetesOptions>>();
kubernetesServiceCollectionBuilder ??= new KubernetesServiceCollectionBuilder(detectKubernetes, options);
kubernetesServiceCollectionBuilder ??= new KubernetesServiceCollectionBuilder(detectKubernetes);
kubernetesServiceCollectionBuilder.RegisterServices(services);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -14,20 +13,16 @@ namespace Microsoft.Extensions.DependencyInjection
public class KubernetesServiceCollectionBuilder : IKubernetesServiceCollectionBuilder
{
private readonly Func<bool> _isRunningInKubernetes;
private readonly AppInsightsForKubernetesOptions _options;
private readonly ApplicationInsightsKubernetesDiagnosticSource _logger = ApplicationInsightsKubernetesDiagnosticSource.Instance;

/// <summary>
/// Construction for <see cref="KubernetesServiceCollectionBuilder"/>.
/// </summary>
/// <param name="isRunningInKubernetes">A function that returns true when running inside Kubernetes.</param>
/// <param name="options"></param>
public KubernetesServiceCollectionBuilder(
Func<bool> isRunningInKubernetes,
IOptions<AppInsightsForKubernetesOptions> options)
Func<bool> isRunningInKubernetes)
{
_isRunningInKubernetes = isRunningInKubernetes ?? throw new ArgumentNullException(nameof(isRunningInKubernetes));
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
}

/// <summary>
Expand Down Expand Up @@ -61,14 +56,7 @@ public IServiceCollection RegisterServices(IServiceCollection serviceCollection)

private void RegisterPerformanceCounterTelemetryInitializer(IServiceCollection serviceCollection)
{
if (!_options.DisablePerformanceCounters)
{
serviceCollection.AddSingleton<ITelemetryInitializer, SimplePerformanceCounterTelemetryInitializer>();
}
else
{
_logger.LogDebug("{initializerName} is disabled by configuration.", nameof(SimplePerformanceCounterTelemetryInitializer));
}
serviceCollection.AddSingleton<ITelemetryInitializer, SimplePerformanceCounterTelemetryInitializer>();
}

private static void RegisterCommonServices(IServiceCollection serviceCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<AppInsightsForKubernetesOptions> 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
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTests/KubernetesTestServiceCollectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.ApplicationInsights.Kubernetes
internal class KubernetesTestServiceCollectionBuilder : KubernetesServiceCollectionBuilder
{
public KubernetesTestServiceCollectionBuilder()
: base(isRunningInKubernetes: () => true, Options.Create(new AppInsightsForKubernetesOptions()))
: base(isRunningInKubernetes: () => true)
{
}

Expand Down