forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_feature_flags.py
More file actions
318 lines (259 loc) · 9 KB
/
test_feature_flags.py
File metadata and controls
318 lines (259 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import concurrent.futures as cf
import sys
import copy
import threading
import pytest
import sentry_sdk
from sentry_sdk.feature_flags import add_feature_flag, FlagBuffer
from sentry_sdk import start_span, start_transaction
from tests.conftest import ApproxDict
def test_featureflags_integration(sentry_init, capture_events, uninstall_integration):
sentry_init()
add_feature_flag("hello", False)
add_feature_flag("world", True)
add_feature_flag("other", False)
events = capture_events()
sentry_sdk.capture_exception(Exception("something wrong!"))
assert len(events) == 1
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
{"flag": "world", "result": True},
{"flag": "other", "result": False},
]
}
@pytest.mark.asyncio
async def test_featureflags_integration_spans_async(sentry_init, capture_events):
sentry_init(
traces_sample_rate=1.0,
)
events = capture_events()
add_feature_flag("hello", False)
try:
with sentry_sdk.start_span(name="test-span"):
with sentry_sdk.start_span(name="test-span-2"):
raise ValueError("something wrong!")
except ValueError as e:
sentry_sdk.capture_exception(e)
found = False
for event in events:
if "exception" in event.keys():
assert event["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
]
}
found = True
assert found, "No event with exception found"
def test_featureflags_integration_spans_sync(sentry_init, capture_events):
sentry_init(
traces_sample_rate=1.0,
)
events = capture_events()
add_feature_flag("hello", False)
try:
with sentry_sdk.start_span(name="test-span"):
with sentry_sdk.start_span(name="test-span-2"):
raise ValueError("something wrong!")
except ValueError as e:
sentry_sdk.capture_exception(e)
found = False
for event in events:
if "exception" in event.keys():
assert event["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
]
}
found = True
assert found, "No event with exception found"
def test_featureflags_integration_threaded(
sentry_init, capture_events, uninstall_integration
):
sentry_init()
events = capture_events()
# Capture an eval before we split isolation scopes.
add_feature_flag("hello", False)
def task(flag_key):
# Creates a new isolation scope for the thread.
# This means the evaluations in each task are captured separately.
with sentry_sdk.isolation_scope():
add_feature_flag(flag_key, False)
# use a tag to identify to identify events later on
sentry_sdk.set_tag("task_id", flag_key)
sentry_sdk.capture_exception(Exception("something wrong!"))
# Run tasks in separate threads
with cf.ThreadPoolExecutor(max_workers=2) as pool:
pool.map(task, ["world", "other"])
# Capture error in original scope
sentry_sdk.set_tag("task_id", "0")
sentry_sdk.capture_exception(Exception("something wrong!"))
assert len(events) == 3
events.sort(key=lambda e: e["tags"]["task_id"])
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
]
}
assert events[1]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
{"flag": "other", "result": False},
]
}
assert events[2]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
{"flag": "world", "result": False},
]
}
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
def test_featureflags_integration_asyncio(
sentry_init, capture_events, uninstall_integration
):
asyncio = pytest.importorskip("asyncio")
sentry_init()
events = capture_events()
# Capture an eval before we split isolation scopes.
add_feature_flag("hello", False)
async def task(flag_key):
# Creates a new isolation scope for the thread.
# This means the evaluations in each task are captured separately.
with sentry_sdk.isolation_scope():
add_feature_flag(flag_key, False)
# use a tag to identify to identify events later on
sentry_sdk.set_tag("task_id", flag_key)
sentry_sdk.capture_exception(Exception("something wrong!"))
async def runner():
return asyncio.gather(task("world"), task("other"))
asyncio.run(runner())
# Capture error in original scope
sentry_sdk.set_tag("task_id", "0")
sentry_sdk.capture_exception(Exception("something wrong!"))
assert len(events) == 3
events.sort(key=lambda e: e["tags"]["task_id"])
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
]
}
assert events[1]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
{"flag": "other", "result": False},
]
}
assert events[2]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
{"flag": "world", "result": False},
]
}
def test_flag_tracking():
"""Assert the ring buffer works."""
buffer = FlagBuffer(capacity=3)
buffer.set("a", True)
flags = buffer.get()
assert len(flags) == 1
assert flags == [{"flag": "a", "result": True}]
buffer.set("b", True)
flags = buffer.get()
assert len(flags) == 2
assert flags == [{"flag": "a", "result": True}, {"flag": "b", "result": True}]
buffer.set("c", True)
flags = buffer.get()
assert len(flags) == 3
assert flags == [
{"flag": "a", "result": True},
{"flag": "b", "result": True},
{"flag": "c", "result": True},
]
buffer.set("d", False)
flags = buffer.get()
assert len(flags) == 3
assert flags == [
{"flag": "b", "result": True},
{"flag": "c", "result": True},
{"flag": "d", "result": False},
]
buffer.set("e", False)
buffer.set("f", False)
flags = buffer.get()
assert len(flags) == 3
assert flags == [
{"flag": "d", "result": False},
{"flag": "e", "result": False},
{"flag": "f", "result": False},
]
# Test updates
buffer.set("e", True)
buffer.set("e", False)
buffer.set("e", True)
flags = buffer.get()
assert flags == [
{"flag": "d", "result": False},
{"flag": "f", "result": False},
{"flag": "e", "result": True},
]
buffer.set("d", True)
flags = buffer.get()
assert flags == [
{"flag": "f", "result": False},
{"flag": "e", "result": True},
{"flag": "d", "result": True},
]
def test_flag_buffer_concurrent_access():
buffer = FlagBuffer(capacity=100)
error_occurred = False
def writer():
for i in range(1_000_000):
buffer.set(f"key_{i}", True)
def reader():
nonlocal error_occurred
try:
for _ in range(1000):
copy.deepcopy(buffer)
except RuntimeError:
error_occurred = True
writer_thread = threading.Thread(target=writer)
reader_thread = threading.Thread(target=reader)
writer_thread.start()
reader_thread.start()
writer_thread.join(timeout=5)
reader_thread.join(timeout=5)
# This should always be false. If this ever fails we know we have concurrent access to a
# shared resource. When deepcopying we should have exclusive access to the underlying
# memory.
assert error_occurred is False
def test_flag_limit(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
with start_transaction(name="hi"):
with start_span(op="foo", name="bar"):
add_feature_flag("0", True)
add_feature_flag("1", True)
add_feature_flag("2", True)
add_feature_flag("3", True)
add_feature_flag("4", True)
add_feature_flag("5", True)
add_feature_flag("6", True)
add_feature_flag("7", True)
add_feature_flag("8", True)
add_feature_flag("9", True)
add_feature_flag("10", True)
(event,) = events
assert event["spans"][0]["data"] == ApproxDict(
{
"flag.evaluation.0": True,
"flag.evaluation.1": True,
"flag.evaluation.2": True,
"flag.evaluation.3": True,
"flag.evaluation.4": True,
"flag.evaluation.5": True,
"flag.evaluation.6": True,
"flag.evaluation.7": True,
"flag.evaluation.8": True,
"flag.evaluation.9": True,
}
)
assert "flag.evaluation.10" not in event["spans"][0]["data"]