Skip to content

Commit 7c40943

Browse files
committed
Rm get_variant code
1 parent adb3bb1 commit 7c40943

File tree

3 files changed

+2
-208
lines changed

3 files changed

+2
-208
lines changed

sentry_sdk/integrations/unleash.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def setup_once():
1919
# type: () -> None
2020
# Wrap and patch evaluation methods (instance methods)
2121
old_is_enabled = UnleashClient.is_enabled
22-
old_get_variant = UnleashClient.get_variant
2322

2423
@wraps(old_is_enabled)
2524
def sentry_is_enabled(self, feature, *args, **kwargs):
@@ -33,22 +32,7 @@ def sentry_is_enabled(self, feature, *args, **kwargs):
3332

3433
return enabled
3534

36-
@wraps(old_get_variant)
37-
def sentry_get_variant(self, feature, *args, **kwargs):
38-
# type: (UnleashClient, str, *Any, **Any) -> Any
39-
variant = old_get_variant(self, feature, *args, **kwargs)
40-
enabled = variant.get("feature_enabled", False)
41-
42-
# Payloads are not always used as the feature's value for application logic. They
43-
# may be used for metrics or debugging context instead. Therefore, we treat every
44-
# variant as a boolean toggle, using the `enabled` field.
45-
flags = sentry_sdk.get_current_scope().flags
46-
flags.set(feature, enabled)
47-
48-
return variant
49-
5035
UnleashClient.is_enabled = sentry_is_enabled # type: ignore
51-
UnleashClient.get_variant = sentry_get_variant # type: ignore
5236

5337
# Error processor
5438
scope = sentry_sdk.get_current_scope()

tests/integrations/unleash/test_unleash.py

Lines changed: 0 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,6 @@ def test_is_enabled(sentry_init, capture_events, uninstall_integration):
3434
}
3535

3636

37-
def test_get_variant(sentry_init, capture_events, uninstall_integration):
38-
uninstall_integration(UnleashIntegration.identifier)
39-
40-
with mock_unleash_client():
41-
client = UnleashClient() # type: ignore[arg-type]
42-
sentry_init(integrations=[UnleashIntegration()])
43-
client.get_variant("no_payload_feature")
44-
client.get_variant("no_variant_feature")
45-
client.get_variant("string_feature")
46-
client.get_variant("json_feature")
47-
client.get_variant("csv_feature")
48-
client.get_variant("number_feature")
49-
client.get_variant("unknown_feature")
50-
51-
events = capture_events()
52-
sentry_sdk.capture_exception(Exception("something wrong!"))
53-
54-
assert len(events) == 1
55-
assert events[0]["contexts"]["flags"] == {
56-
"values": [
57-
{"flag": "no_payload_feature", "result": True},
58-
{"flag": "no_variant_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},
63-
{"flag": "unknown_feature", "result": False},
64-
]
65-
}
66-
67-
6837
def test_is_enabled_threaded(sentry_init, capture_events, uninstall_integration):
6938
uninstall_integration(UnleashIntegration.identifier)
7039

@@ -114,55 +83,6 @@ def task(flag_key):
11483
}
11584

11685

117-
def test_get_variant_threaded(sentry_init, capture_events, uninstall_integration):
118-
uninstall_integration(UnleashIntegration.identifier)
119-
120-
with mock_unleash_client():
121-
client = UnleashClient() # type: ignore[arg-type]
122-
sentry_init(integrations=[UnleashIntegration()])
123-
events = capture_events()
124-
125-
def task(flag_key):
126-
# Creates a new isolation scope for the thread.
127-
# This means the evaluations in each task are captured separately.
128-
with sentry_sdk.isolation_scope():
129-
client.get_variant(flag_key)
130-
# use a tag to identify to identify events later on
131-
sentry_sdk.set_tag("task_id", flag_key)
132-
sentry_sdk.capture_exception(Exception("something wrong!"))
133-
134-
# Capture an eval before we split isolation scopes.
135-
client.get_variant("hello")
136-
137-
with cf.ThreadPoolExecutor(max_workers=2) as pool:
138-
pool.map(task, ["no_payload_feature", "other"])
139-
140-
# Capture error in original scope
141-
sentry_sdk.set_tag("task_id", "0")
142-
sentry_sdk.capture_exception(Exception("something wrong!"))
143-
144-
assert len(events) == 3
145-
events.sort(key=lambda e: e["tags"]["task_id"])
146-
147-
assert events[0]["contexts"]["flags"] == {
148-
"values": [
149-
{"flag": "hello", "result": False},
150-
]
151-
}
152-
assert events[1]["contexts"]["flags"] == {
153-
"values": [
154-
{"flag": "hello", "result": False},
155-
{"flag": "no_payload_feature", "result": True},
156-
]
157-
}
158-
assert events[2]["contexts"]["flags"] == {
159-
"values": [
160-
{"flag": "hello", "result": False},
161-
{"flag": "other", "result": False},
162-
]
163-
}
164-
165-
16686
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
16787
def test_is_enabled_asyncio(sentry_init, capture_events, uninstall_integration):
16888
asyncio = pytest.importorskip("asyncio")
@@ -214,66 +134,12 @@ async def runner():
214134
}
215135

216136

