-
Notifications
You must be signed in to change notification settings - Fork 123
remove correlationIdLookupHelper. #636
Changes from 1 commit
5a4bbfb
411e0a4
1c2fe70
05c0b27
bb4620c
09c1283
6084c4f
9533849
3c218f2
fd1cdc8
6244f9c
6e2c1c4
b3ad343
dc4edfc
92f8c14
e1ff218
3eaab02
554705d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
using Microsoft.ApplicationInsights.AspNetCore.Common; | ||
using Microsoft.ApplicationInsights.AspNetCore.Extensions; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.ApplicationInsights.Extensibility.Implementation; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
|
@@ -28,20 +29,19 @@ internal class HostingDiagnosticListener : IApplicationInsightDiagnosticListener | |
public static bool IsAspNetCore20 = typeof(WebHostBuilder).GetTypeInfo().Assembly.GetName().Version.Major >= 2; | ||
|
||
private readonly TelemetryClient client; | ||
private readonly ICorrelationIdLookupHelper correlationIdLookupHelper; | ||
private readonly string sdkVersion; | ||
private readonly IApplicationIdProvider applicationIdProvider; | ||
private readonly string sdkVersion = SdkVersionUtils.GetVersion(); | ||
private const string ActivityCreatedByHostingDiagnosticListener = "ActivityCreatedByHostingDiagnosticListener"; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:HostingDiagnosticListener"/> class. | ||
/// </summary> | ||
/// <param name="client"><see cref="TelemetryClient"/> to post traces to.</param> | ||
/// <param name="correlationIdLookupHelper">A store for correlation ids that we don't have to query it everytime.</param> | ||
public HostingDiagnosticListener(TelemetryClient client, ICorrelationIdLookupHelper correlationIdLookupHelper) | ||
/// <param name="applicationIdProvider">Provider for resolving application Id to be used by Correlation.</param> | ||
public HostingDiagnosticListener(TelemetryClient client, IApplicationIdProvider applicationIdProvider) | ||
{ | ||
this.client = client; | ||
this.correlationIdLookupHelper = correlationIdLookupHelper; | ||
this.sdkVersion = SdkVersionUtils.VersionPrefix + SdkVersionUtils.GetAssemblyVersion(); | ||
this.client = client ?? throw new ArgumentNullException(nameof(client)); | ||
this.applicationIdProvider = applicationIdProvider ?? throw new ArgumentNullException(nameof(applicationIdProvider)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets NOT do this check, and if provider is null, don't add correlation headers. |
||
} | ||
|
||
/// <inheritdoc/> | ||
|
@@ -236,10 +236,9 @@ private void SetAppIdInResponseHeader(HttpContext httpContext, RequestTelemetry | |
!string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) && | ||
(!responseHeaders.ContainsKey(RequestResponseHeaders.RequestContextHeader) || HttpHeadersUtilities.ContainsRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey))) | ||
{ | ||
string correlationId = null; | ||
if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(requestTelemetry.Context.InstrumentationKey, out correlationId)) | ||
if (this.applicationIdProvider.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out string applicationId)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do this only if applicationIdprovider is NOT null. |
||
{ | ||
HttpHeadersUtilities.SetRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey, correlationId); | ||
HttpHeadersUtilities.SetRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey, applicationId); | ||
} | ||
} | ||
} | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ namespace Microsoft.Extensions.DependencyInjection | |
using Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers; | ||
using Microsoft.ApplicationInsights.DependencyCollector; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId; | ||
using Microsoft.ApplicationInsights.WindowsServer; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
|
@@ -165,7 +166,7 @@ public static IServiceCollection AddApplicationInsightsTelemetry(this IServiceCo | |
services.AddSingleton<ITelemetryModule, AzureInstanceMetadataTelemetryModule>(); | ||
services.AddSingleton<TelemetryConfiguration>(provider => provider.GetService<IOptions<TelemetryConfiguration>>().Value); | ||
|
||
services.AddSingleton<ICorrelationIdLookupHelper>(provider => new CorrelationIdLookupHelper(() => provider.GetService<IOptions<TelemetryConfiguration>>().Value)); | ||
services.AddSingleton<IApplicationIdProvider>(provider => new ApplicationInsightsApplicationIdProvider()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be great to document how one can override this provider with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use this overload |
||
|
||
services.AddSingleton<TelemetryClient>(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,28 @@ | ||
namespace Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers | ||
{ | ||
using System; | ||
using System.Net.Http.Headers; | ||
using Microsoft.ApplicationInsights.AspNetCore.Common; | ||
using Microsoft.ApplicationInsights.AspNetCore.DiagnosticListeners; | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.Extensibility.Implementation; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
/// <summary> | ||
/// A telemetry initializer that will set the correlation context for all telemetry items in web application. | ||
/// </summary> | ||
internal class OperationCorrelationTelemetryInitializer : TelemetryInitializerBase | ||
{ | ||
private ICorrelationIdLookupHelper correlationIdLookupHelper = null; | ||
private readonly IApplicationIdProvider applicationIdProvider; | ||
|
||
/// <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="correlationIdLookupHelper">A store for correlation ids that we don't have to query it everytime.</param> | ||
public OperationCorrelationTelemetryInitializer( | ||
IHttpContextAccessor httpContextAccessor, ICorrelationIdLookupHelper correlationIdLookupHelper) : base(httpContextAccessor) | ||
/// <param name="applicationIdProvider">Provider for resolving application Id to be used by Correlation.</param> | ||
public OperationCorrelationTelemetryInitializer(IHttpContextAccessor httpContextAccessor, IApplicationIdProvider applicationIdProvider) : base(httpContextAccessor) | ||
{ | ||
if (correlationIdLookupHelper == null) | ||
{ | ||
throw new ArgumentNullException(nameof(correlationIdLookupHelper)); | ||
} | ||
this.correlationIdLookupHelper = correlationIdLookupHelper; | ||
this.applicationIdProvider = applicationIdProvider ?? throw new ArgumentNullException(nameof(applicationIdProvider)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we avoid this exception logic here and instead check if appidprovider is null in Initialize? |
||
} | ||
|
||
/// <summary> | ||
|
@@ -38,17 +31,13 @@ public OperationCorrelationTelemetryInitializer( | |
/// <param name="platformContext">Http context</param> | ||
/// <param name="requestTelemetry">Request telemetry object associated with the current request.</param> | ||
/// <param name="telemetry">Telemetry item to initialize.</param> | ||
protected override void OnInitializeTelemetry( | ||
HttpContext platformContext, | ||
RequestTelemetry requestTelemetry, | ||
ITelemetry telemetry) | ||
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry) | ||
{ | ||
HttpRequest currentRequest = platformContext.Request; | ||
if (currentRequest?.Headers != null && string.IsNullOrEmpty(requestTelemetry.Source)) | ||
{ | ||
string headerCorrelationId = HttpHeadersUtilities.GetRequestContextKeyValue(currentRequest.Headers, RequestResponseHeaders.RequestContextSourceKey); | ||
string headerCorrelationId = HttpHeadersUtilities.GetRequestContextKeyValue(currentRequest.Headers, RequestResponseHeaders.RequestContextSourceKey); | ||
|
||
string appCorrelationId = null; | ||
// If the source header is present on the incoming request, and it is an external component (not the same ikey as the one used by the current component), populate the source field. | ||
if (!string.IsNullOrEmpty(headerCorrelationId)) | ||
{ | ||
|
@@ -57,8 +46,7 @@ protected override void OnInitializeTelemetry( | |
{ | ||
requestTelemetry.Source = headerCorrelationId; | ||
} | ||
else if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(requestTelemetry.Context.InstrumentationKey, out appCorrelationId) && | ||
appCorrelationId != headerCorrelationId) | ||
else if (this.applicationIdProvider.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out string applicationId) && applicationId != headerCorrelationId) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only if provider is not null |
||
{ | ||
requestTelemetry.Source = headerCorrelationId; | ||
} | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inject TelemetryConfiguration here, and obtain appid provider from the Configuration, so that even if there is no AppId provider(occurs if user removes it from DI), this wouldn't result in exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cijothomas Are you sure we want to do that?
My thought is that only the NetFramework Modules will be expecting a TelemetryConfiguration.
All of your NetCore Modules are using DependencyInjection, and it would make sense to keep that consistent.
As an alternative; I can make this parameter null by default or make a second ctor without the IApplicationIdProvider parameter, to support DependencyInjection.
I assume consistency would be prefered, but I will defer to your judgement here :)
If I make changes here, would you like me to make the same changes in
OperationCorrelationTelemetryInitializer
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me it’s about somebody forgot to add it, not somebody removed it. In some custom scenarios.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SergeyKanzhelev, But the TelemetryConfiguration is also being constructed by DependencyInjection.
Maybe I'm not understanding a user custom config scenario. :)
Would something like this be an appropriate alternative...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cijothomas explained this to me offline. I'm cleaning this up now.
Thanks