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

Resolving TelemetryClient in ActivitySourceDependencyCollector should be on need #252

Merged
merged 1 commit into from
May 16, 2024
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,6 @@
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Diagnostics;

Expand All @@ -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<string, ActivitySamplingResult> activities;
private readonly ActivityListener? listener;

public ActivitySourceDependencyCollector(TelemetryClient client, IDictionary<string, ActivitySamplingResult> activities)
public ActivitySourceDependencyCollector(IServiceProvider serviceProvider, IDictionary<string, ActivitySamplingResult> 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)
Expand All @@ -34,6 +35,9 @@ public ActivitySourceDependencyCollector(TelemetryClient client, IDictionary<str

internal void ActivityStopped(Activity activity)
{
var client = serviceProvider.GetService<TelemetryClient>();
if (client is null) return;

// extensibility point - can chain more telemetry extraction methods here
var telemetry = ExtractDependencyTelemetry(activity);
if (telemetry == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ public static IServiceCollection AddActivitySourceDependencyCollector(this IServ
public static IServiceCollection AddActivitySourceDependencyCollector(this IServiceCollection services,
IDictionary<string, ActivitySamplingResult> activities)
{
return services.AddHostedService(p => ActivatorUtilities.CreateInstance<ActivitySourceDependencyCollector>(p, [activities]));
return services.AddHostedService(p => new ActivitySourceDependencyCollector(p, activities));
}
}