Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Ability to add telemetry processors through DI #445

Merged
merged 2 commits into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,6 +1,7 @@
namespace Microsoft.Extensions.DependencyInjection
{
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.ApplicationInsights.Channel;
Expand All @@ -21,6 +22,7 @@ internal class TelemetryConfigurationOptionsSetup : IConfigureOptions<TelemetryC
private readonly IEnumerable<ITelemetryInitializer> initializers;
private readonly IEnumerable<ITelemetryModule> modules;
private readonly ITelemetryChannel telemetryChannel;
private readonly IEnumerable<Func<ITelemetryProcessor, ITelemetryProcessor>> telemetryProcessorFactories;

/// <summary>
/// Initializes a new instance of the <see cref="T:TelemetryConfigurationOptionsSetup"/> class.
Expand All @@ -29,11 +31,13 @@ public TelemetryConfigurationOptionsSetup(
IServiceProvider serviceProvider,
IOptions<ApplicationInsightsServiceOptions> applicationInsightsServiceOptions,
IEnumerable<ITelemetryInitializer> initializers,
IEnumerable<ITelemetryModule> modules)
IEnumerable<ITelemetryModule> modules,
IEnumerable<Func<ITelemetryProcessor, ITelemetryProcessor>> telemetryProcessorFactories)
{
this.applicationInsightsServiceOptions = applicationInsightsServiceOptions.Value;
this.initializers = initializers;
this.modules = modules;
this.telemetryProcessorFactories = telemetryProcessorFactories;
this.telemetryChannel = serviceProvider.GetService<ITelemetryChannel>();
}

Expand All @@ -45,6 +49,15 @@ public void Configure(TelemetryConfiguration configuration)
configuration.InstrumentationKey = this.applicationInsightsServiceOptions.InstrumentationKey;
}

if (this.telemetryProcessorFactories.Count() > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use this.telemetryProcessorFactories.Any() (it short-circuits the enumeration once it hits the first item)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, perhaps this if statement is unnecessary. If the imported telemetryProcessorFactories is empty, then the foreach does nothing, but I think we should still call .Build() for the case where users have manually added processors to the TelemetryProcessorChainBuilder. Someone needs to call .Build() and this might be the right place.

{
foreach (Func<ITelemetryProcessor, ITelemetryProcessor> processorFactory in this.telemetryProcessorFactories)
{
configuration.TelemetryProcessorChainBuilder.Use(processorFactory);
}
configuration.TelemetryProcessorChainBuilder.Build();
}

this.AddTelemetryChannelAndProcessorsForFullFramework(configuration);

configuration.TelemetryChannel = this.telemetryChannel ?? configuration.TelemetryChannel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,19 @@ public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWi
Assert.True(dependencyModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Count > 0);
}

[Fact]
public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithTelemetryProcessorFactoriesFromContainer()
{
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
services.AddSingleton<Func<ITelemetryProcessor, ITelemetryProcessor>>((next) => new FakeTelemetryProcessor(next));

services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());

IServiceProvider serviceProvider = services.BuildServiceProvider();
var telemetryConfiguration = serviceProvider.GetTelemetryConfiguration();
Assert.Contains(telemetryConfiguration.TelemetryProcessors, (processor) => processor is FakeTelemetryProcessor);
}

#if NET451
[Fact]
public static void AddsAddaptiveSamplingServiceToTheConfigurationInFullFrameworkByDefault()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Microsoft.ApplicationInsights.AspNetCore.Tests
{
using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;

internal class FakeTelemetryProcessor : ITelemetryProcessor
{
private readonly ITelemetryProcessor next;

public FakeTelemetryProcessor(ITelemetryProcessor next)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}

this.next = next;
}

public void Process(ITelemetry item)
{
this.next.Process(item);
}
}
}