1
0
Форкнуть 0

Merge pull request #7 from Microsoft/abaranch/operationid

Add OperationNameTelelemtryInitializer
This commit is contained in:
Anastasia Baranchenkova 2015-03-31 15:06:46 -07:00
Родитель 31f4301bfd 17aefc235f
Коммит 30086e8e27
6 изменённых файлов: 146 добавлений и 6 удалений

Просмотреть файл

@ -51,6 +51,7 @@
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebClientIpHeaderTelemetryInitializer(svcs));
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebUserAgentTelemetryInitializer(svcs));
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebOperationNameTelemetryInitializer(svcs));
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebOperationIdTelemetryInitializer(svcs));
return new TelemetryClient();
});

Просмотреть файл

@ -11,22 +11,27 @@
public abstract class TelemetryInitializerBase : ITelemetryInitializer
{
IServiceProvider serviceProvider;
private IServiceProvider serviceProvider;
public TelemetryInitializerBase(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException("serviceProvider");
}
this.serviceProvider = serviceProvider;
if (this.serviceProvider == null)
{
// TODO: Diagnostics
}
}
public void Initialize(ITelemetry telemetry)
{
try
{
if (serviceProvider == null)
{
return;
}
var contextHolder = this.serviceProvider.GetService<HttpContextHolder>();
if (contextHolder == null)

Просмотреть файл

@ -0,0 +1,21 @@
namespace Microsoft.ApplicationInsights.AspNet.TelemetryInitializers
{
using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.AspNet.Http;
public class WebOperationIdTelemetryInitializer : TelemetryInitializerBase
{
public WebOperationIdTelemetryInitializer(IServiceProvider serviceProvider) : base(serviceProvider)
{ }
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{
if (string.IsNullOrEmpty(telemetry.Context.Operation.Id))
{
telemetry.Context.Operation.Id = requestTelemetry.Id;
}
}
}
}

Просмотреть файл

@ -105,6 +105,30 @@
}
}
[Fact]
public void AddTelemetryWillAddOperationIdTelelemtryInitializerInConfigurationActive()
{
try
{
var serviceCollection = HostingServices.Create(null);
IConfiguration config = new Configuration().AddJsonFile("content\\config.json");
serviceCollection.AddApplicationInsightsTelemetry(config);
var serviceProvider = serviceCollection.BuildServiceProvider();
var items = TelemetryConfiguration.Active.TelemetryInitializers
.Select(i => i is WebOperationIdTelemetryInitializer)
.ToList();
Assert.Equal(1, items.Count);
}
finally
{
CleanActiveConfiguration();
}
}
[Fact]
public void JSSnippetWillNotThrowWithoutInstrumentationKey()
{

Просмотреть файл

@ -0,0 +1,73 @@
namespace Microsoft.ApplicationInsights.AspNet.Tests.TelemetryInitializers
{
using Microsoft.ApplicationInsights.AspNet.Implementation;
using Microsoft.ApplicationInsights.AspNet.TelemetryInitializers;
using Microsoft.ApplicationInsights.AspNet.Tests.Helpers;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.AspNet.Http.Core;
using System;
using System.Collections.Generic;
using Xunit;
public class WebOperationIdTelemetryInitializerTest
{
[Fact]
public void InitializeDoesNotThrowIfHttpContextHolderIsUnavailable()
{
var initializer = new WebOperationIdTelemetryInitializer(new TestServiceProvider());
initializer.Initialize(new RequestTelemetry());
}
[Fact]
public void InitializeDoesNotThrowIfHttpContextIsUnavailable()
{
var serviceProvider = new TestServiceProvider(new List<object>() { new HttpContextHolder() });
var initializer = new WebOperationIdTelemetryInitializer(serviceProvider);
initializer.Initialize(new RequestTelemetry());
}
[Fact]
public void InitializeDoesNotThrowIfRequestTelemetryIsUnavailable()
{
var contextHolder = new HttpContextHolder();
contextHolder.Context = new DefaultHttpContext();
var serviceProvider = new TestServiceProvider(new List<object>() { contextHolder });
var initializer = new WebOperationIdTelemetryInitializer(serviceProvider);
initializer.Initialize(new RequestTelemetry());
}
[Fact]
public void InitializeDoesNotOverrideOperationIdProvidedInline()
{
var telemetry = new EventTelemetry();
telemetry.Context.Operation.Id = "123";
var requestTelemetry = new RequestTelemetry();
var contextHolder = new HttpContextHolder() { Context = new DefaultHttpContext() };
var serviceProvider = new TestServiceProvider(new List<object>() { contextHolder, requestTelemetry });
var initializer = new WebOperationIdTelemetryInitializer(serviceProvider);
initializer.Initialize(telemetry);
Assert.Equal("123", telemetry.Context.Operation.Id);
}
[Fact]
public void InitializeSetsTelemetryOperationIdToRequestId()
{
var telemetry = new EventTelemetry();
var requestTelemetry = new RequestTelemetry();
var contextHolder = new HttpContextHolder() { Context = new DefaultHttpContext() };
var serviceProvider = new TestServiceProvider(new List<object>() { contextHolder, requestTelemetry });
var initializer = new WebOperationIdTelemetryInitializer(serviceProvider);
initializer.Initialize(telemetry);
Assert.Equal(requestTelemetry.Id, telemetry.Context.Operation.Id);
}
}
}

Просмотреть файл

@ -38,6 +38,22 @@
initializer.Initialize(new RequestTelemetry());
}
[Fact]
public void InitializeDoesNotOverrideOperationNameProvidedInline()
{
var telemetry = new EventTelemetry();
telemetry.Context.Operation.Name = "Name";
var contextHolder = new HttpContextHolder() { Context = new DefaultHttpContext() };
var serviceProvider = new TestServiceProvider(new List<object>() { contextHolder, new RequestTelemetry() });
var initializer = new WebOperationNameTelemetryInitializer(serviceProvider);
initializer.Initialize(telemetry);
Assert.Equal("Name", telemetry.Context.Operation.Name);
}
[Fact]
public void InitializeSetsTelemetryOperationNameToMethodAndPath()
{