diff --git a/src/ApplicationInsights.AspNet/ApplicationInsightsExtensions.cs b/src/ApplicationInsights.AspNet/ApplicationInsightsExtensions.cs index f0fb18f..67ac959 100644 --- a/src/ApplicationInsights.AspNet/ApplicationInsightsExtensions.cs +++ b/src/ApplicationInsights.AspNet/ApplicationInsightsExtensions.cs @@ -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(new TelemetryClient()); + services.AddSingleton((svcs) => { + TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebClientIpHeaderTelemetryInitializer(svcs)); + return new TelemetryClient(); + }); + services.AddScoped((svcs) => { var rt = new RequestTelemetry(); // this is workaround to inject proper instrumentation key into javascript: rt.Context.InstrumentationKey = svcs.GetService().Context.InstrumentationKey; return rt; }); + + services.AddScoped(); } public static HtmlString ApplicationInsightsJavaScriptSnippet(this IHtmlHelper helper, string instrumentationKey) diff --git a/src/ApplicationInsights.AspNet/ApplicationInsightsRequestMiddleware.cs b/src/ApplicationInsights.AspNet/ApplicationInsightsRequestMiddleware.cs index bff41c9..bc928b3 100644 --- a/src/ApplicationInsights.AspNet/ApplicationInsightsRequestMiddleware.cs +++ b/src/ApplicationInsights.AspNet/ApplicationInsightsRequestMiddleware.cs @@ -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().Context = httpContext; + var sw = new Stopwatch(); sw.Start(); diff --git a/src/ApplicationInsights.AspNet/DataCollection/WebClientIpHeaderTelemetryInitializer.cs b/src/ApplicationInsights.AspNet/DataCollection/WebClientIpHeaderTelemetryInitializer.cs new file mode 100644 index 0000000..9719921 --- /dev/null +++ b/src/ApplicationInsights.AspNet/DataCollection/WebClientIpHeaderTelemetryInitializer.cs @@ -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(); + if (!string.IsNullOrEmpty(request.Context.Location.Ip)) + { + telemetry.Context.Location.Ip = request.Context.Location.Ip; + } + else + { + var context = this.serviceProvider.GetService().Context; + + var connectionFeature = context.GetFeature(); + + if (connectionFeature != null) + { + string ip = connectionFeature.RemoteIpAddress.ToString(); + request.Context.Location.Ip = ip; + if (request != telemetry) + { + telemetry.Context.Location.Ip = ip; + } + } + } + } + } +} \ No newline at end of file diff --git a/src/ApplicationInsights.AspNet/Implementation/HttpContextHolder.cs b/src/ApplicationInsights.AspNet/Implementation/HttpContextHolder.cs new file mode 100644 index 0000000..8030132 --- /dev/null +++ b/src/ApplicationInsights.AspNet/Implementation/HttpContextHolder.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNet.Http; +using System; + +namespace Microsoft.ApplicationInsights.AspNet.Implementation +{ + public class HttpContextHolder + { + public HttpContext Context; + } +} \ No newline at end of file