-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added telemetry initializer adding client IP to custom dimensions * Made client IP key configurable
- Loading branch information
1 parent
9f83528
commit 96993b1
Showing
15 changed files
with
230 additions
and
35 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
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
26 changes: 26 additions & 0 deletions
26
src/ApplicationInsightsRequestLogging/Initializers/ClientIpInitializer.cs
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,26 @@ | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Azureblue.ApplicationInsights.RequestLogging | ||
{ | ||
public class ClientIpInitializer : ITelemetryInitializer | ||
{ | ||
private readonly BodyLoggerOptions _options; | ||
|
||
public ClientIpInitializer(IOptions<BodyLoggerOptions> options) => _options = options.Value; | ||
|
||
public void Initialize(ITelemetry telemetry) | ||
{ | ||
if (!_options.DisableIpMasking) return; | ||
|
||
var clientIpKey = _options.ClientIpPropertyKey; | ||
if (telemetry is ISupportProperties propTelemetry && !propTelemetry.Properties.ContainsKey(clientIpKey)) | ||
{ | ||
var clientIpValue = telemetry.Context.Location.Ip; | ||
propTelemetry.Properties.Add(clientIpKey, clientIpValue); | ||
} | ||
} | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
test/ApplicationInsightsRequestLoggingTests/FakeRemoteIpAddressMiddleware.cs
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,14 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace ApplicationInsightsRequestLoggingTests; | ||
|
||
public class FakeRemoteIpAddressMiddleware : IMiddleware | ||
{ | ||
public async Task InvokeAsync(HttpContext context, RequestDelegate next) | ||
{ | ||
context.Connection.RemoteIpAddress = IPAddress.Parse("127.168.1.32"); | ||
await next(context); | ||
} | ||
} |
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,22 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace ManualTests.Controllers | ||
{ | ||
[Route("api/")] | ||
[ApiController] | ||
public class TestController : ControllerBase | ||
{ | ||
[HttpPost("test")] | ||
public ActionResult<TestData> Test([FromBody] TestData data) | ||
{ | ||
return Ok(data); | ||
} | ||
} | ||
} | ||
|
||
public class TestData | ||
{ | ||
public string Name { get; set; } | ||
public string Blog { get; set; } | ||
public string Topics { get; set; } | ||
} |
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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\ApplicationInsightsRequestLogging\ApplicationInsightsRequestLogging.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.