diff --git a/.github/workflows/test-integrations-miscellaneous.yml b/.github/workflows/test-integrations-miscellaneous.yml index 0b49a27219..88a576505e 100644 --- a/.github/workflows/test-integrations-miscellaneous.yml +++ b/.github/workflows/test-integrations-miscellaneous.yml @@ -45,10 +45,18 @@ jobs: - name: Erase coverage run: | coverage erase + - name: Test launchdarkly latest + run: | + set -x # print commands that are executed + ./scripts/runtox.sh "py${{ matrix.python-version }}-launchdarkly-latest" - name: Test loguru latest run: | set -x # print commands that are executed ./scripts/runtox.sh "py${{ matrix.python-version }}-loguru-latest" + - name: Test openfeature latest + run: | + set -x # print commands that are executed + ./scripts/runtox.sh "py${{ matrix.python-version }}-openfeature-latest" - name: Test opentelemetry latest run: | set -x # print commands that are executed @@ -117,10 +125,18 @@ jobs: - name: Erase coverage run: | coverage erase + - name: Test launchdarkly pinned + run: | + set -x # print commands that are executed + ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-launchdarkly" - name: Test loguru pinned run: | set -x # print commands that are executed ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-loguru" + - name: Test openfeature pinned + run: | + set -x # print commands that are executed + ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-openfeature" - name: Test opentelemetry pinned run: | set -x # print commands that are executed diff --git a/mypy.ini b/mypy.ini index bacba96ceb..63fa7f334f 100644 --- a/mypy.ini +++ b/mypy.ini @@ -74,6 +74,8 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-openai.*] ignore_missing_imports = True +[mypy-openfeature.*] +ignore_missing_imports = True [mypy-huggingface_hub.*] ignore_missing_imports = True [mypy-arq.*] diff --git a/requirements-linting.txt b/requirements-linting.txt index d2a65b31db..c9d4bd7f5c 100644 --- a/requirements-linting.txt +++ b/requirements-linting.txt @@ -15,3 +15,5 @@ flake8-bugbear pep8-naming pre-commit # local linting httpcore +openfeature-sdk +launchdarkly-server-sdk diff --git a/scripts/split-tox-gh-actions/split-tox-gh-actions.py b/scripts/split-tox-gh-actions/split-tox-gh-actions.py index 7ed2505f40..c0bf2a7a09 100755 --- a/scripts/split-tox-gh-actions/split-tox-gh-actions.py +++ b/scripts/split-tox-gh-actions/split-tox-gh-actions.py @@ -125,7 +125,9 @@ "tornado", ], "Miscellaneous": [ + "launchdarkly", "loguru", + "openfeature", "opentelemetry", "potel", "pure_eval", diff --git a/sentry_sdk/_lru_cache.py b/sentry_sdk/_lru_cache.py index 37e86e5fe3..ec557b1093 100644 --- a/sentry_sdk/_lru_cache.py +++ b/sentry_sdk/_lru_cache.py @@ -62,6 +62,8 @@ """ +from copy import copy + SENTINEL = object() @@ -89,6 +91,13 @@ def __init__(self, max_size): self.hits = self.misses = 0 + def __copy__(self): + cache = LRUCache(self.max_size) + cache.full = self.full + cache.cache = copy(self.cache) + cache.root = copy(self.root) + return cache + def set(self, key, value): link = self.cache.get(key, SENTINEL) @@ -154,3 +163,11 @@ def get(self, key, default=None): self.hits += 1 return link[VALUE] + + def get_all(self): + nodes = [] + node = self.root[NEXT] + while node is not self.root: + nodes.append((node[KEY], node[VALUE])) + node = node[NEXT] + return nodes diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index 6791abeb0e..fdb20caadf 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -59,6 +59,7 @@ class CompressionAlgo(Enum): "Experiments", { "max_spans": Optional[int], + "max_flags": Optional[int], "record_sql_params": Optional[bool], "continuous_profiling_auto_start": Optional[bool], "continuous_profiling_mode": Optional[ContinuousProfilerMode], diff --git a/sentry_sdk/flag_utils.py b/sentry_sdk/flag_utils.py new file mode 100644 index 0000000000..2b345a7f0b --- /dev/null +++ b/sentry_sdk/flag_utils.py @@ -0,0 +1,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 diff --git a/sentry_sdk/integrations/launchdarkly.py b/sentry_sdk/integrations/launchdarkly.py new file mode 100644 index 0000000000..9e00e12ede --- /dev/null +++ b/sentry_sdk/integrations/launchdarkly.py @@ -0,0 +1,64 @@ +from typing import TYPE_CHECKING +import sentry_sdk + +from sentry_sdk.integrations import DidNotEnable, Integration +from sentry_sdk.flag_utils import flag_error_processor + +try: + import ldclient + from ldclient.hook import Hook, Metadata + + if TYPE_CHECKING: + from ldclient import LDClient + from ldclient.hook import EvaluationSeriesContext + from ldclient.evaluation import EvaluationDetail + + from typing import Any +except ImportError: + raise DidNotEnable("LaunchDarkly is not installed") + + +class LaunchDarklyIntegration(Integration): + identifier = "launchdarkly" + + def __init__(self, ld_client=None): + # type: (LDClient | None) -> None + """ + :param client: An initialized LDClient instance. If a client is not provided, this + integration will attempt to use the shared global instance. + """ + try: + client = ld_client or ldclient.get() + except Exception as exc: + raise DidNotEnable("Error getting LaunchDarkly client. " + repr(exc)) + + if not client.is_initialized(): + raise DidNotEnable("LaunchDarkly client is not initialized.") + + # Register the flag collection hook with the LD client. + client.add_hook(LaunchDarklyHook()) + + @staticmethod + def setup_once(): + # type: () -> None + scope = sentry_sdk.get_current_scope() + scope.add_error_processor(flag_error_processor) + + +class LaunchDarklyHook(Hook): + + @property + def metadata(self): + # type: () -> Metadata + return Metadata(name="sentry-feature-flag-recorder") + + def after_evaluation(self, series_context, data, detail): + # type: (EvaluationSeriesContext, dict[Any, Any], EvaluationDetail) -> dict[Any, Any] + if isinstance(detail.value, bool): + flags = sentry_sdk.get_current_scope().flags + flags.set(series_context.key, detail.value) + return data + + def before_evaluation(self, series_context, data): + # type: (EvaluationSeriesContext, dict[Any, Any]) -> dict[Any, Any] + return data # No-op. diff --git a/sentry_sdk/integrations/openfeature.py b/sentry_sdk/integrations/openfeature.py new file mode 100644 index 0000000000..18f968a703 --- /dev/null +++ b/sentry_sdk/integrations/openfeature.py @@ -0,0 +1,43 @@ +from typing import TYPE_CHECKING +import sentry_sdk + +from sentry_sdk.integrations import DidNotEnable, Integration +from sentry_sdk.flag_utils import flag_error_processor + +try: + from openfeature import api + from openfeature.hook import Hook + + if TYPE_CHECKING: + from openfeature.flag_evaluation import FlagEvaluationDetails + from openfeature.hook import HookContext, HookHints +except ImportError: + raise DidNotEnable("OpenFeature is not installed") + + +class OpenFeatureIntegration(Integration): + identifier = "openfeature" + + @staticmethod + def setup_once(): + # type: () -> None + scope = sentry_sdk.get_current_scope() + scope.add_error_processor(flag_error_processor) + + # Register the hook within the global openfeature hooks list. + api.add_hooks(hooks=[OpenFeatureHook()]) + + +class OpenFeatureHook(Hook): + + def after(self, hook_context, details, hints): + # type: (HookContext, FlagEvaluationDetails[bool], HookHints) -> None + if isinstance(details.value, bool): + flags = sentry_sdk.get_current_scope().flags + flags.set(details.flag_key, details.value) + + def error(self, hook_context, exception, hints): + # type: (HookContext, Exception, HookHints) -> None + if isinstance(hook_context.default_value, bool): + flags = sentry_sdk.get_current_scope().flags + flags.set(hook_context.flag_key, hook_context.default_value) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 0c0482904e..34ccc7f940 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -11,6 +11,7 @@ from sentry_sdk.attachments import Attachment from sentry_sdk.consts import DEFAULT_MAX_BREADCRUMBS, FALSE_VALUES, INSTRUMENTER +from sentry_sdk.flag_utils import FlagBuffer, DEFAULT_FLAG_CAPACITY from sentry_sdk.profiler.continuous_profiler import try_autostart_continuous_profiler from sentry_sdk.profiler.transaction_profiler import Profile from sentry_sdk.session import Session @@ -192,6 +193,7 @@ class Scope: "client", "_type", "_last_event_id", + "_flags", ) def __init__(self, ty=None, client=None): @@ -249,6 +251,8 @@ def __copy__(self): rv._last_event_id = self._last_event_id + rv._flags = copy(self._flags) + return rv @classmethod @@ -685,6 +689,7 @@ def clear(self): # self._last_event_id is only applicable to isolation scopes self._last_event_id = None # type: Optional[str] + self._flags = None # type: Optional[FlagBuffer] @_attr_setter def level(self, value): @@ -1546,6 +1551,17 @@ def __repr__(self): self._type, ) + @property + def flags(self): + # type: () -> FlagBuffer + if self._flags is None: + max_flags = ( + self.get_client().options["_experiments"].get("max_flags") + or DEFAULT_FLAG_CAPACITY + ) + self._flags = FlagBuffer(capacity=max_flags) + return self._flags + @contextmanager def new_scope(): diff --git a/setup.py b/setup.py index e9c83eb1fa..e5e0c8eaa4 100644 --- a/setup.py +++ b/setup.py @@ -63,9 +63,11 @@ def get_file_text(file_name): "huey": ["huey>=2"], "huggingface_hub": ["huggingface_hub>=0.22"], "langchain": ["langchain>=0.0.210"], + "launchdarkly": ["launchdarkly-server-sdk>=9.8.0"], "litestar": ["litestar>=2.0.0"], "loguru": ["loguru>=0.5"], "openai": ["openai>=1.0.0", "tiktoken>=0.3.0"], + "openfeature": ["openfeature-sdk>=0.7.1"], "opentelemetry": ["opentelemetry-distro>=0.35b0"], "opentelemetry-experimental": ["opentelemetry-distro"], "pure_eval": ["pure_eval", "executing", "asttokens"], diff --git a/tests/integrations/launchdarkly/__init__.py b/tests/integrations/launchdarkly/__init__.py new file mode 100644 index 0000000000..06e09884c8 --- /dev/null +++ b/tests/integrations/launchdarkly/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("ldclient") diff --git a/tests/integrations/launchdarkly/test_launchdarkly.py b/tests/integrations/launchdarkly/test_launchdarkly.py new file mode 100644 index 0000000000..acbe764104 --- /dev/null +++ b/tests/integrations/launchdarkly/test_launchdarkly.py @@ -0,0 +1,116 @@ +import asyncio +import concurrent.futures as cf + +import ldclient + +import sentry_sdk +import pytest + +from ldclient import LDClient +from ldclient.config import Config +from ldclient.context import Context +from ldclient.integrations.test_data import TestData + +from sentry_sdk.integrations import DidNotEnable +from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration + + +@pytest.mark.parametrize( + "use_global_client", + (False, True), +) +def test_launchdarkly_integration(sentry_init, use_global_client): + td = TestData.data_source() + config = Config("sdk-key", update_processor_class=td) + if use_global_client: + ldclient.set_config(config) + sentry_init(integrations=[LaunchDarklyIntegration()]) + client = ldclient.get() + else: + client = LDClient(config=config) + sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) + + # Set test values + td.update(td.flag("hello").variation_for_all(True)) + td.update(td.flag("world").variation_for_all(True)) + + # Evaluate + client.variation("hello", Context.create("my-org", "organization"), False) + client.variation("world", Context.create("user1", "user"), False) + client.variation("other", Context.create("user2", "user"), False) + + assert sentry_sdk.get_current_scope().flags.get() == [ + {"flag": "hello", "result": True}, + {"flag": "world", "result": True}, + {"flag": "other", "result": False}, + ] + + +def test_launchdarkly_integration_threaded(sentry_init): + td = TestData.data_source() + client = LDClient(config=Config("sdk-key", update_processor_class=td)) + sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) + context = Context.create("user1") + + 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(): + client.variation(flag_key, context, False) + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] + + td.update(td.flag("hello").variation_for_all(True)) + td.update(td.flag("world").variation_for_all(False)) + # Capture an eval before we split isolation scopes. + client.variation("hello", context, False) + + with cf.ThreadPoolExecutor(max_workers=2) as pool: + results = list(pool.map(task, ["world", "other"])) + + assert results[0] == ["hello", "world"] + assert results[1] == ["hello", "other"] + + +def test_launchdarkly_integration_asyncio(sentry_init): + """Assert concurrently evaluated flags do not pollute one another.""" + td = TestData.data_source() + client = LDClient(config=Config("sdk-key", update_processor_class=td)) + sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) + context = Context.create("user1") + + async def task(flag_key): + with sentry_sdk.isolation_scope(): + client.variation(flag_key, context, False) + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] + + async def runner(): + return asyncio.gather(task("world"), task("other")) + + td.update(td.flag("hello").variation_for_all(True)) + td.update(td.flag("world").variation_for_all(False)) + client.variation("hello", context, False) + + results = asyncio.run(runner()).result() + assert results[0] == ["hello", "world"] + assert results[1] == ["hello", "other"] + + +def test_launchdarkly_integration_did_not_enable(monkeypatch): + # Client is not passed in and set_config wasn't called. + # TODO: Bad practice to access internals like this. We can skip this test, or remove this + # case entirely (force user to pass in a client instance). + ldclient._reset_client() + try: + ldclient.__lock.lock() + ldclient.__config = None + finally: + ldclient.__lock.unlock() + + with pytest.raises(DidNotEnable): + LaunchDarklyIntegration() + + # Client not initialized. + client = LDClient(config=Config("sdk-key")) + monkeypatch.setattr(client, "is_initialized", lambda: False) + with pytest.raises(DidNotEnable): + LaunchDarklyIntegration(ld_client=client) diff --git a/tests/integrations/openfeature/__init__.py b/tests/integrations/openfeature/__init__.py new file mode 100644 index 0000000000..a17549ea79 --- /dev/null +++ b/tests/integrations/openfeature/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("openfeature") diff --git a/tests/integrations/openfeature/test_openfeature.py b/tests/integrations/openfeature/test_openfeature.py new file mode 100644 index 0000000000..24e7857f9a --- /dev/null +++ b/tests/integrations/openfeature/test_openfeature.py @@ -0,0 +1,80 @@ +import asyncio +import concurrent.futures as cf +import sentry_sdk + +from openfeature import api +from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider +from sentry_sdk.integrations.openfeature import OpenFeatureIntegration + + +def test_openfeature_integration(sentry_init): + sentry_init(integrations=[OpenFeatureIntegration()]) + + flags = { + "hello": InMemoryFlag("on", {"on": True, "off": False}), + "world": InMemoryFlag("off", {"on": True, "off": False}), + } + api.set_provider(InMemoryProvider(flags)) + + client = api.get_client() + client.get_boolean_value("hello", default_value=False) + client.get_boolean_value("world", default_value=False) + client.get_boolean_value("other", default_value=True) + + assert sentry_sdk.get_current_scope().flags.get() == [ + {"flag": "hello", "result": True}, + {"flag": "world", "result": False}, + {"flag": "other", "result": True}, + ] + + +def test_openfeature_integration_threaded(sentry_init): + sentry_init(integrations=[OpenFeatureIntegration()]) + + flags = { + "hello": InMemoryFlag("on", {"on": True, "off": False}), + "world": InMemoryFlag("off", {"on": True, "off": False}), + } + api.set_provider(InMemoryProvider(flags)) + + client = api.get_client() + client.get_boolean_value("hello", default_value=False) + + def task(flag): + # Create a new isolation scope for the thread. This means the flags + with sentry_sdk.isolation_scope(): + client.get_boolean_value(flag, default_value=False) + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] + + with cf.ThreadPoolExecutor(max_workers=2) as pool: + results = list(pool.map(task, ["world", "other"])) + + assert results[0] == ["hello", "world"] + assert results[1] == ["hello", "other"] + + +def test_openfeature_integration_asyncio(sentry_init): + """Assert concurrently evaluated flags do not pollute one another.""" + + async def task(flag): + with sentry_sdk.isolation_scope(): + client.get_boolean_value(flag, default_value=False) + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] + + async def runner(): + return asyncio.gather(task("world"), task("other")) + + sentry_init(integrations=[OpenFeatureIntegration()]) + + flags = { + "hello": InMemoryFlag("on", {"on": True, "off": False}), + "world": InMemoryFlag("off", {"on": True, "off": False}), + } + api.set_provider(InMemoryProvider(flags)) + + client = api.get_client() + client.get_boolean_value("hello", default_value=False) + + results = asyncio.run(runner()).result() + assert results[0] == ["hello", "world"] + assert results[1] == ["hello", "other"] diff --git a/tests/test_flag_utils.py b/tests/test_flag_utils.py new file mode 100644 index 0000000000..3fa4f3abfe --- /dev/null +++ b/tests/test_flag_utils.py @@ -0,0 +1,43 @@ +from sentry_sdk.flag_utils import FlagBuffer + + +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}, + ] diff --git a/tests/test_lru_cache.py b/tests/test_lru_cache.py index 5343e76169..3e9c0ac964 100644 --- a/tests/test_lru_cache.py +++ b/tests/test_lru_cache.py @@ -35,3 +35,26 @@ def test_cache_eviction(): cache.set(4, 4) assert cache.get(3) is None assert cache.get(4) == 4 + + +def test_cache_miss(): + cache = LRUCache(1) + assert cache.get(0) is None + + +def test_cache_set_overwrite(): + cache = LRUCache(3) + cache.set(0, 0) + cache.set(0, 1) + assert cache.get(0) == 1 + + +def test_cache_get_all(): + cache = LRUCache(3) + cache.set(0, 0) + cache.set(1, 1) + cache.set(2, 2) + cache.set(3, 3) + assert cache.get_all() == [(1, 1), (2, 2), (3, 3)] + cache.get(1) + assert cache.get_all() == [(2, 2), (3, 3), (1, 1)] diff --git a/tox.ini b/tox.ini index ef30e539b5..f3a7ba4ea0 100644 --- a/tox.ini +++ b/tox.ini @@ -184,6 +184,14 @@ envlist = {py3.9,py3.11,py3.12}-openai-latest {py3.9,py3.11,py3.12}-openai-notiktoken + # OpenFeature + {py3.8,py3.12,py3.13}-openfeature-v0.7 + {py3.8,py3.12,py3.13}-openfeature-latest + + # LaunchDarkly + {py3.8,py3.12,py3.13}-launchdarkly-v9.8.0 + {py3.8,py3.12,py3.13}-launchdarkly-latest + # OpenTelemetry (OTel) {py3.7,py3.9,py3.12,py3.13}-opentelemetry @@ -539,6 +547,14 @@ deps = openai-latest: tiktoken~=0.6.0 openai-notiktoken: openai + # OpenFeature + openfeature-v0.7: openfeature-sdk~=0.7.1 + openfeature-latest: openfeature-sdk + + # LaunchDarkly + launchdarkly-v9.8.0: launchdarkly-server-sdk~=9.8.0 + launchdarkly-latest: launchdarkly-server-sdk + # OpenTelemetry (OTel) opentelemetry: opentelemetry-distro @@ -727,9 +743,11 @@ setenv = huey: TESTPATH=tests/integrations/huey huggingface_hub: TESTPATH=tests/integrations/huggingface_hub langchain: TESTPATH=tests/integrations/langchain + launchdarkly: TESTPATH=tests/integrations/launchdarkly litestar: TESTPATH=tests/integrations/litestar loguru: TESTPATH=tests/integrations/loguru openai: TESTPATH=tests/integrations/openai + openfeature: TESTPATH=tests/integrations/openfeature opentelemetry: TESTPATH=tests/integrations/opentelemetry potel: TESTPATH=tests/integrations/opentelemetry pure_eval: TESTPATH=tests/integrations/pure_eval