From 31d7735997fa0d8d8f4dc41fe43381f5f6f00cb0 Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Thu, 16 May 2024 05:41:27 +0300 Subject: [PATCH] Resolving `TelemetryClient` in `ActivitySourceDependencyCollector` should be on need (#252) `IHostedService` implementations may be invoked before the `TelemetryClient` is available. This change makes it possible to use `ActivitySourceDependencyCollector` in such scenarios instead of crashing. --- .../ActivitySourceDependencyCollector.cs | 10 +++++++--- .../IServiceCollectionExtensions.cs | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Tingle.AspNetCore.ApplicationInsights/ActivitySourceDependencyCollector.cs b/src/Tingle.AspNetCore.ApplicationInsights/ActivitySourceDependencyCollector.cs index aa4c116..c4e1b49 100644 --- a/src/Tingle.AspNetCore.ApplicationInsights/ActivitySourceDependencyCollector.cs +++ b/src/Tingle.AspNetCore.ApplicationInsights/ActivitySourceDependencyCollector.cs @@ -1,5 +1,6 @@ using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System.Diagnostics; @@ -8,13 +9,13 @@ namespace Tingle.AspNetCore.ApplicationInsights; // See https://github.com/microsoft/ApplicationInsights-dotnet/issues/1427 internal class ActivitySourceDependencyCollector : IHostedService { - private readonly TelemetryClient client; + private readonly IServiceProvider serviceProvider; private readonly IDictionary activities; private readonly ActivityListener? listener; - public ActivitySourceDependencyCollector(TelemetryClient client, IDictionary activities) + public ActivitySourceDependencyCollector(IServiceProvider serviceProvider, IDictionary activities) { - this.client = client ?? throw new ArgumentNullException(nameof(client)); + this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); this.activities = activities ?? throw new ArgumentNullException(nameof(activities)); if (activities.Count > 0) @@ -34,6 +35,9 @@ public ActivitySourceDependencyCollector(TelemetryClient client, IDictionary(); + if (client is null) return; + // extensibility point - can chain more telemetry extraction methods here var telemetry = ExtractDependencyTelemetry(activity); if (telemetry == null) diff --git a/src/Tingle.AspNetCore.ApplicationInsights/IServiceCollectionExtensions.cs b/src/Tingle.AspNetCore.ApplicationInsights/IServiceCollectionExtensions.cs index 8d8aef4..ca5f247 100644 --- a/src/Tingle.AspNetCore.ApplicationInsights/IServiceCollectionExtensions.cs +++ b/src/Tingle.AspNetCore.ApplicationInsights/IServiceCollectionExtensions.cs @@ -62,6 +62,6 @@ public static IServiceCollection AddActivitySourceDependencyCollector(this IServ public static IServiceCollection AddActivitySourceDependencyCollector(this IServiceCollection services, IDictionary activities) { - return services.AddHostedService(p => ActivatorUtilities.CreateInstance(p, [activities])); + return services.AddHostedService(p => new ActivitySourceDependencyCollector(p, activities)); } }