217-
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
218-
def test_get_variant_asyncio(sentry_init, capture_events, uninstall_integration):
219-
asyncio = pytest.importorskip("asyncio")
220-
221-
uninstall_integration(UnleashIntegration.identifier)
222-
223-
with mock_unleash_client():
224-
client = UnleashClient() # type: ignore[arg-type]
225-
sentry_init(integrations=[UnleashIntegration()])
226-
events = capture_events()
227-
228-
async def task(flag_key):
229-
with sentry_sdk.isolation_scope():
230-
client.get_variant(flag_key)
231-
# use a tag to identify to identify events later on
232-
sentry_sdk.set_tag("task_id", flag_key)
233-
sentry_sdk.capture_exception(Exception("something wrong!"))
234-
235-
async def runner():
236-
return asyncio.gather(task("no_payload_feature"), task("other"))
237-
238-
# Capture an eval before we split isolation scopes.
239-
client.get_variant("hello")
240-
241-
asyncio.run(runner())
242-
243-
# Capture error in original scope
244-
sentry_sdk.set_tag("task_id", "0")
245-
sentry_sdk.capture_exception(Exception("something wrong!"))
246-
247-
assert len(events) == 3
248-
events.sort(key=lambda e: e["tags"]["task_id"])
249-
250-
assert events[0]["contexts"]["flags"] == {
251-
"values": [
252-
{"flag": "hello", "result": False},
253-
]
254-
}
255-
assert events[1]["contexts"]["flags"] == {
256-
"values": [
257-
{"flag": "hello", "result": False},
258-
{"flag": "no_payload_feature", "result": True},
259-
]
260-
}
261-
assert events[2]["contexts"]["flags"] == {
262-
"values": [
263-
{"flag": "hello", "result": False},
264-
{"flag": "other", "result": False},
265-
]
266-
}
267-
268-
269137
def test_wraps_original(sentry_init, uninstall_integration):
270138
with mock_unleash_client():
271139
client = UnleashClient() # type: ignore[arg-type]
272140

273141
mock_is_enabled = mock.Mock(return_value=random() < 0.5)
274-
mock_get_variant = mock.Mock(return_value={"enabled": random() < 0.5})
275142
client.is_enabled = mock_is_enabled
276-
client.get_variant = mock_get_variant
277143

278144
uninstall_integration(UnleashIntegration.identifier)
279145
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
@@ -285,26 +151,16 @@ def test_wraps_original(sentry_init, uninstall_integration):
285151
{"kwarg": 1},
286152
)
287153

288-
res = client.get_variant("test-flag", "arg", kwarg=1)
289-
assert res == mock_get_variant.return_value
290-
assert mock_get_variant.call_args == (
291-
("test-flag", "arg"),
292-
{"kwarg": 1},
293-
)
294-
295154

296155
def test_wrapper_attributes(sentry_init, uninstall_integration):
297156
with mock_unleash_client():
298157
client = UnleashClient() # type: ignore[arg-type]
299158

300159
original_is_enabled = client.is_enabled
301-
original_get_variant = client.get_variant
302160

303161
uninstall_integration(UnleashIntegration.identifier)
304162
sentry_init(integrations=[UnleashIntegration()]) # type: ignore
305163

306164
# Mock clients methods have not lost their qualified names after decoration.
307165
assert client.is_enabled.__name__ == "is_enabled"
308166
assert client.is_enabled.__qualname__ == original_is_enabled.__qualname__
309-
assert client.get_variant.__name__ == "get_variant"
310-
assert client.get_variant.__qualname__ == original_get_variant.__qualname__

tests/integrations/unleash/testutils.py

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def mock_unleash_client():
88
Temporarily replaces UnleashClient's methods with mock implementations
99
for testing.
1010
11-
This context manager swaps out UnleashClient's __init__, is_enabled,
12-
and get_variant methods with mock versions from MockUnleashClient.
11+
This context manager swaps out UnleashClient's __init__ and is_enabled,
12+
methods with mock versions from MockUnleashClient.
1313
Original methods are restored when exiting the context.
1414
1515
After mocking the client class the integration can be initialized.
@@ -23,17 +23,14 @@ def mock_unleash_client():
2323
"""
2424
old_init = UnleashClient.__init__
2525
old_is_enabled = UnleashClient.is_enabled
26-
old_get_variant = UnleashClient.get_variant
2726

2827
UnleashClient.__init__ = MockUnleashClient.__init__
2928
UnleashClient.is_enabled = MockUnleashClient.is_enabled
30-
UnleashClient.get_variant = MockUnleashClient.get_variant
3129

3230
yield
3331

3432
UnleashClient.__init__ = old_init
3533
UnleashClient.is_enabled = old_is_enabled
36-
UnleashClient.get_variant = old_get_variant
3734

3835

3936
class MockUnleashClient:
@@ -44,48 +41,5 @@ def __init__(self, *a, **kw):
4441
"world": False,
4542
}
4643

47-
self.feature_to_variant = {
48-
"string_feature": {
49-
"name": "variant1",
50-
"enabled": True,
51-
"feature_enabled": True,
52-
"payload": {"type": "string", "value": "val1"},
53-
},
54-
"json_feature": {
55-
"name": "variant1",
56-
"enabled": True,
57-
"feature_enabled": True,
58-
"payload": {"type": "json", "value": '{"key1": 0.53}'},
59-
},
60-
"number_feature": {
61-
"name": "variant1",
62-
"enabled": True,
63-
"feature_enabled": True,
64-
"payload": {"type": "number", "value": "134.5"},
65-
},
66-
"csv_feature": {
67-
"name": "variant1",
68-
"enabled": True,
69-
"feature_enabled": True,
70-
"payload": {"type": "csv", "value": "abc 123\ncsbq 94"},
71-
},
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-
},
82-
}
83-
84-
# Returned when the feature does not exist.
85-
self.fallback_variant = {"name": "disabled", "enabled": False}
86-
8744
def is_enabled(self, feature, *a, **kw):
8845
return self.features.get(feature, False)
89-
90-
def get_variant(self, feature, *a, **kw):
91-
return self.feature_to_variant.get(feature, self.fallback_variant)

0 commit comments

Comments
 (0)