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

remove correlationIdLookupHelper. #636

Merged
merged 18 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from 17 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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">Nullable Provider for resolving application Id to be used by Correlation.</param>
public HostingDiagnosticListener(TelemetryClient client, IApplicationIdProvider applicationIdProvider = null)
{
this.client = client;
this.correlationIdLookupHelper = correlationIdLookupHelper;
this.sdkVersion = SdkVersionUtils.VersionPrefix + SdkVersionUtils.GetAssemblyVersion();
this.client = client ?? throw new ArgumentNullException(nameof(client));
this.applicationIdProvider = applicationIdProvider;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -236,10 +236,10 @@ 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))
string applicationId = null;
if (this.applicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false)
{
HttpHeadersUtilities.SetRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey, correlationId);
HttpHeadersUtilities.SetRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey, applicationId);
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ExceptionTrackingMiddleware(RequestDelegate next, TelemetryClient client)
{
this.next = next;
this.telemetryClient = client;
this.sdkVersion = SdkVersionUtils.VersionPrefix + SdkVersionUtils.GetAssemblyVersion();
this.sdkVersion = SdkVersionUtils.GetVersion();
}

public async Task Invoke(HttpContext httpContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Microsoft.Extensions.DependencyInjection
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DependencyCollector;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId;
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;
using Microsoft.ApplicationInsights.WindowsServer;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
Expand Down Expand Up @@ -168,7 +169,7 @@ public static IServiceCollection AddApplicationInsightsTelemetry(this IServiceCo
services.AddSingleton<ITelemetryModule, QuickPulseTelemetryModule>();
services.AddSingleton<TelemetryConfiguration>(provider => provider.GetService<IOptions<TelemetryConfiguration>>().Value);

services.AddSingleton<ICorrelationIdLookupHelper>(provider => new CorrelationIdLookupHelper(() => provider.GetService<IOptions<TelemetryConfiguration>>().Value));
services.TryAddSingleton<IApplicationIdProvider, ApplicationInsightsApplicationIdProvider>();

services.AddSingleton<TelemetryClient>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal class TelemetryConfigurationOptionsSetup : IConfigureOptions<TelemetryC
private readonly ITelemetryChannel telemetryChannel;
private readonly IEnumerable<ITelemetryProcessorFactory> telemetryProcessorFactories;
private readonly IEnumerable<ITelemetryModuleConfigurator> telemetryModuleConfigurators;
private readonly IApplicationIdProvider applicationIdProvider;

/// <summary>
/// Initializes a new instance of the <see cref="T:TelemetryConfigurationOptionsSetup"/> class.
Expand All @@ -44,6 +45,7 @@ public TelemetryConfigurationOptionsSetup(
this.telemetryProcessorFactories = telemetryProcessorFactories;
this.telemetryModuleConfigurators = telemetryModuleConfigurators;
this.telemetryChannel = serviceProvider.GetService<ITelemetryChannel>();
this.applicationIdProvider = serviceProvider.GetService<IApplicationIdProvider>();
}

/// <inheritdoc />
Expand Down Expand Up @@ -116,6 +118,9 @@ public void Configure(TelemetryConfiguration configuration)
module.Initialize(configuration);
}
}

// Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule depends on this nullable configuration to support Correlation.
configuration.ApplicationIdProvider = this.applicationIdProvider;
}

private void AddQuickPulse(TelemetryConfiguration configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class ApplicationInsightsLogger : ILogger
private readonly TelemetryClient telemetryClient;
private readonly Func<string, LogLevel, bool> filter;
private readonly ApplicationInsightsLoggerOptions options;
private readonly string sdkVersion;
private readonly string sdkVersion = SdkVersionUtils.GetVersion();

/// <summary>
/// Creates a new instance of <see cref="ApplicationInsightsLogger"/>
Expand All @@ -29,7 +29,6 @@ public ApplicationInsightsLogger(string name, TelemetryClient telemetryClient, F
this.telemetryClient = telemetryClient;
this.filter = filter;
this.options = options;
this.sdkVersion = SdkVersionUtils.VersionPrefix + SdkVersionUtils.GetAssemblyVersion();
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>Application Insights for ASP.NET Core web applications. See https://azure.microsoft.com/documentation/articles/app-insights-asp-net-five/ for more information. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156</Description>
<Copyright>Copyright © Microsoft. All Rights Reserved.</Copyright>
<AssemblyTitle>Application Insights for ASP.NET Core Web Applications</AssemblyTitle>
<VersionPrefix>2.2.1</VersionPrefix>
<VersionPrefix>2.3.0-beta1</VersionPrefix>
<Authors>Microsoft</Authors>
<TargetFrameworks>net451;net46;netstandard1.6</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard1.6</TargetFrameworks>
Expand Down Expand Up @@ -72,17 +72,15 @@
<PackageReference Include="System.Threading.Tasks.Analyzers" Version="1.2.0-beta2">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.6.0-beta2" />
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.6.0-beta2-build14769" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.6.0-beta2-build14769" />
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer" Version="2.6.0-beta2-build14769" />
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.6.0-beta2" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.6.0-beta3" />
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.6.0-beta3" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.6.0-beta3" />
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer" Version="2.6.0-beta3" />
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.6.0-beta3" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net451' OR '$(TargetFramework)' == 'net46' ">
<PackageReference Include="System.Net.Http" Version="4.3.1" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.6.0-beta2-build14769" />
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.6.0-beta2" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
Expand Down
10 changes: 9 additions & 1 deletion src/Microsoft.ApplicationInsights.AspNetCore/SdkVersionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ internal class SdkVersionUtils
public const string VersionPrefix = "aspnet5c:";
#endif

internal static string GetAssemblyVersion()
/// <summary>
/// Get the Assembly Version with SDK prefix.
/// </summary>
internal static string GetVersion()
{
return VersionPrefix + GetAssemblyVersion();
}

private static string GetAssemblyVersion()
{
return typeof(SdkVersionUtils).GetTypeInfo().Assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.First()
Expand Down
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">Nullable Provider for resolving application Id to be used by Correlation.</param>
public OperationCorrelationTelemetryInitializer(IHttpContextAccessor httpContextAccessor, IApplicationIdProvider applicationIdProvider = null) : base(httpContextAccessor)
{
if (correlationIdLookupHelper == null)
{
throw new ArgumentNullException(nameof(correlationIdLookupHelper));
}
this.correlationIdLookupHelper = correlationIdLookupHelper;
this.applicationIdProvider = applicationIdProvider;
}

/// <summary>
Expand All @@ -38,17 +31,14 @@ 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;
string applicationId = 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))
{
Expand All @@ -57,8 +47,8 @@ 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 applicationId) ?? false)
&& applicationId != headerCorrelationId)
{
requestTelemetry.Source = headerCorrelationId;
}
Expand Down
Loading