Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activity Based TargetingId Persistance #467

Merged
merged 4 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Microsoft.FeatureManagement.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.FeatureManagement
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EvaluationDataToApplicationInsights", "examples\EvaluationDataToApplicationInsights\EvaluationDataToApplicationInsights.csproj", "{1502529E-47E9-4306-98C4-BF6CF7C7C275}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore", "src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore.csproj", "{C647611B-A8E5-4AD7-9DBA-60DDE276644B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorServerApp", "examples\BlazorServerApp\BlazorServerApp.csproj", "{12BAB5A6-4EEB-4917-B5D9-4AFB6253008E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VariantServiceDemo", "examples\VariantServiceDemo\VariantServiceDemo.csproj", "{E8E17CB9-434E-4386-BF96-FA53BBFDCD6F}"
Expand Down Expand Up @@ -79,10 +77,6 @@ Global
{1502529E-47E9-4306-98C4-BF6CF7C7C275}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1502529E-47E9-4306-98C4-BF6CF7C7C275}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1502529E-47E9-4306-98C4-BF6CF7C7C275}.Release|Any CPU.Build.0 = Release|Any CPU
{C647611B-A8E5-4AD7-9DBA-60DDE276644B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C647611B-A8E5-4AD7-9DBA-60DDE276644B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C647611B-A8E5-4AD7-9DBA-60DDE276644B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C647611B-A8E5-4AD7-9DBA-60DDE276644B}.Release|Any CPU.Build.0 = Release|Any CPU
{12BAB5A6-4EEB-4917-B5D9-4AFB6253008E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12BAB5A6-4EEB-4917-B5D9-4AFB6253008E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12BAB5A6-4EEB-4917-B5D9-4AFB6253008E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.AspNetCore\Microsoft.FeatureManagement.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion examples/EvaluationDataToApplicationInsights/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using EvaluationDataToApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore;
using Microsoft.FeatureManagement.Telemetry.ApplicationInsights;

var builder = WebApplication.CreateBuilder(args);

Expand Down
2 changes: 1 addition & 1 deletion examples/VariantServiceDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using VariantServiceDemo;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore;
using Microsoft.FeatureManagement.Telemetry.ApplicationInsights;


var builder = WebApplication.CreateBuilder(args);
Expand Down
1 change: 0 additions & 1 deletion examples/VariantServiceDemo/VariantServiceDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.AspNetCore\Microsoft.FeatureManagement.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.csproj" />
</ItemGroup>

Expand Down
3 changes: 1 addition & 2 deletions pack.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ $targetProjects = @(

"Microsoft.FeatureManagement",
"Microsoft.FeatureManagement.AspNetCore",
"Microsoft.FeatureManagement.Telemetry.ApplicationInsights",
"Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore"
"Microsoft.FeatureManagement.Telemetry.ApplicationInsights"
)

# Create the log directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
//

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging;
using Microsoft.FeatureManagement.FeatureFilters;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace Microsoft.FeatureManagement
Expand All @@ -23,22 +25,23 @@ public class TargetingHttpContextMiddleware
/// <summary>
/// Creates an instance of the TargetingHttpContextMiddleware
/// </summary>
public TargetingHttpContextMiddleware(RequestDelegate next, ILoggerFactory loggerFactory) {
public TargetingHttpContextMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
_logger = loggerFactory?.CreateLogger<TargetingHttpContextMiddleware>() ?? throw new ArgumentNullException(nameof(loggerFactory));
}

/// <summary>
/// Adds targeting information to the HTTP context.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> to add the targeting information to.</param>
/// <param name="httpContext">The <see cref="HttpContext"/> to add the targeting information to.</param>
/// <param name="targetingContextAccessor">The <see cref="ITargetingContextAccessor"/> to retrieve the targeting information from.</param>
/// <exception cref="ArgumentNullException">Thrown if the provided context or targetingContextAccessor is null.</exception>
public async Task InvokeAsync(HttpContext context, ITargetingContextAccessor targetingContextAccessor)
public async Task InvokeAsync(HttpContext httpContext, ITargetingContextAccessor targetingContextAccessor)
{
if (context == null)
if (httpContext == null)
{
throw new ArgumentNullException(nameof(context));
throw new ArgumentNullException(nameof(httpContext));
}

if (targetingContextAccessor == null)
Expand All @@ -50,14 +53,27 @@ public async Task InvokeAsync(HttpContext context, ITargetingContextAccessor tar

if (targetingContext != null)
{
context.Items[TargetingIdKey] = targetingContext.UserId;
var activityFeature = httpContext.Features.Get<IHttpActivityFeature>();
rossgrambo marked this conversation as resolved.
Show resolved Hide resolved

if (activityFeature == null)
{
_logger.LogDebug("The IHttpActivityFeature from the IFeatureCollection was null");
}
else if (activityFeature.Activity == null)
{
_logger.LogDebug("The Activity on the IHttpActivityFeature was null");
}
else
{
activityFeature.Activity.AddBaggage(TargetingIdKey, targetingContext.UserId);
}
}
else
{
_logger.LogDebug("The targeting context accessor returned a null TargetingContext");
}

await _next(context);
await _next(httpContext);
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using System.Diagnostics;

namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights
{
/// <summary>
/// Used to add targeting information to outgoing Application Insights telemetry.
/// </summary>
public class TargetingTelemetryInitializer : ITelemetryInitializer
{
private const string TargetingIdKey = $"Microsoft.FeatureManagement.TargetingId";

/// <summary>
/// When telemetry is initialized, adds targeting information to all relevant telemetry.
/// </summary>
/// <param name="telemetry">The <see cref="ITelemetry"/> to be initialized.</param>
/// <exception cref="ArgumentNullException">Thrown if the any param is null.</exception>
public void Initialize(ITelemetry telemetry)
{
if (telemetry == null)
{
throw new ArgumentNullException(nameof(telemetry));
}

// Extract the targeting id from the current activity's baggage
string targetingId = Activity.Current?.Baggage.FirstOrDefault(t => t.Key == TargetingIdKey).Value;

// Don't modify telemetry if there's no available targeting id
if (string.IsNullOrEmpty(targetingId))
{
return;
}

// Telemetry.Properties is deprecated in favor of ISupportProperties
if (telemetry is ISupportProperties telemetryWithSupportProperties)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case that this wouldn't be true, perhaps if a user used an old version of app insights?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is no, that they will always be available. The deprecation message for telemetry.Properties says:

TelemetryContext.Properties is obsolete. Use GlobalProperties to set global level properties. For properties at item level, use ISupportProperties.Properties.

They were added in v2.21.0. .NET 6 and above should use at least v2.21.0, so I think we can assume this will be available. The versions before that are all deprecated (actually including v2.21.0 as well). v2.22.0 is the latest.

{
telemetryWithSupportProperties.Properties["TargetingId"] = targetingId;
}
}
}
}