Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public ValueTask PublishEvent(EvaluationEvent evaluationEvent, CancellationToken
{ "Enabled", evaluationEvent.Enabled.ToString() }
};

if (evaluationEvent.TargetingContext != null)
{
properties["TargetingId"] = evaluationEvent.TargetingContext.UserId;
Copy link
Member

Choose a reason for hiding this comment

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

I am ok with the current implementation. Just curious about what will happen if the TargetingContext.UserId is null.
How will the experimentation link be established in this case? Will all metrics and events with a null "TargetingId" be linked together?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good question. The metrics will still be emitted, but linking them together from a null targetingId would not provide much value. Split will ignore the data for Experimentation if the id is null. The only value of the field is to explicitly indicate that there was no value, so it cannot be associated with metrics.

}

if (evaluationEvent.VariantAssignmentReason != VariantAssignmentReason.None)
{
properties["Variant"] = evaluationEvent.Variant?.Name;
Expand Down
26 changes: 15 additions & 11 deletions src/Microsoft.FeatureManagement/FeatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,21 @@ private async Task<EvaluationEvent> EvaluateFeature<TContext>(string feature, TC
FeatureDefinition = await GetFeatureDefinition(feature).ConfigureAwait(false)
};

//
// Determine Targeting Context
TargetingContext targetingContext;

if (useContext)
{
targetingContext = context as TargetingContext;
}
else
{
targetingContext = await ResolveTargetingContextAsync(cancellationToken).ConfigureAwait(false);
}

evaluationEvent.TargetingContext = targetingContext;

if (evaluationEvent.FeatureDefinition != null)
{
//
Expand Down Expand Up @@ -296,17 +311,6 @@ private async Task<EvaluationEvent> EvaluateFeature<TContext>(string feature, TC
}
else
{
TargetingContext targetingContext;

if (useContext)
{
targetingContext = context as TargetingContext;
}
else
{
targetingContext = await ResolveTargetingContextAsync(cancellationToken).ConfigureAwait(false);
}

if (targetingContext != null && evaluationEvent.FeatureDefinition.Allocation != null)
{
variantDefinition = await AssignVariantAsync(evaluationEvent, targetingContext, cancellationToken).ConfigureAwait(false);
Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.FeatureManagement/Telemetry/EvaluationEvent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.FeatureManagement.FeatureFilters;

namespace Microsoft.FeatureManagement.Telemetry
{
/// <summary>
Expand All @@ -13,6 +15,11 @@ public class EvaluationEvent
/// </summary>
public FeatureDefinition FeatureDefinition { get; set; }

/// <summary>
/// The targeting context used to evaluate the feature.
/// </summary>
public TargetingContext TargetingContext { get; set; }

/// <summary>
/// The enabled state of the feature after evaluation.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions tests/Tests.FeatureManagement/FeatureManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,7 @@ public async Task TelemetryPublishing()
variantResult = await featureManager.GetVariantAsync(Features.VariantFeaturePercentileOn, cancellationToken);
Assert.Equal("Big", variantResult.Name);
Assert.Equal("Big", testPublisher.evaluationEventCache.Variant.Name);
Assert.Equal("Marsha", testPublisher.evaluationEventCache.TargetingContext.UserId);
Assert.Equal(VariantAssignmentReason.Percentile, testPublisher.evaluationEventCache.VariantAssignmentReason);

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