1
0
Форкнуть 0

Merge pull request #1 from Microsoft/sergkanz/prototypeOfTelemetryInitializer

prototype of telemetry initializer
This commit is contained in:
Sergey Kanzhelev 2015-03-25 14:27:22 -07:00
Родитель 4d69cd678d 525fe11698
Коммит f6f9c81cac
4 изменённых файлов: 69 добавлений и 1 удалений

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

@ -8,6 +8,8 @@
using System;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.ApplicationInsights.AspNet.DataCollection;
using Microsoft.ApplicationInsights.AspNet.Implementation;
public static class ApplicationInsightsExtensions
{
@ -33,13 +35,19 @@
{
TelemetryConfiguration.Active.InstrumentationKey = config.Get("ApplicationInsights:InstrumentationKey");
services.AddInstance<TelemetryClient>(new TelemetryClient());
services.AddSingleton<TelemetryClient>((svcs) => {
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebClientIpHeaderTelemetryInitializer(svcs));
return new TelemetryClient();
});
services.AddScoped<RequestTelemetry>((svcs) => {
var rt = new RequestTelemetry();
// this is workaround to inject proper instrumentation key into javascript:
rt.Context.InstrumentationKey = svcs.GetService<TelemetryClient>().Context.InstrumentationKey;
return rt;
});
services.AddScoped<HttpContextHolder>();
}
public static HtmlString ApplicationInsightsJavaScriptSnippet(this IHtmlHelper helper, string instrumentationKey)

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

@ -1,6 +1,7 @@
namespace Microsoft.ApplicationInsights.AspNet
{
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.AspNet.Implementation;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNet.Builder;
@ -29,6 +30,8 @@
public async Task Invoke(HttpContext httpContext)
{
this.serviceProvider.GetService<HttpContextHolder>().Context = httpContext;
var sw = new Stopwatch();
sw.Start();

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

@ -0,0 +1,47 @@

namespace Microsoft.ApplicationInsights.AspNet.DataCollection
{
using Microsoft.ApplicationInsights.Extensibility;
using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Interfaces;
using Microsoft.ApplicationInsights.AspNet.Implementation;
public class WebClientIpHeaderTelemetryInitializer : ITelemetryInitializer
{
private IServiceProvider serviceProvider;
public WebClientIpHeaderTelemetryInitializer(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public void Initialize(ITelemetry telemetry)
{
var request = this.serviceProvider.GetService<RequestTelemetry>();
if (!string.IsNullOrEmpty(request.Context.Location.Ip))
{
telemetry.Context.Location.Ip = request.Context.Location.Ip;
}
else
{
var context = this.serviceProvider.GetService<HttpContextHolder>().Context;
var connectionFeature = context.GetFeature<IHttpConnectionFeature>();
if (connectionFeature != null)
{
string ip = connectionFeature.RemoteIpAddress.ToString();
request.Context.Location.Ip = ip;
if (request != telemetry)
{
telemetry.Context.Location.Ip = ip;
}
}
}
}
}
}

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

@ -0,0 +1,10 @@
using Microsoft.AspNet.Http;
using System;
namespace Microsoft.ApplicationInsights.AspNet.Implementation
{
public class HttpContextHolder
{
public HttpContext Context;
}
}