Skip to content

Commit

Permalink
Track all variants regardless of payload
Browse files Browse the repository at this point in the history
  • Loading branch information
aliu39 committed Dec 23, 2024
1 parent 35b8dc2 commit 4cc1cae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
15 changes: 8 additions & 7 deletions sentry_sdk/integrations/unleash.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def sentry_is_enabled(self, feature, *a, **kw):
# type: (UnleashClient, str, *Any, **Any) -> Any
enabled = old_is_enabled(self, feature, *a, **kw)

# We have no way of knowing what type of feature this is. Have to treat it as
# boolean flag. TODO: Unless we fetch a list of non-bool flags on startup..
# We have no way of knowing what type of unleash feature this is, so we have to treat
# it as a boolean / toggle feature.
flags = sentry_sdk.get_current_scope().flags
flags.set(feature, enabled)

Expand All @@ -42,12 +42,13 @@ def sentry_get_variant(self, feature, *a, **kw):
# type: (UnleashClient, str, *Any, **Any) -> Any
variant = old_get_variant(self, feature, *a, **kw)
enabled = variant.get("enabled", False)
payload_type = variant.get("payload", {}).get("type")

if payload_type is None:
flags = sentry_sdk.get_current_scope().flags
flags.set(feature, enabled)
# _payload_type = variant.get("payload", {}).get("type")

# 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
# variant as a boolean toggle, using the `enabled` field.
flags = sentry_sdk.get_current_scope().flags
flags.set(feature, enabled)
return variant

UnleashClient.is_enabled = sentry_is_enabled # type: ignore
Expand Down
20 changes: 12 additions & 8 deletions tests/integrations/unleash/test_unleash.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_get_variant(sentry_init, capture_events, uninstall_integration):
uninstall_integration(UnleashIntegration)
sentry_init(integrations=[UnleashIntegration()])

client.get_variant("toggle_feature")
client.get_variant("no_payload_feature")
client.get_variant("string_feature")
client.get_variant("json_feature")
client.get_variant("csv_feature")
Expand All @@ -55,7 +55,11 @@ def test_get_variant(sentry_init, capture_events, uninstall_integration):
assert len(events) == 1
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "toggle_feature", "result": True},
{"flag": "no_payload_feature", "result": True},
{"flag": "string_feature", "result": True},
{"flag": "json_feature", "result": True},
{"flag": "csv_feature", "result": True},
{"flag": "number_feature", "result": True},
{"flag": "unknown_feature", "result": False},
]
}
Expand Down Expand Up @@ -129,7 +133,7 @@ def task(flag_key):
client.get_variant("hello")

with cf.ThreadPoolExecutor(max_workers=2) as pool:
pool.map(task, ["other", "toggle_feature"])
pool.map(task, ["no_payload_feature", "other"])

# Capture error in original scope
sentry_sdk.set_tag("task_id", "0")
Expand All @@ -146,13 +150,13 @@ def task(flag_key):
assert events[1]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
{"flag": "other", "result": False},
{"flag": "no_payload_feature", "result": True},
]
}
assert events[2]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
{"flag": "toggle_feature", "result": True},
{"flag": "other", "result": False},
]
}

Expand Down Expand Up @@ -226,7 +230,7 @@ async def task(flag_key):
sentry_sdk.capture_exception(Exception("something wrong!"))

async def runner():
return asyncio.gather(task("other"), task("toggle_feature"))
return asyncio.gather(task("no_payload_feature"), task("other"))

# Capture an eval before we split isolation scopes.
client.get_variant("hello")
Expand All @@ -248,13 +252,13 @@ async def runner():
assert events[1]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
{"flag": "other", "result": False},
{"flag": "no_payload_feature", "result": True},
]
}
assert events[2]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
{"flag": "toggle_feature", "result": True},
{"flag": "other", "result": False},
]
}

Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/unleash/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, *a, **kw):
"enabled": True,
"payload": {"type": "csv", "value": "abc 123\ncsbq 94"},
},
"toggle_feature": {"name": "variant1", "enabled": True},
"no_payload_feature": {"name": "variant1", "enabled": True},
}

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

0 comments on commit 4cc1cae

Please sign in to comment.