Skip to content

Commit 9403168

Browse files
mrm9084rossgrambo
andauthored
New telemetry values (#41)
* Adding new telemetry values * Update _send_telemetry.py * cleaning up code * Removing temp files * Moving Allocation ID to provider * type check fixes * Update _send_telemetry.py * Update _featuremanagerbase.py * Update featuremanagement/azuremonitor/_send_telemetry.py Co-authored-by: Ross Grambo <[email protected]> * trying to fix formatter * trying to fix black --------- Co-authored-by: Ross Grambo <[email protected]>
1 parent 577987c commit 9403168

File tree

9 files changed

+58
-18
lines changed

9 files changed

+58
-18
lines changed

.github/workflows/validate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Analysing the code with pylint
2323
run: |
2424
pylint featuremanagement
25-
- uses: psf/black@stable
25+
- uses: psf/black@24.8.0
2626
- name: Run mypy
2727
run: |
2828
mypy featuremanagement

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release History
22

3+
## 2.0.0b2 (Unreleased)
4+
5+
* Adds VariantAllocationPercentage, DefaultWhenEnabled, and AllocationId to telemetry.
6+
* Allocation seed value is now None by default, and only defaults to `allocation\n<feature.id>` when assigning variants.
7+
38
## 2.0.0b1 (09/10/2024)
49

510
* Adds support for Feature Variants.

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
project = "FeatureManagement"
1111
copyright = "2024, Microsoft"
1212
author = "Microsoft"
13-
release = "2.0.0b1"
13+
release = "2.0.0b2"
1414

1515
# -- General configuration ---------------------------------------------------
1616

featuremanagement/_featuremanagerbase.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ def _assign_variant(
175175
evaluation_event.reason = VariantAssignmentReason.GROUP
176176
variant_name = group_allocation.variant
177177
if not variant_name and feature.allocation.percentile:
178-
context_id = targeting_context.user_id + "\n" + feature.allocation.seed
178+
seed = feature.allocation.seed
179+
if not seed:
180+
seed = "allocation\n" + feature.name
181+
context_id = targeting_context.user_id + "\n" + seed
179182
box: float = self._is_targeted(context_id)
180183
for percentile_allocation in feature.allocation.percentile:
181184
if box == 100 and percentile_allocation.percentile_to == 100:

featuremanagement/_models/_allocation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ class Allocation:
103103
Represents an allocation configuration for a feature flag.
104104
"""
105105

106-
def __init__(self, feature_name: str) -> None:
106+
def __init__(self) -> None:
107107
self._default_when_enabled = None
108108
self._default_when_disabled = None
109109
self._user: List[UserAllocation] = []
110110
self._group: List[GroupAllocation] = []
111111
self._percentile: List[PercentileAllocation] = []
112-
self._seed = "allocation\n" + feature_name
112+
self._seed = None
113113

114114
@classmethod
115-
def convert_from_json(cls, json: Dict[str, Any], feature_name: str) -> Optional["Allocation"]:
115+
def convert_from_json(cls, json: Dict[str, Any]) -> Optional["Allocation"]:
116116
"""
117117
Convert a JSON object to Allocation.
118118
@@ -123,7 +123,7 @@ def convert_from_json(cls, json: Dict[str, Any], feature_name: str) -> Optional[
123123
"""
124124
if not json:
125125
return None
126-
allocation = cls(feature_name)
126+
allocation = cls()
127127
allocation._default_when_enabled = json.get(DEFAULT_WHEN_ENABLED)
128128
allocation._default_when_disabled = json.get(DEFAULT_WHEN_DISABLED)
129129
allocation._user = []
@@ -197,7 +197,7 @@ def percentile(self) -> List[PercentileAllocation]:
197197
return self._percentile
198198

199199
@property
200-
def seed(self) -> str:
200+
def seed(self) -> Optional[str]:
201201
"""
202202
Get the seed for the allocation.
203203

featuremanagement/_models/_feature_flag.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ def convert_from_json(cls, json_value: Mapping[str, Any]) -> "FeatureFlag":
5151
)
5252
else:
5353
feature_flag._conditions = FeatureConditions()
54-
feature_flag._allocation = Allocation.convert_from_json(
55-
json_value.get(FEATURE_FLAG_ALLOCATION, None), feature_flag._id
56-
)
54+
feature_flag._allocation = Allocation.convert_from_json(json_value.get(FEATURE_FLAG_ALLOCATION, None))
5755
if FEATURE_FLAG_VARIANTS in json_value:
5856
variants: List[Mapping[str, Any]] = json_value.get(FEATURE_FLAG_VARIANTS, [])
5957
feature_flag._variants = []
@@ -66,7 +64,7 @@ def convert_from_json(cls, json_value: Mapping[str, Any]) -> "FeatureFlag":
6664
return feature_flag
6765

6866
@property
69-
def name(self) -> Optional[str]:
67+
def name(self) -> str:
7068
"""
7169
Get the name of the feature flag.
7270

featuremanagement/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
# license information.
55
# -------------------------------------------------------------------------
66

7-
VERSION = "2.0.0b1"
7+
VERSION = "2.0.0b2"

featuremanagement/azuremonitor/_send_telemetry.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
EVENT_NAME = "FeatureEvaluation"
2727

28+
EVALUATION_EVENT_VERSION = "1.0.0"
29+
2830

2931
def track_event(event_name: str, user: str, event_properties: Optional[Dict[str, Optional[str]]] = None) -> None:
3032
"""
@@ -51,17 +53,49 @@ def publish_telemetry(evaluation_event: EvaluationEvent) -> None:
5153
"""
5254
if not HAS_AZURE_MONITOR_EVENTS_EXTENSION:
5355
return
54-
event = {}
55-
if evaluation_event.feature:
56-
event[FEATURE_NAME] = evaluation_event.feature.name
56+
event: Dict[str, Optional[str]] = {}
57+
if not evaluation_event.feature:
58+
return
59+
event[FEATURE_NAME] = evaluation_event.feature.name
5760
event[ENABLED] = str(evaluation_event.enabled)
61+
event["Version"] = EVALUATION_EVENT_VERSION
5862

63+
# VariantAllocationPercentage
5964
if evaluation_event.reason and evaluation_event.reason != VariantAssignmentReason.NONE:
6065
if evaluation_event.variant:
6166
event[VARIANT] = evaluation_event.variant.name
6267
event[REASON] = evaluation_event.reason.value
6368

64-
if evaluation_event.feature and evaluation_event.feature.telemetry:
69+
if evaluation_event.reason == VariantAssignmentReason.DEFAULT_WHEN_ENABLED:
70+
allocation_percentage = 0
71+
72+
if evaluation_event.feature.allocation and evaluation_event.feature.allocation.percentile:
73+
for allocation in evaluation_event.feature.allocation.percentile:
74+
if (
75+
evaluation_event.variant
76+
and allocation.variant == evaluation_event.variant.name
77+
and allocation.percentile_to
78+
):
79+
allocation_percentage += allocation.percentile_to - allocation.percentile_from
80+
81+
event["VariantAssignmentPercentage"] = str(100 - allocation_percentage)
82+
elif evaluation_event.reason == VariantAssignmentReason.PERCENTILE:
83+
if evaluation_event.feature.allocation and evaluation_event.feature.allocation.percentile:
84+
allocation_percentage = 0
85+
for allocation in evaluation_event.feature.allocation.percentile:
86+
if (
87+
evaluation_event.variant
88+
and allocation.variant == evaluation_event.variant.name
89+
and allocation.percentile_to
90+
):
91+
allocation_percentage += allocation.percentile_to - allocation.percentile_from
92+
event["VariantAssignmentPercentage"] = str(allocation_percentage)
93+
94+
# DefaultWhenEnabled
95+
if evaluation_event.feature.allocation and evaluation_event.feature.allocation.default_when_enabled:
96+
event["DefaultWhenEnabled"] = evaluation_event.feature.allocation.default_when_enabled
97+
98+
if evaluation_event.feature.telemetry:
6599
for metadata_key, metadata_value in evaluation_event.feature.telemetry.metadata.items():
66100
if metadata_key not in event:
67101
event[metadata_key] = metadata_value

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ build-backend = "setuptools.build_meta"
1919

2020
[project]
2121
name = "FeatureManagement"
22-
version = "2.0.0b1"
22+
version = "2.0.0b2"
2323
authors = [
2424
{ name="Microsoft Corporation", email="[email protected]" },
2525
]

0 commit comments

Comments
 (0)