File: flag_utils.py

package info (click to toggle)
sentry-python 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,004 kB
  • sloc: python: 55,908; makefile: 114; sh: 111; xml: 2
file content (47 lines) | stat: -rw-r--r-- 1,224 bytes parent folder | download
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
from copy import copy
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 __copy__(self):
        # type: () -> FlagBuffer
        buffer = FlagBuffer(capacity=self.capacity)
        buffer.buffer = copy(self.buffer)
        return buffer

    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