Skip to content

Commit bf2a67e

Browse files
committed
fix(flags): use feature_enabled field of unleash variants
1 parent fa241c3 commit bf2a67e

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

sentry_sdk/integrations/unleash.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def sentry_is_enabled(self, feature, *args, **kwargs):
3737
def sentry_get_variant(self, feature, *args, **kwargs):
3838
# type: (UnleashClient, str, *Any, **Any) -> Any
3939
variant = old_get_variant(self, feature, *args, **kwargs)
40-
enabled = variant.get("enabled", False)
40+
enabled = variant.get("feature_enabled", False)
4141

4242
# Payloads are not always used as the feature's value for application logic. They
4343
# may be used for metrics or debugging context instead. Therefore, we treat every

tests/integrations/unleash/test_unleash.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_is_enabled(sentry_init, capture_events, uninstall_integration):
1515
uninstall_integration(UnleashIntegration.identifier)
1616

1717
with mock_unleash_client():
18-
client = UnleashClient()
18+
client = UnleashClient() # type: ignore[arg-type]
1919
sentry_init(integrations=[UnleashIntegration()])
2020
client.is_enabled("hello")
2121
client.is_enabled("world")
@@ -38,9 +38,10 @@ def test_get_variant(sentry_init, capture_events, uninstall_integration):
3838
uninstall_integration(UnleashIntegration.identifier)
3939

4040
with mock_unleash_client():
41-
client = UnleashClient()
42-
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
41+
client = UnleashClient() # type: ignore[arg-type]
42+
sentry_init(integrations=[UnleashIntegration()])
4343
client.get_variant("no_payload_feature")
44+
client.get_variant("no_variant_feature")
4445
client.get_variant("string_feature")
4546
client.get_variant("json_feature")
4647
client.get_variant("csv_feature")
@@ -54,6 +55,7 @@ def test_get_variant(sentry_init, capture_events, uninstall_integration):
5455
assert events[0]["contexts"]["flags"] == {
5556
"values": [
5657
{"flag": "no_payload_feature", "result": True},
58+
{"flag": "no_variant_feature", "result": True},
5759
{"flag": "string_feature", "result": True},
5860
{"flag": "json_feature", "result": True},
5961
{"flag": "csv_feature", "result": True},
@@ -67,8 +69,8 @@ def test_is_enabled_threaded(sentry_init, capture_events, uninstall_integration)
6769
uninstall_integration(UnleashIntegration.identifier)
6870

6971
with mock_unleash_client():
70-
client = UnleashClient()
71-
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
72+
client = UnleashClient() # type: ignore[arg-type]
73+
sentry_init(integrations=[UnleashIntegration()])
7274
events = capture_events()
7375

7476
def task(flag_key):
@@ -116,8 +118,8 @@ def test_get_variant_threaded(sentry_init, capture_events, uninstall_integration
116118
uninstall_integration(UnleashIntegration.identifier)
117119

118120
with mock_unleash_client():
119-
client = UnleashClient()
120-
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
121+
client = UnleashClient() # type: ignore[arg-type]
122+
sentry_init(integrations=[UnleashIntegration()])
121123
events = capture_events()
122124

123125
def task(flag_key):
@@ -167,8 +169,8 @@ def test_is_enabled_asyncio(sentry_init, capture_events, uninstall_integration):
167169
uninstall_integration(UnleashIntegration.identifier)
168170

169171
with mock_unleash_client():
170-
client = UnleashClient()
171-
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
172+
client = UnleashClient() # type: ignore[arg-type]
173+
sentry_init(integrations=[UnleashIntegration()])
172174
events = capture_events()
173175

174176
async def task(flag_key):
@@ -219,8 +221,8 @@ def test_get_variant_asyncio(sentry_init, capture_events, uninstall_integration)
219221
uninstall_integration(UnleashIntegration.identifier)
220222

221223
with mock_unleash_client():
222-
client = UnleashClient()
223-
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
224+
client = UnleashClient() # type: ignore[arg-type]
225+
sentry_init(integrations=[UnleashIntegration()])
224226
events = capture_events()
225227

226228
async def task(flag_key):
@@ -266,7 +268,7 @@ async def runner():
266268

267269
def test_wraps_original(sentry_init, uninstall_integration):
268270
with mock_unleash_client():
269-
client = UnleashClient()
271+
client = UnleashClient() # type: ignore[arg-type]
270272

271273
mock_is_enabled = mock.Mock(return_value=random() < 0.5)
272274
mock_get_variant = mock.Mock(return_value={"enabled": random() < 0.5})
@@ -293,7 +295,7 @@ def test_wraps_original(sentry_init, uninstall_integration):
293295

294296
def test_wrapper_attributes(sentry_init, uninstall_integration):
295297
with mock_unleash_client():
296-
client = UnleashClient() # <- Returns a MockUnleashClient
298+
client = UnleashClient() # type: ignore[arg-type]
297299

298300
original_is_enabled = client.is_enabled
299301
original_get_variant = client.get_variant

tests/integrations/unleash/testutils.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,43 @@ def __init__(self, *a, **kw):
4848
"string_feature": {
4949
"name": "variant1",
5050
"enabled": True,
51+
"feature_enabled": True,
5152
"payload": {"type": "string", "value": "val1"},
5253
},
5354
"json_feature": {
5455
"name": "variant1",
5556
"enabled": True,
57+
"feature_enabled": True,
5658
"payload": {"type": "json", "value": '{"key1": 0.53}'},
5759
},
5860
"number_feature": {
5961
"name": "variant1",
6062
"enabled": True,
63+
"feature_enabled": True,
6164
"payload": {"type": "number", "value": "134.5"},
6265
},
6366
"csv_feature": {
6467
"name": "variant1",
6568
"enabled": True,
69+
"feature_enabled": True,
6670
"payload": {"type": "csv", "value": "abc 123\ncsbq 94"},
6771
},
68-
"no_payload_feature": {"name": "variant1", "enabled": True},
72+
"no_payload_feature": {
73+
"name": "variant1",
74+
"enabled": True,
75+
"feature_enabled": True,
76+
},
77+
"no_variant_feature": {
78+
"name": "disabled",
79+
"enabled": False,
80+
"feature_enabled": True,
81+
},
6982
}
7083

71-
self.disabled_variant = {"name": "disabled", "enabled": False}
84+
self.nonexistent_variant = {"name": "disabled", "enabled": False}
7285

7386
def is_enabled(self, feature, *a, **kw):
7487
return self.features.get(feature, False)
7588

7689
def get_variant(self, feature, *a, **kw):
77-
return self.feature_to_variant.get(feature, self.disabled_variant)
90+
return self.feature_to_variant.get(feature, self.nonexistent_variant)

0 commit comments

Comments
 (0)