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
10 changes: 6 additions & 4 deletions featuremanagement/azuremonitor/_send_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ def publish_telemetry(evaluation_event: EvaluationEvent) -> None:

# VariantAllocationPercentage
allocation_percentage = 0
if reason == VariantAssignmentReason.DEFAULT_WHEN_ENABLED and feature.allocation and feature.allocation.percentile:
for allocation in feature.allocation.percentile:
allocation_percentage += allocation.percentile_to - allocation.percentile_from
event["VariantAssignmentPercentage"] = str(100 - allocation_percentage)
if reason == VariantAssignmentReason.DEFAULT_WHEN_ENABLED:
event["VariantAssignmentPercentage"] = str(100)
if feature.allocation:
for allocation in feature.allocation.percentile:
allocation_percentage += allocation.percentile_to - allocation.percentile_from
event["VariantAssignmentPercentage"] = str(100 - allocation_percentage)
elif reason == VariantAssignmentReason.PERCENTILE:
if feature.allocation and feature.allocation.percentile:
for allocation in feature.allocation.percentile:
Expand Down
35 changes: 35 additions & 0 deletions tests/test_send_telemetry_appinsights.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_send_telemetry_appinsights(self):
assert mock_track_event.call_args[0][1]["TargetingId"] == "test_user"
assert mock_track_event.call_args[0][1]["Variant"] == "TestVariant"
assert mock_track_event.call_args[0][1]["VariantAssignmentReason"] == "DefaultWhenDisabled"
assert "VariantAssignmentPercentage" not in mock_track_event.call_args[0][1]
assert mock_track_event.call_args[0][1]["ETag"] == "cmwBRcIAq1jUyKL3Kj8bvf9jtxBrFg-R-ayExStMC90"
assert (
mock_track_event.call_args[0][1]["FeatureFlagReference"]
Expand Down Expand Up @@ -77,6 +78,7 @@ def test_send_telemetry_appinsights_no_user(self):
assert "TargetingId" not in mock_track_event.call_args[0][1]
assert mock_track_event.call_args[0][1]["Variant"] == "TestVariant"
assert mock_track_event.call_args[0][1]["VariantAssignmentReason"] == "DefaultWhenDisabled"
assert "VariantAssignmentPercentage" not in mock_track_event.call_args[0][1]
assert "DefaultWhenEnabled" not in mock_track_event.call_args[0][1]

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

def test_send_telemetry_appinsights_default_when_enabled_no_percentile(self):
feature_flag = FeatureFlag.convert_from_json(
{
"id": "TestFeature",
"allocation": {
"default_when_enabled": "big",
},
}
)
evaluation_event = EvaluationEvent(feature_flag)
variant = Variant("big", None)
evaluation_event.feature = feature_flag
evaluation_event.enabled = True
evaluation_event.user = "test_user"
evaluation_event.variant = variant
evaluation_event.reason = VariantAssignmentReason.DEFAULT_WHEN_ENABLED

with patch("featuremanagement.azuremonitor._send_telemetry.azure_monitor_track_event") as mock_track_event:
# This is called like this so we can override the track_event function
featuremanagement.azuremonitor._send_telemetry.publish_telemetry( # pylint: disable=protected-access
evaluation_event
)
mock_track_event.assert_called_once()
assert mock_track_event.call_args[0][0] == "FeatureEvaluation"
assert mock_track_event.call_args[0][1]["FeatureName"] == "TestFeature"
assert mock_track_event.call_args[0][1]["Enabled"] == "True"
assert mock_track_event.call_args[0][1]["TargetingId"] == "test_user"
assert mock_track_event.call_args[0][1]["Variant"] == "big"
assert mock_track_event.call_args[0][1]["VariantAssignmentReason"] == "DefaultWhenEnabled"
assert mock_track_event.call_args[0][1]["VariantAssignmentPercentage"] == "100"
assert "DefaultWhenEnabled" in mock_track_event.call_args[0][1]
assert mock_track_event.call_args[0][1]["DefaultWhenEnabled"] == "big"

def test_send_telemetry_appinsights_allocation(self):
feature_flag = FeatureFlag.convert_from_json(
{
Expand Down
Loading