Add WebOperationNameTelemetryInitialzer
This commit is contained in:
Родитель
5e93ae73c4
Коммит
31f4301bfd
|
@ -50,6 +50,8 @@
|
|||
services.AddSingleton<TelemetryClient>((svcs) => {
|
||||
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebClientIpHeaderTelemetryInitializer(svcs));
|
||||
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebUserAgentTelemetryInitializer(svcs));
|
||||
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebOperationNameTelemetryInitializer(svcs));
|
||||
|
||||
return new TelemetryClient();
|
||||
});
|
||||
|
||||
|
|
|
@ -48,7 +48,6 @@
|
|||
sw.Stop();
|
||||
|
||||
var telemetry = this.serviceProvider.GetService<RequestTelemetry>();
|
||||
telemetry.Name = httpContext.Request.Method + " " + httpContext.Request.Path.Value;
|
||||
telemetry.Timestamp = now;
|
||||
telemetry.Duration = sw.Elapsed;
|
||||
telemetry.ResponseCode = httpContext.Response.StatusCode.ToString();
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
namespace Microsoft.ApplicationInsights.AspNet.TelemetryInitializers
|
||||
{
|
||||
using System;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using Microsoft.AspNet.Http;
|
||||
|
||||
public class WebOperationNameTelemetryInitializer : TelemetryInitializerBase
|
||||
{
|
||||
public WebOperationNameTelemetryInitializer(IServiceProvider serviceProvider) : base(serviceProvider)
|
||||
{ }
|
||||
|
||||
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
|
||||
{
|
||||
if (string.IsNullOrEmpty(telemetry.Context.Operation.Name))
|
||||
{
|
||||
var name = platformContext.Request.Method + " " + platformContext.Request.Path.Value; // Test potential dangerous request;
|
||||
|
||||
var telemetryType = telemetry as RequestTelemetry;
|
||||
if (telemetryType != null && string.IsNullOrEmpty(telemetryType.Name))
|
||||
{
|
||||
telemetryType.Name = name;
|
||||
}
|
||||
|
||||
telemetry.Context.Operation.Name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,6 +14,8 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.ApplicationInsights.AspNet.TelemetryInitializers;
|
||||
|
||||
public class ApplicationInsightsExtensionsTests
|
||||
{
|
||||
|
@ -79,6 +81,30 @@
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddTelemetryWillAddOperationNameTelelemtryInitializerInConfigurationActive()
|
||||
{
|
||||
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 WebOperationNameTelemetryInitializer)
|
||||
.ToList();
|
||||
|
||||
Assert.Equal(1, items.Count);
|
||||
}
|
||||
finally
|
||||
{
|
||||
CleanActiveConfiguration();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JSSnippetWillNotThrowWithoutInstrumentationKey()
|
||||
{
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
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.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
public class WebOperationNameTelemetryInitializerTests
|
||||
{
|
||||
[Fact]
|
||||
public void InitializeDoesNotThrowIfHttpContextHolderIsUnavailable()
|
||||
{
|
||||
var initializer = new WebOperationNameTelemetryInitializer(new TestServiceProvider());
|
||||
|
||||
initializer.Initialize(new RequestTelemetry());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeDoesNotThrowIfHttpContextIsUnavailable()
|
||||
{
|
||||
var serviceProvider = new TestServiceProvider(new List<object>() { new HttpContextHolder() });
|
||||
var initializer = new WebOperationNameTelemetryInitializer(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 WebOperationNameTelemetryInitializer(serviceProvider);
|
||||
|
||||
initializer.Initialize(new RequestTelemetry());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeSetsTelemetryOperationNameToMethodAndPath()
|
||||
{
|
||||
var telemetry = new EventTelemetry();
|
||||
|
||||
var request = new DefaultHttpContext().Request;
|
||||
request.Method = "GET";
|
||||
request.Path = new Microsoft.AspNet.Http.PathString("/Test");
|
||||
var contextHolder = new HttpContextHolder();
|
||||
contextHolder.Context = request.HttpContext;
|
||||
|
||||
var serviceProvider = new TestServiceProvider(new List<object>() { contextHolder, new RequestTelemetry() });
|
||||
var initializer = new WebOperationNameTelemetryInitializer(serviceProvider);
|
||||
|
||||
initializer.Initialize(telemetry);
|
||||
|
||||
Assert.Equal("GET /Test", telemetry.Context.Operation.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeSetsRequestNameToMethodAndPath()
|
||||
{
|
||||
var telemetry = new RequestTelemetry();
|
||||
|
||||
var request = new DefaultHttpContext().Request;
|
||||
request.Method = "GET";
|
||||
request.Path = new Microsoft.AspNet.Http.PathString("/Test");
|
||||
var contextHolder = new HttpContextHolder();
|
||||
contextHolder.Context = request.HttpContext;
|
||||
|
||||
var serviceProvider = new TestServiceProvider(new List<object>() { contextHolder, new RequestTelemetry() });
|
||||
var initializer = new WebOperationNameTelemetryInitializer(serviceProvider);
|
||||
|
||||
initializer.Initialize(telemetry);
|
||||
|
||||
Assert.Equal("GET /Test", telemetry.Name);
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче