Skip to content

Commit c21ad56

Browse files
mrm9084rossgrambo
andauthored
Variant Assignment Percentage Fix (#46)
* Fixing no assignment percentage when assignment is None * Update _send_telemetry.py * Update featuremanagement/azuremonitor/_send_telemetry.py Co-authored-by: Ross Grambo <[email protected]> --------- Co-authored-by: Ross Grambo <[email protected]>
1 parent 0498455 commit c21ad56

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

featuremanagement/azuremonitor/_send_telemetry.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ def publish_telemetry(evaluation_event: EvaluationEvent) -> None:
7777

7878
# VariantAllocationPercentage
7979
allocation_percentage = 0
80-
if reason == VariantAssignmentReason.DEFAULT_WHEN_ENABLED and feature.allocation and feature.allocation.percentile:
81-
for allocation in feature.allocation.percentile:
82-
allocation_percentage += allocation.percentile_to - allocation.percentile_from
83-
event["VariantAssignmentPercentage"] = str(100 - allocation_percentage)
80+
if reason == VariantAssignmentReason.DEFAULT_WHEN_ENABLED:
81+
event["VariantAssignmentPercentage"] = str(100)
82+
if feature.allocation:
83+
for allocation in feature.allocation.percentile:
84+
allocation_percentage += allocation.percentile_to - allocation.percentile_from
85+
event["VariantAssignmentPercentage"] = str(100 - allocation_percentage)
8486
elif reason == VariantAssignmentReason.PERCENTILE:
8587
if feature.allocation and feature.allocation.percentile:
8688
for allocation in feature.allocation.percentile:

tests/test_send_telemetry_appinsights.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def test_send_telemetry_appinsights(self):
4747
assert mock_track_event.call_args[0][1]["TargetingId"] == "test_user"
4848
assert mock_track_event.call_args[0][1]["Variant"] == "TestVariant"
4949
assert mock_track_event.call_args[0][1]["VariantAssignmentReason"] == "DefaultWhenDisabled"
50+
assert "VariantAssignmentPercentage" not in mock_track_event.call_args[0][1]
5051
assert mock_track_event.call_args[0][1]["ETag"] == "cmwBRcIAq1jUyKL3Kj8bvf9jtxBrFg-R-ayExStMC90"
5152
assert (
5253
mock_track_event.call_args[0][1]["FeatureFlagReference"]
@@ -77,6 +78,7 @@ def test_send_telemetry_appinsights_no_user(self):
7778
assert "TargetingId" not in mock_track_event.call_args[0][1]
7879
assert mock_track_event.call_args[0][1]["Variant"] == "TestVariant"
7980
assert mock_track_event.call_args[0][1]["VariantAssignmentReason"] == "DefaultWhenDisabled"
81+
assert "VariantAssignmentPercentage" not in mock_track_event.call_args[0][1]
8082
assert "DefaultWhenEnabled" not in mock_track_event.call_args[0][1]
8183

8284
def test_send_telemetry_appinsights_no_variant(self):
@@ -145,6 +147,39 @@ def test_send_telemetry_appinsights_default_when_enabled(self):
145147
assert "DefaultWhenEnabled" in mock_track_event.call_args[0][1]
146148
assert mock_track_event.call_args[0][1]["DefaultWhenEnabled"] == "big"
147149

150+
def test_send_telemetry_appinsights_default_when_enabled_no_percentile(self):
151+
feature_flag = FeatureFlag.convert_from_json(
152+
{
153+
"id": "TestFeature",
154+
"allocation": {
155+
"default_when_enabled": "big",
156+
},
157+
}
158+
)
159+
evaluation_event = EvaluationEvent(feature_flag)
160+
variant = Variant("big", None)
161+
evaluation_event.feature = feature_flag
162+
evaluation_event.enabled = True
163+
evaluation_event.user = "test_user"
164+
evaluation_event.variant = variant
165+
evaluation_event.reason = VariantAssignmentReason.DEFAULT_WHEN_ENABLED
166+
167+
with patch("featuremanagement.azuremonitor._send_telemetry.azure_monitor_track_event") as mock_track_event:
168+
# This is called like this so we can override the track_event function
169+
featuremanagement.azuremonitor._send_telemetry.publish_telemetry( # pylint: disable=protected-access
170+
evaluation_event
171+
)
172+
mock_track_event.assert_called_once()
173+
assert mock_track_event.call_args[0][0] == "FeatureEvaluation"
174+
assert mock_track_event.call_args[0][1]["FeatureName"] == "TestFeature"
175+
assert mock_track_event.call_args[0][1]["Enabled"] == "True"
176+
assert mock_track_event.call_args[0][1]["TargetingId"] == "test_user"
177+
assert mock_track_event.call_args[0][1]["Variant"] == "big"
178+
assert mock_track_event.call_args[0][1]["VariantAssignmentReason"] == "DefaultWhenEnabled"
179+
assert mock_track_event.call_args[0][1]["VariantAssignmentPercentage"] == "100"
180+
assert "DefaultWhenEnabled" in mock_track_event.call_args[0][1]
181+
assert mock_track_event.call_args[0][1]["DefaultWhenEnabled"] == "big"
182+
148183
def test_send_telemetry_appinsights_allocation(self):
149184
feature_flag = FeatureFlag.convert_from_json(
150185
{

0 commit comments

Comments
 (0)