This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ClientIpTelemetryInitializer
- Loading branch information
Tomas Restrepo
committed
Mar 16, 2016
1 parent
1bd5e87
commit bd686be
Showing
8 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters