PHP 8.3.31
Preview: feature_flags.py Size: 2.50 KB
//proc/self/root/opt/hc_python/lib/python3.12/site-packages/sentry_sdk/feature_flags.py

import copy
from threading import Lock
from typing import TYPE_CHECKING, Any

import sentry_sdk
from sentry_sdk._lru_cache import LRUCache
from sentry_sdk.tracing import Span
from sentry_sdk.tracing_utils import has_span_streaming_enabled

if TYPE_CHECKING:
    from typing import TypedDict

    FlagData = TypedDict("FlagData", {"flag": str, "result": bool})


DEFAULT_FLAG_CAPACITY = 100


class FlagBuffer:
    def __init__(self, capacity: int) -> None:
        self.capacity = capacity
        self.lock = Lock()

        # Buffer is private. The name is mangled to discourage use. If you use this attribute
        # directly you're on your own!
        self.__buffer = LRUCache(capacity)

    def clear(self) -> None:
        self.__buffer = LRUCache(self.capacity)

    def __deepcopy__(self, memo: "dict[int, Any]") -> "FlagBuffer":
        with self.lock:
            buffer = FlagBuffer(self.capacity)
            buffer.__buffer = copy.deepcopy(self.__buffer, memo)
            return buffer

    def get(self) -> "list[FlagData]":
        with self.lock:
            return [
                {"flag": key, "result": value} for key, value in self.__buffer.get_all()
            ]

    def set(self, flag: str, result: bool) -> None:
        if isinstance(result, FlagBuffer):
            # If someone were to insert `self` into `self` this would create a circular dependency
            # on the lock. This is of course a deadlock. However, this is far outside the expected
            # usage of this class. We guard against it here for completeness and to document this
            # expected failure mode.
            raise ValueError(
                "FlagBuffer instances can not be inserted into the dictionary."
            )

        with self.lock:
            self.__buffer.set(flag, result)


def add_feature_flag(flag: str, result: bool) -> None:
    """
    Records a flag and its value to be sent on subsequent error events.
    We recommend you do this on flag evaluations. Flags are buffered per Sentry scope.
    """
    client = sentry_sdk.get_client()

    flags = sentry_sdk.get_isolation_scope().flags
    flags.set(flag, result)

    if has_span_streaming_enabled(client.options):
        span = sentry_sdk.traces.get_current_span()
        if span and isinstance(span, sentry_sdk.traces.StreamedSpan):
            span.set_attribute(f"flag.evaluation.{flag}", result)

    else:
        span = sentry_sdk.get_current_span()
        if span and isinstance(span, Span):
            span.set_flag(f"flag.evaluation.{flag}", result)

Directory Contents

Dirs: 5 × Files: 36

Name Size Perms Modified Actions
ai DIR
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
crons DIR
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
profiler DIR
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
15.59 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
2.95 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
49.95 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
61.95 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
959 B lrw-r--r-- 2026-06-11 06:30:30
Edit Download
9.37 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
2.50 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
24.54 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
2.60 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.42 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
4.47 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
0 B lrw-r--r-- 2026-06-11 06:30:30
Edit Download
74.09 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
5.99 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
12.82 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
5.08 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
8.59 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
11.85 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
25.08 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
50.33 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
54.36 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
44.41 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.24 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
65.96 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
10.91 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
5.70 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
3.00 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
2.43 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.88 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.14 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.21 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
10.98 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
8.12 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
13.16 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
3.85 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.46 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).