Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Implement ClientIpTelemetryInitializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Restrepo committed Mar 16, 2016
1 parent 1bd5e87 commit bd686be
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions WCF/NuGet/common/ApplicationInsights.config.install.xdt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Wcf.OperationCorrelationTelemetryInitializer, Microsoft.AI.Wcf" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Wcf.UserTelemetryInitializer, Microsoft.AI.Wcf" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Wcf.UserAgentTelemetryInitializer, Microsoft.AI.Wcf" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Wcf.ClientIpTelemetryInitializer, Microsoft.AI.Wcf" />
</TelemetryInitializers>
</ApplicationInsights>
1 change: 1 addition & 0 deletions WCF/NuGet/common/ApplicationInsights.config.uninstall.xdt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Wcf.OperationCorrelationTelemetryInitializer, Microsoft.AI.Wcf" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Wcf.UserTelemetryInitializer, Microsoft.AI.Wcf" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Wcf.UserAgentTelemetryInitializer, Microsoft.AI.Wcf" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Wcf.ClientIpTelemetryInitializer, Microsoft.AI.Wcf" />
</TelemetryInitializers>
<TelemetryInitializers xdt:Transform="Remove" xdt:Locator="Condition(count(*)=0)"/>

Expand Down
59 changes: 59 additions & 0 deletions WCF/Shared.Tests/ClientIpTelemetryInitializerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.ServiceModel.Channels;
using System.Text;

namespace Microsoft.ApplicationInsights.Wcf.Tests
{
[TestClass]
public class ClientIpTelemetryInitializerTests
{
[TestMethod]
public void SetsClientIpFromWcfContextOnRequest()
{
const String clientIp = "10.12.32.12";
var context = new MockOperationContext();
context.IncomingProperties.Add(
RemoteEndpointMessageProperty.Name,
new RemoteEndpointMessageProperty(clientIp, 7656));

var initializer = new ClientIpTelemetryInitializer();

var telemetry = context.Request;
initializer.Initialize(telemetry, context);

Assert.AreEqual(clientIp, telemetry.Context.Location.Ip);
}

[TestMethod]
public void SetsClientIpFromWcfContextOnOtherEvent()
{
const String originalIp = "10.12.32.12";
const String newIp = "172.34.12.45";
var context = new MockOperationContext();
context.IncomingProperties.Add(
RemoteEndpointMessageProperty.Name,
new RemoteEndpointMessageProperty(originalIp, 7656));

var initializer = new ClientIpTelemetryInitializer();

// initialize request with the original IP
initializer.Initialize(context.Request, context);

// replace IP so that we can tell
// it is being picked up from the request
// rather than the context
context.IncomingProperties.Clear();
context.IncomingProperties.Add(
RemoteEndpointMessageProperty.Name,
new RemoteEndpointMessageProperty(newIp, 7656));

var telemetry = new EventTelemetry("myevent");
initializer.Initialize(telemetry, context);

Assert.AreEqual(originalIp, telemetry.Context.Location.Ip);
}
}
}
1 change: 1 addition & 0 deletions WCF/Shared.Tests/Microsoft.AI.Wcf.Tests.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Import_RootNamespace>Microsoft.ApplicationInsights.Wcf.Tests</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ClientIpTelemetryInitializerTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ContractDescriptionBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ContractFilterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)OperationFilterTests.cs" />
Expand Down
43 changes: 43 additions & 0 deletions WCF/Shared/ClientIpTelemetryInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using System.ServiceModel.Channels;

namespace Microsoft.ApplicationInsights.Wcf
{
/// <summary>
/// Collects the IP of the client calling the WCF service
/// </summary>
public sealed class ClientIpTelemetryInitializer : WcfTelemetryInitializer
{
/// <summary>
/// Initialize the telemetry event with the client IP if available
/// </summary>
/// <param name="telemetry">The telemetry event</param>
/// <param name="operation">The WCF operation context</param>
protected override void OnInitialize(ITelemetry telemetry, IOperationContext operation)
{
if ( String.IsNullOrEmpty(telemetry.Context.Location.Ip) )
{
var location = operation.Request.Context.Location;
if ( String.IsNullOrEmpty(location.Ip) )
{
UpdateClientIp(location, operation);
}
telemetry.Context.Location.Ip = location.Ip;
}
}

private void UpdateClientIp(LocationContext location, IOperationContext operation)
{
if ( operation.HasIncomingMessageProperty(RemoteEndpointMessageProperty.Name) )
{
var property = (RemoteEndpointMessageProperty)
operation.GetIncomingMessageProperty(RemoteEndpointMessageProperty.Name);
location.Ip = property.Address;
}
}
}
}
1 change: 1 addition & 0 deletions WCF/Shared/Microsoft.AI.Wcf.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Import_RootNamespace>Microsoft.ApplicationInsights.Wcf</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ClientIpTelemetryInitializer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\ContractFilter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\Executor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\OperationFilter.cs" />
Expand Down
1 change: 1 addition & 0 deletions WCF/net40.tests/ApplicationInsights.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Add Type="Microsoft.ApplicationInsights.Wcf.OperationCorrelationTelemetryInitializer, Microsoft.AI.Wcf"/>
<Add Type="Microsoft.ApplicationInsights.Wcf.UserTelemetryInitializer, Microsoft.AI.Wcf"/>
<Add Type="Microsoft.ApplicationInsights.Wcf.UserAgentTelemetryInitializer, Microsoft.AI.Wcf"/>
<Add Type="Microsoft.ApplicationInsights.Wcf.ClientIpTelemetryInitializer, Microsoft.AI.Wcf"/>
</TelemetryInitializers>
<TelemetryChannel Type="Microsoft.ApplicationInsights.Wcf.Tests.Channels.TestTelemetryChannel, Microsoft.AI.Wcf.Tests.Net40"/>
</ApplicationInsights>
1 change: 1 addition & 0 deletions WCF/net45.tests/ApplicationInsights.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Add Type="Microsoft.ApplicationInsights.Wcf.OperationCorrelationTelemetryInitializer, Microsoft.AI.Wcf"/>
<Add Type="Microsoft.ApplicationInsights.Wcf.UserTelemetryInitializer, Microsoft.AI.Wcf"/>
<Add Type="Microsoft.ApplicationInsights.Wcf.UserAgentTelemetryInitializer, Microsoft.AI.Wcf"/>
<Add Type="Microsoft.ApplicationInsights.Wcf.ClientIpTelemetryInitializer, Microsoft.AI.Wcf"/>
</TelemetryInitializers>
<TelemetryChannel Type="Microsoft.ApplicationInsights.Wcf.Tests.Channels.TestTelemetryChannel, Microsoft.AI.Wcf.Tests.Net45"/>
</ApplicationInsights>

0 comments on commit bd686be

Please sign in to comment.