-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathflag_utils.py
40 lines (27 loc) · 1 KB
/
flag_utils.py
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
from typing import TYPE_CHECKING
import sentry_sdk
from sentry_sdk._lru_cache import LRUCache
if TYPE_CHECKING:
from typing import TypedDict, Optional
from sentry_sdk._types import Event, ExcInfo
FlagData = TypedDict("FlagData", {"flag": str, "result": bool})
DEFAULT_FLAG_CAPACITY = 100
class FlagBuffer:
def __init__(self, capacity):
# type: (int) -> None
self.buffer = LRUCache(capacity)
self.capacity = capacity
def clear(self):
# type: () -> None
self.buffer = LRUCache(self.capacity)
def get(self):
# type: () -> list[FlagData]
return [{"flag": key, "result": value} for key, value in self.buffer.get_all()]
def set(self, flag, result):
# type: (str, bool) -> None
self.buffer.set(flag, result)
def flag_error_processor(event, exc_info):
# type: (Event, ExcInfo) -> Optional[Event]
scope = sentry_sdk.get_current_scope()
event["contexts"]["flags"] = {"values": scope.flags.get()}
return event