Skip to content

Commit 4cc1cae

Browse files
committed
Track all variants regardless of payload
1 parent 35b8dc2 commit 4cc1cae

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

sentry_sdk/integrations/unleash.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def sentry_is_enabled(self, feature, *a, **kw):
3030
# type: (UnleashClient, str, *Any, **Any) -> Any
3131
enabled = old_is_enabled(self, feature, *a, **kw)
3232

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

@@ -42,12 +42,13 @@ def sentry_get_variant(self, feature, *a, **kw):
4242
# type: (UnleashClient, str, *Any, **Any) -> Any
4343
variant = old_get_variant(self, feature, *a, **kw)
4444
enabled = variant.get("enabled", False)
45-
payload_type = variant.get("payload", {}).get("type")
46-
47-
if payload_type is None:
48-
flags = sentry_sdk.get_current_scope().flags
49-
flags.set(feature, enabled)
45+
# _payload_type = variant.get("payload", {}).get("type")
5046

47+
# Payloads are not always used as the feature's value for application logic. They
48+
# may be used for metrics or debugging context instead. Therefore, we treat every
49+
# variant as a boolean toggle, using the `enabled` field.
50+
flags = sentry_sdk.get_current_scope().flags
51+
flags.set(feature, enabled)
5152
return variant
5253

5354
UnleashClient.is_enabled = sentry_is_enabled # type: ignore

tests/integrations/unleash/test_unleash.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_get_variant(sentry_init, capture_events, uninstall_integration):
4242
uninstall_integration(UnleashIntegration)
4343
sentry_init(integrations=[UnleashIntegration()])
4444

45-
client.get_variant("toggle_feature")
45+
client.get_variant("no_payload_feature")
4646
client.get_variant("string_feature")
4747
client.get_variant("json_feature")
4848
client.get_variant("csv_feature")
@@ -55,7 +55,11 @@ def test_get_variant(sentry_init, capture_events, uninstall_integration):
5555
assert len(events) == 1
5656
assert events[0]["contexts"]["flags"] == {
5757
"values": [
58-
{"flag": "toggle_feature", "result": True},
58+
{"flag": "no_payload_feature", "result": True},
59+
{"flag": "string_feature", "result": True},
60+
{"flag": "json_feature", "result": True},
61+
{"flag": "csv_feature", "result": True},
62+
{"flag": "number_feature", "result": True},
5963
{"flag": "unknown_feature", "result": False},
6064
]
6165
}
@@ -129,7 +133,7 @@ def task(flag_key):
129133
client.get_variant("hello")
130134

131135
with cf.ThreadPoolExecutor(max_workers=2) as pool:
132-
pool.map(task, ["other", "toggle_feature"])
136+
pool.map(task, ["no_payload_feature", "other"])
133137

134138
# Capture error in original scope
135139
sentry_sdk.set_tag("task_id", "0")
@@ -146,13 +150,13 @@ def task(flag_key):
146150
assert events[1]["contexts"]["flags"] == {
147151
"values": [
148152
{"flag": "hello", "result": False},
149-
{"flag": "other", "result": False},
153+
{"flag": "no_payload_feature", "result": True},
150154
]
151155
}
152156
assert events[2]["contexts"]["flags"] == {
153157
"values": [
154158
{"flag": "hello", "result": False},
155-
{"flag": "toggle_feature", "result": True},
159+
{"flag": "other", "result": False},
156160
]
157161
}
158162

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

228232
async def runner():
229-
return asyncio.gather(task("other"), task("toggle_feature"))
233+
return asyncio.gather(task("no_payload_feature"), task("other"))
230234

231235
# Capture an eval before we split isolation scopes.
232236
client.get_variant("hello")
@@ -248,13 +252,13 @@ async def runner():
248252
assert events[1]["contexts"]["flags"] == {
249253
"values": [
250254
{"flag": "hello", "result": False},
251-
{"flag": "other", "result": False},
255+
{"flag": "no_payload_feature", "result": True},
252256
]
253257
}
254258
assert events[2]["contexts"]["flags"] == {
255259
"values": [
256260
{"flag": "hello", "result": False},
257-
{"flag": "toggle_feature", "result": True},
261+
{"flag": "other", "result": False},
258262
]
259263
}
260264

tests/integrations/unleash/testutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, *a, **kw):
2727
"enabled": True,
2828
"payload": {"type": "csv", "value": "abc 123\ncsbq 94"},
2929
},
30-
"toggle_feature": {"name": "variant1", "enabled": True},
30+
"no_payload_feature": {"name": "variant1", "enabled": True},
3131
}
3232

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

0 commit comments

Comments
 (0)