Skip to content

Commit 6a4c677

Browse files
authored
Adds TargetingContext to EvaluationEvent & Emits TargetingId in the App Insights Publisher (#347)
* Adds TargetingId to EvaluationEvent * Adjusts EvaluationEvent to use TargetingContext instead of ITargetingContext
1 parent 2d7073a commit 6a4c677

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryPublisher.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public ValueTask PublishEvent(EvaluationEvent evaluationEvent, CancellationToken
4242
{ "Enabled", evaluationEvent.Enabled.ToString() }
4343
};
4444

45+
if (evaluationEvent.TargetingContext != null)
46+
{
47+
properties["TargetingId"] = evaluationEvent.TargetingContext.UserId;
48+
}
49+
4550
if (evaluationEvent.VariantAssignmentReason != VariantAssignmentReason.None)
4651
{
4752
properties["Variant"] = evaluationEvent.Variant?.Name;

src/Microsoft.FeatureManagement/FeatureManager.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,21 @@ private async Task<EvaluationEvent> EvaluateFeature<TContext>(string feature, TC
263263
FeatureDefinition = await GetFeatureDefinition(feature).ConfigureAwait(false)
264264
};
265265

266+
//
267+
// Determine Targeting Context
268+
TargetingContext targetingContext;
269+
270+
if (useContext)
271+
{
272+
targetingContext = context as TargetingContext;
273+
}
274+
else
275+
{
276+
targetingContext = await ResolveTargetingContextAsync(cancellationToken).ConfigureAwait(false);
277+
}
278+
279+
evaluationEvent.TargetingContext = targetingContext;
280+
266281
if (evaluationEvent.FeatureDefinition != null)
267282
{
268283
//
@@ -296,17 +311,6 @@ private async Task<EvaluationEvent> EvaluateFeature<TContext>(string feature, TC
296311
}
297312
else
298313
{
299-
TargetingContext targetingContext;
300-
301-
if (useContext)
302-
{
303-
targetingContext = context as TargetingContext;
304-
}
305-
else
306-
{
307-
targetingContext = await ResolveTargetingContextAsync(cancellationToken).ConfigureAwait(false);
308-
}
309-
310314
if (targetingContext != null && evaluationEvent.FeatureDefinition.Allocation != null)
311315
{
312316
variantDefinition = await AssignVariantAsync(evaluationEvent, targetingContext, cancellationToken).ConfigureAwait(false);

src/Microsoft.FeatureManagement/Telemetry/EvaluationEvent.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33
//
4+
using Microsoft.FeatureManagement.FeatureFilters;
5+
46
namespace Microsoft.FeatureManagement.Telemetry
57
{
68
/// <summary>
@@ -13,6 +15,11 @@ public class EvaluationEvent
1315
/// </summary>
1416
public FeatureDefinition FeatureDefinition { get; set; }
1517

18+
/// <summary>
19+
/// The targeting context used to evaluate the feature.
20+
/// </summary>
21+
public TargetingContext TargetingContext { get; set; }
22+
1623
/// <summary>
1724
/// The enabled state of the feature after evaluation.
1825
/// </summary>

tests/Tests.FeatureManagement/FeatureManagement.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,7 @@ public async Task TelemetryPublishing()
10871087
variantResult = await featureManager.GetVariantAsync(Features.VariantFeaturePercentileOn, cancellationToken);
10881088
Assert.Equal("Big", variantResult.Name);
10891089
Assert.Equal("Big", testPublisher.evaluationEventCache.Variant.Name);
1090+
Assert.Equal("Marsha", testPublisher.evaluationEventCache.TargetingContext.UserId);
10901091
Assert.Equal(VariantAssignmentReason.Percentile, testPublisher.evaluationEventCache.VariantAssignmentReason);
10911092

10921093
variantResult = await featureManager.GetVariantAsync(Features.VariantFeaturePercentileOff, cancellationToken);

0 commit comments

Comments
 (0)