Skip to content

Commit bb8565d

Browse files
Merges variants and isenabled paths. Adds variant reason field. (#290)
* Adds Reason field and adjusts evaluation logic * use enum AssignmentReason * add more testcases & remove some interal methods * fix typo * update comments * resolve comments * update comment for DisabledDefault * fix typo * update * update telemetry publisher * add reason when no allocation section * update * remove nullable * resolve comments * update comments --------- Co-authored-by: zhiyuanliang <zhiyuanliang@microsoft.com>
1 parent 9d0d968 commit bb8565d

File tree

8 files changed

+740
-585
lines changed

8 files changed

+740
-585
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33
//
44
using Microsoft.ApplicationInsights;
5+
using System.Diagnostics;
56

67
namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights
78
{
@@ -30,15 +31,17 @@ public ValueTask PublishEvent(EvaluationEvent evaluationEvent, CancellationToken
3031

3132
FeatureDefinition featureDefinition = evaluationEvent.FeatureDefinition;
3233

33-
Dictionary<string, string> properties = new Dictionary<string, string>()
34+
var properties = new Dictionary<string, string>()
3435
{
3536
{ "FeatureName", featureDefinition.Name },
36-
{ "IsEnabled", evaluationEvent.IsEnabled.ToString() }
37+
{ "Enabled", evaluationEvent.Enabled.ToString() }
3738
};
3839

39-
if (evaluationEvent.Variant != null)
40+
if (evaluationEvent.VariantAssignmentReason != VariantAssignmentReason.None)
4041
{
41-
properties["Variant"] = evaluationEvent.Variant.Name;
42+
properties["Variant"] = evaluationEvent.Variant?.Name;
43+
44+
properties["VariantAssignmentReason"] = ToString(evaluationEvent.VariantAssignmentReason);
4245
}
4346

4447
if (featureDefinition.TelemetryMetadata != null)
@@ -66,5 +69,26 @@ private void ValidateEvent(EvaluationEvent evaluationEvent)
6669
throw new ArgumentNullException(nameof(evaluationEvent.FeatureDefinition));
6770
}
6871
}
72+
73+
private static string ToString(VariantAssignmentReason reason)
74+
{
75+
Debug.Assert(reason != VariantAssignmentReason.None);
76+
77+
const string DefaultWhenDisabled = "DefaultWhenDisabled";
78+
const string DefaultWhenEnabled = "DefaultWhenEnabled";
79+
const string User = "User";
80+
const string Group = "Group";
81+
const string Percentile = "Percentile";
82+
83+
return reason switch
84+
{
85+
VariantAssignmentReason.DefaultWhenDisabled => DefaultWhenDisabled,
86+
VariantAssignmentReason.DefaultWhenEnabled => DefaultWhenEnabled,
87+
VariantAssignmentReason.User => User,
88+
VariantAssignmentReason.Group => Group,
89+
VariantAssignmentReason.Percentile => Percentile,
90+
_ => throw new ArgumentException("Invalid assignment reason.", nameof(reason))
91+
};
92+
}
6993
}
7094
}

src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/Microsoft.FeatureManagement.Telemetry.ApplicationInsights.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
76
</PropertyGroup>
87

98
<ItemGroup>

src/Microsoft.FeatureManagement/FeatureManager.cs

Lines changed: 122 additions & 123 deletions
Large diffs are not rendered by default.

src/Microsoft.FeatureManagement/Telemetry/EvaluationEvent.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33
//
4-
using Microsoft.FeatureManagement.FeatureFilters;
5-
64
namespace Microsoft.FeatureManagement.Telemetry
75
{
86
/// <summary>
@@ -18,11 +16,16 @@ public class EvaluationEvent
1816
/// <summary>
1917
/// The enabled state of the feature after evaluation.
2018
/// </summary>
21-
public bool IsEnabled { get; set; }
19+
public bool Enabled { get; set; }
2220

2321
/// <summary>
2422
/// The variant given after evaluation.
2523
/// </summary>
2624
public Variant Variant { get; set; }
25+
26+
/// <summary>
27+
/// The reason why the variant was assigned.
28+
/// </summary>
29+
public VariantAssignmentReason VariantAssignmentReason { get; set; }
2730
}
2831
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
//
4+
namespace Microsoft.FeatureManagement.Telemetry
5+
{
6+
/// <summary>
7+
/// The reason the variant was assigned during the evaluation of a feature.
8+
/// </summary>
9+
public enum VariantAssignmentReason
10+
{
11+
/// <summary>
12+
/// Variant allocation did not happend. No variant is assigned.
13+
/// </summary>
14+
None,
15+
16+
/// <summary>
17+
/// The default variant is assigned when a feature flag is disabled.
18+
/// </summary>
19+
DefaultWhenDisabled,
20+
21+
/// <summary>
22+
/// The default variant is assigned because of no applicable user/group/percentile allocation when a feature flag is enabled.
23+
/// </summary>
24+
DefaultWhenEnabled,
25+
26+
/// <summary>
27+
/// The variant is assigned because of the user allocation when a feature flag is enabled.
28+
/// </summary>
29+
User,
30+
31+
/// <summary>
32+
/// The variant is assigned because of the group allocation when a feature flag is enabled.
33+
/// </summary>
34+
Group,
35+
36+
/// <summary>
37+
/// The variant is assigned because of the percentile allocation when a feature flag is enabled.
38+
/// </summary>
39+
Percentile
40+
}
41+
}

0 commit comments

Comments
 (0)