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

Commit

Permalink
changing to TelemetryConfiguration injection
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMothra committed Apr 12, 2018
1 parent 6084c4f commit 9533849
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class HostingDiagnosticListener : IApplicationInsightDiagnosticListener
public static bool IsAspNetCore20 = typeof(WebHostBuilder).GetTypeInfo().Assembly.GetName().Version.Major >= 2;

private readonly TelemetryClient client;
private readonly IApplicationIdProvider applicationIdProvider;
private readonly TelemetryConfiguration telemetryConfiguration;
private readonly string sdkVersion = SdkVersionUtils.GetVersion();
private const string ActivityCreatedByHostingDiagnosticListener = "ActivityCreatedByHostingDiagnosticListener";

Expand All @@ -38,10 +38,10 @@ internal class HostingDiagnosticListener : IApplicationInsightDiagnosticListener
/// </summary>
/// <param name="client"><see cref="TelemetryClient"/> to post traces to.</param>
/// <param name="applicationIdProvider">Nullable Provider for resolving application Id to be used by Correlation.</param>
public HostingDiagnosticListener(TelemetryClient client, IApplicationIdProvider applicationIdProvider)
public HostingDiagnosticListener(TelemetryClient client, TelemetryConfiguration telemetryConfiguration)
{
this.client = client ?? throw new ArgumentNullException(nameof(client));
this.applicationIdProvider = applicationIdProvider;
this.telemetryConfiguration = telemetryConfiguration ?? throw new ArgumentNullException(nameof(telemetryConfiguration));
}

/// <inheritdoc/>
Expand Down Expand Up @@ -237,7 +237,7 @@ private void SetAppIdInResponseHeader(HttpContext httpContext, RequestTelemetry
(!responseHeaders.ContainsKey(RequestResponseHeaders.RequestContextHeader) || HttpHeadersUtilities.ContainsRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey)))
{
string applicationId = null;
if (this.applicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false)
if (this.telemetryConfiguration.ApplicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false)
{
HttpHeadersUtilities.SetRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey, applicationId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
/// </summary>
internal class OperationCorrelationTelemetryInitializer : TelemetryInitializerBase
{
private readonly IApplicationIdProvider applicationIdProvider;
private readonly TelemetryConfiguration telemetryConfiguration;

/// <summary>
/// Initializes a new instance of the <see cref="OperationCorrelationTelemetryInitializer"/> class.
/// </summary>
/// <param name="httpContextAccessor">Accessor for retrieving the current HTTP context.</param>
/// <param name="applicationIdProvider">Nullable Provider for resolving application Id to be used by Correlation.</param>
public OperationCorrelationTelemetryInitializer(IHttpContextAccessor httpContextAccessor, IApplicationIdProvider applicationIdProvider) : base(httpContextAccessor)
public OperationCorrelationTelemetryInitializer(IHttpContextAccessor httpContextAccessor, TelemetryConfiguration telemetryConfiguration) : base(httpContextAccessor)
{
this.applicationIdProvider = applicationIdProvider;
this.telemetryConfiguration = telemetryConfiguration ?? throw new ArgumentNullException(nameof(telemetryConfiguration));
}

/// <summary>
Expand All @@ -47,7 +47,7 @@ protected override void OnInitializeTelemetry(HttpContext platformContext, Reque
{
requestTelemetry.Source = headerCorrelationId;
}
else if ((this.applicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false)
else if ((this.telemetryConfiguration.ApplicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false)
&& applicationId != headerCorrelationId)
{
requestTelemetry.Source = headerCorrelationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ExceptionTrackingMiddlewareTest
[Fact]
public void InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform()
{
var middleware = new HostingDiagnosticListener(CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry), CommonMocks.GetMockApplicationIdProvider());
var middleware = new HostingDiagnosticListener(CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry), CommonMocks.GetMockTelemetryConfiguration());

middleware.OnHostingException(null, null);

Expand All @@ -27,7 +27,7 @@ public void InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform()
[Fact]
public void SdkVersionIsPopulatedByMiddleware()
{
var middleware = new HostingDiagnosticListener(CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry), CommonMocks.GetMockApplicationIdProvider());
var middleware = new HostingDiagnosticListener(CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry), CommonMocks.GetMockTelemetryConfiguration());

middleware.OnHostingException(null, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,26 @@ public static class CommonMocks

public static TelemetryClient MockTelemetryClient(Action<ITelemetry> onSendCallback)
{
return new TelemetryClient(new TelemetryConfiguration()
return new TelemetryClient(GetMockTelemetryConfiguration(onSendCallback));
}

internal static TelemetryConfiguration GetMockTelemetryConfiguration()
{
return new TelemetryConfiguration()
{
InstrumentationKey = InstrumentationKey,
TelemetryChannel = new FakeTelemetryChannel { OnSend = onSendCallback }
});
ApplicationIdProvider = new MockApplicationIdProvider(InstrumentationKey, TestApplicationId)
};
}

internal static IApplicationIdProvider GetMockApplicationIdProvider()
internal static TelemetryConfiguration GetMockTelemetryConfiguration(Action<ITelemetry> onSendCallback)
{
return new MockApplicationIdProvider(InstrumentationKey, TestApplicationId);
return new TelemetryConfiguration()
{
InstrumentationKey = InstrumentationKey,
TelemetryChannel = new FakeTelemetryChannel { OnSend = onSendCallback },
ApplicationIdProvider = new MockApplicationIdProvider(InstrumentationKey, TestApplicationId)
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private HttpContext CreateContext(string scheme, HostString host, PathString? pa

public RequestTrackingMiddlewareTest()
{
this.middleware = new HostingDiagnosticListener(CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)), CommonMocks.GetMockApplicationIdProvider());
this.middleware = new HostingDiagnosticListener(CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)), CommonMocks.GetMockTelemetryConfiguration());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static OperationCorrelationTelemetryInitializer CreateInitializer(HttpCo

private static OperationCorrelationTelemetryInitializer CreateInitializer(IHttpContextAccessor contextAccessor)
{
return new OperationCorrelationTelemetryInitializer(contextAccessor, CommonMocks.GetMockApplicationIdProvider());
return new OperationCorrelationTelemetryInitializer(contextAccessor, CommonMocks.GetMockTelemetryConfiguration());
}

[Fact]
Expand Down

0 comments on commit 9533849

Please sign in to comment.