Skip to content

Commit

Permalink
fix(flags): use feature_enabled field of unleash variants
Browse files Browse the repository at this point in the history
  • Loading branch information
aliu39 committed Jan 10, 2025
1 parent fa241c3 commit bf2a67e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/unleash.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def sentry_is_enabled(self, feature, *args, **kwargs):
def sentry_get_variant(self, feature, *args, **kwargs):
# type: (UnleashClient, str, *Any, **Any) -> Any
variant = old_get_variant(self, feature, *args, **kwargs)
enabled = variant.get("enabled", False)
enabled = variant.get("feature_enabled", False)

# Payloads are not always used as the feature's value for application logic. They
# may be used for metrics or debugging context instead. Therefore, we treat every
Expand Down
28 changes: 15 additions & 13 deletions tests/integrations/unleash/test_unleash.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_is_enabled(sentry_init, capture_events, uninstall_integration):
uninstall_integration(UnleashIntegration.identifier)

with mock_unleash_client():
client = UnleashClient()
client = UnleashClient() # type: ignore[arg-type]
sentry_init(integrations=[UnleashIntegration()])
client.is_enabled("hello")
client.is_enabled("world")
Expand All @@ -38,9 +38,10 @@ def test_get_variant(sentry_init, capture_events, uninstall_integration):
uninstall_integration(UnleashIntegration.identifier)

with mock_unleash_client():
client = UnleashClient()
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
client = UnleashClient() # type: ignore[arg-type]
sentry_init(integrations=[UnleashIntegration()])
client.get_variant("no_payload_feature")
client.get_variant("no_variant_feature")
client.get_variant("string_feature")
client.get_variant("json_feature")
client.get_variant("csv_feature")
Expand All @@ -54,6 +55,7 @@ def test_get_variant(sentry_init, capture_events, uninstall_integration):
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "no_payload_feature", "result": True},
{"flag": "no_variant_feature", "result": True},
{"flag": "string_feature", "result": True},
{"flag": "json_feature", "result": True},
{"flag": "csv_feature", "result": True},
Expand All @@ -67,8 +69,8 @@ def test_is_enabled_threaded(sentry_init, capture_events, uninstall_integration)
uninstall_integration(UnleashIntegration.identifier)

with mock_unleash_client():
client = UnleashClient()
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
client = UnleashClient() # type: ignore[arg-type]
sentry_init(integrations=[UnleashIntegration()])
events = capture_events()

def task(flag_key):
Expand Down Expand Up @@ -116,8 +118,8 @@ def test_get_variant_threaded(sentry_init, capture_events, uninstall_integration
uninstall_integration(UnleashIntegration.identifier)

with mock_unleash_client():
client = UnleashClient()
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
client = UnleashClient() # type: ignore[arg-type]
sentry_init(integrations=[UnleashIntegration()])
events = capture_events()

def task(flag_key):
Expand Down Expand Up @@ -167,8 +169,8 @@ def test_is_enabled_asyncio(sentry_init, capture_events, uninstall_integration):
uninstall_integration(UnleashIntegration.identifier)

with mock_unleash_client():
client = UnleashClient()
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
client = UnleashClient() # type: ignore[arg-type]
sentry_init(integrations=[UnleashIntegration()])
events = capture_events()

async def task(flag_key):
Expand Down Expand Up @@ -219,8 +221,8 @@ def test_get_variant_asyncio(sentry_init, capture_events, uninstall_integration)
uninstall_integration(UnleashIntegration.identifier)

with mock_unleash_client():
client = UnleashClient()
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
client = UnleashClient() # type: ignore[arg-type]
sentry_init(integrations=[UnleashIntegration()])
events = capture_events()

async def task(flag_key):
Expand Down Expand Up @@ -266,7 +268,7 @@ async def runner():

def test_wraps_original(sentry_init, uninstall_integration):
with mock_unleash_client():
client = UnleashClient()
client = UnleashClient() # type: ignore[arg-type]

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

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

original_is_enabled = client.is_enabled
original_get_variant = client.get_variant
Expand Down
19 changes: 16 additions & 3 deletions tests/integrations/unleash/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,43 @@ def __init__(self, *a, **kw):
"string_feature": {
"name": "variant1",
"enabled": True,
"feature_enabled": True,
"payload": {"type": "string", "value": "val1"},
},
"json_feature": {
"name": "variant1",
"enabled": True,
"feature_enabled": True,
"payload": {"type": "json", "value": '{"key1": 0.53}'},
},
"number_feature": {
"name": "variant1",
"enabled": True,
"feature_enabled": True,
"payload": {"type": "number", "value": "134.5"},
},
"csv_feature": {
"name": "variant1",
"enabled": True,
"feature_enabled": True,
"payload": {"type": "csv", "value": "abc 123\ncsbq 94"},
},
"no_payload_feature": {"name": "variant1", "enabled": True},
"no_payload_feature": {
"name": "variant1",
"enabled": True,
"feature_enabled": True,
},
"no_variant_feature": {
"name": "disabled",
"enabled": False,
"feature_enabled": True,
},
}

self.disabled_variant = {"name": "disabled", "enabled": False}
self.nonexistent_variant = {"name": "disabled", "enabled": False}

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

def get_variant(self, feature, *a, **kw):
return self.feature_to_variant.get(feature, self.disabled_variant)
return self.feature_to_variant.get(feature, self.nonexistent_variant)

0 comments on commit bf2a67e

Please sign in to comment.