Bring back DomainNameRoleInstanceTelemetryInitializer as there is no other initializer or base sdk which can populate roleinstance and nodename for Linux. Fix 671
This commit is contained in:
Родитель
ae979f0c80
Коммит
167b0daa93
|
@ -134,6 +134,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
{
|
||||
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
|
||||
services.AddSingleton<ITelemetryInitializer, ApplicationInsights.AspNetCore.TelemetryInitializers.DomainNameRoleInstanceTelemetryInitializer>();
|
||||
services.AddSingleton<ITelemetryInitializer, AzureWebAppRoleEnvironmentTelemetryInitializer>();
|
||||
services.AddSingleton<ITelemetryInitializer, ComponentVersionTelemetryInitializer>();
|
||||
services.AddSingleton<ITelemetryInitializer, ClientIpHeaderTelemetryInitializer>();
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
namespace Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers
|
||||
{
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Threading;
|
||||
using Channel;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using Microsoft.ApplicationInsights.Extensibility.Implementation;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
/// <summary>
|
||||
/// A telemetry initializer that populates cloud context role instance.
|
||||
/// </summary>
|
||||
public class DomainNameRoleInstanceTelemetryInitializer : TelemetryInitializerBase
|
||||
{
|
||||
private string roleInstanceName;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DomainNameRoleInstanceTelemetryInitializer" /> class.
|
||||
/// </summary>
|
||||
/// <param name="httpContextAccessor">HTTP context accessor.</param>
|
||||
public DomainNameRoleInstanceTelemetryInitializer(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes role instance name and node name with the host name.
|
||||
/// </summary>
|
||||
/// <param name="platformContext">Platform context.</param>
|
||||
/// <param name="requestTelemetry">Request telemetry.</param>
|
||||
/// <param name="telemetry">Telemetry item.</param>
|
||||
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
|
||||
{
|
||||
if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleInstance))
|
||||
{
|
||||
var name = LazyInitializer.EnsureInitialized(ref this.roleInstanceName, this.GetMachineName);
|
||||
telemetry.Context.Cloud.RoleInstance = name;
|
||||
}
|
||||
|
||||
InternalContext internalContext = telemetry.Context.GetInternalContext();
|
||||
if (string.IsNullOrEmpty(internalContext.NodeName))
|
||||
{
|
||||
var name = LazyInitializer.EnsureInitialized(ref this.roleInstanceName, this.GetMachineName);
|
||||
internalContext.NodeName = name;
|
||||
}
|
||||
}
|
||||
|
||||
private string GetMachineName()
|
||||
{
|
||||
string hostName = Dns.GetHostName();
|
||||
|
||||
// Issue #61: For dnxcore machine name does not have domain name like in full framework
|
||||
#if !NETSTANDARD1_6
|
||||
string domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName;
|
||||
if (!hostName.EndsWith(domainName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
hostName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", hostName, domainName);
|
||||
}
|
||||
#endif
|
||||
return hostName;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -52,6 +52,7 @@ namespace Microsoft.Extensions.DependencyInjection.Test
|
|||
public static class AddApplicationInsightsTelemetry
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(typeof(ITelemetryInitializer), typeof(ApplicationInsights.AspNetCore.TelemetryInitializers.DomainNameRoleInstanceTelemetryInitializer), ServiceLifetime.Singleton)]
|
||||
[InlineData(typeof(ITelemetryInitializer), typeof(AzureWebAppRoleEnvironmentTelemetryInitializer), ServiceLifetime.Singleton)]
|
||||
[InlineData(typeof(ITelemetryInitializer), typeof(ComponentVersionTelemetryInitializer), ServiceLifetime.Singleton)]
|
||||
[InlineData(typeof(ITelemetryInitializer), typeof(ClientIpHeaderTelemetryInitializer), ServiceLifetime.Singleton)]
|
||||
|
@ -69,6 +70,7 @@ namespace Microsoft.Extensions.DependencyInjection.Test
|
|||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(ITelemetryInitializer), typeof(ApplicationInsights.AspNetCore.TelemetryInitializers.DomainNameRoleInstanceTelemetryInitializer), ServiceLifetime.Singleton)]
|
||||
[InlineData(typeof(ITelemetryInitializer), typeof(AzureWebAppRoleEnvironmentTelemetryInitializer), ServiceLifetime.Singleton)]
|
||||
[InlineData(typeof(ITelemetryInitializer), typeof(ComponentVersionTelemetryInitializer), ServiceLifetime.Singleton)]
|
||||
[InlineData(typeof(ITelemetryInitializer), typeof(ClientIpHeaderTelemetryInitializer), ServiceLifetime.Singleton)]
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
namespace Microsoft.ApplicationInsights.AspNetCore.Tests.ContextInitializers
|
||||
{
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using Helpers;
|
||||
using Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using Microsoft.ApplicationInsights.Extensibility.Implementation;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Xunit;
|
||||
|
||||
public class DomainNameRoleInstanceTelemetryInitializerTests
|
||||
{
|
||||
private const string TestListenerName = "TestListener";
|
||||
|
||||
[Fact]
|
||||
public void InitializeThrowIfHttpContextAccessorIsNull()
|
||||
{
|
||||
Assert.ThrowsAny<ArgumentNullException>(() =>
|
||||
{
|
||||
var initializer = new DomainNameRoleInstanceTelemetryInitializer(null);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeDoesNotThrowIfHttpContextIsUnavailable()
|
||||
{
|
||||
var ac = new HttpContextAccessor() { HttpContext = null };
|
||||
|
||||
var initializer = new DomainNameRoleInstanceTelemetryInitializer(ac);
|
||||
|
||||
initializer.Initialize(new RequestTelemetry());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeDoesNotThrowIfRequestTelemetryIsUnavailable()
|
||||
{
|
||||
var ac = new HttpContextAccessor() { HttpContext = new DefaultHttpContext() };
|
||||
|
||||
var initializer = new DomainNameRoleInstanceTelemetryInitializer(ac);
|
||||
|
||||
initializer.Initialize(new RequestTelemetry());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RoleInstanceNameIsSetToDomainAndHost()
|
||||
{
|
||||
var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(new RequestTelemetry(), null);
|
||||
var source = new DomainNameRoleInstanceTelemetryInitializer(contextAccessor);
|
||||
var requestTelemetry = new RequestTelemetry();
|
||||
source.Initialize(requestTelemetry);
|
||||
|
||||
string hostName = Dns.GetHostName();
|
||||
|
||||
#if NET451 || NET46
|
||||
string domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName;
|
||||
if (hostName.EndsWith(domainName, StringComparison.OrdinalIgnoreCase) == false)
|
||||
{
|
||||
hostName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", hostName, domainName);
|
||||
}
|
||||
#endif
|
||||
|
||||
Assert.Equal(hostName, requestTelemetry.Context.Cloud.RoleInstance);
|
||||
Assert.Equal(hostName, requestTelemetry.Context.GetInternalContext().NodeName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ContextInitializerDoesNotOverrideMachineName()
|
||||
{
|
||||
var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(new RequestTelemetry(), null);
|
||||
var source = new DomainNameRoleInstanceTelemetryInitializer(contextAccessor);
|
||||
var requestTelemetry = new RequestTelemetry();
|
||||
requestTelemetry.Context.Cloud.RoleInstance = "Test";
|
||||
requestTelemetry.Context.GetInternalContext().NodeName = "Test1";
|
||||
|
||||
source.Initialize(requestTelemetry);
|
||||
|
||||
Assert.Equal("Test", requestTelemetry.Context.Cloud.RoleInstance);
|
||||
Assert.Equal("Test1", requestTelemetry.Context.GetInternalContext().NodeName);
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче