From 58e412eeb81bd80b61fee6ea491db3c61cd010a7 Mon Sep 17 00:00:00 2001 From: Andrew Liu <159852527+aliu39@users.noreply.github.com> Date: Thu, 26 Dec 2024 17:38:48 -0800 Subject: [PATCH] OF client isolation test --- sentry_sdk/integrations/openfeature.py | 1 + .../openfeature/test_openfeature.py | 48 ++++++++++++++++--- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/openfeature.py b/sentry_sdk/integrations/openfeature.py index e62632f72b..525330e1d3 100644 --- a/sentry_sdk/integrations/openfeature.py +++ b/sentry_sdk/integrations/openfeature.py @@ -35,6 +35,7 @@ def setup_once(): if client: # Register the hook within the openfeature client. client.add_hooks(hooks=[OpenFeatureHook()]) + print("added hook to", client) else: # Register the hook within the global openfeature hooks list. api.add_hooks(hooks=[OpenFeatureHook()]) diff --git a/tests/integrations/openfeature/test_openfeature.py b/tests/integrations/openfeature/test_openfeature.py index 29a11e333d..f37ddd79dc 100644 --- a/tests/integrations/openfeature/test_openfeature.py +++ b/tests/integrations/openfeature/test_openfeature.py @@ -1,6 +1,5 @@ import concurrent.futures as cf import sys - import pytest from openfeature import api @@ -10,12 +9,22 @@ from sentry_sdk.integrations.openfeature import OpenFeatureIntegration +@pytest.fixture +def reset_openfeature(uninstall_integration): + yield + + # Teardown + uninstall_integration(OpenFeatureIntegration.identifier) + api.clear_hooks() + api.shutdown() # provider clean up + + @pytest.mark.parametrize( "use_global_client", (False, True), ) def test_openfeature_integration( - sentry_init, use_global_client, capture_events, uninstall_integration + sentry_init, use_global_client, capture_events, reset_openfeature ): flags = { "hello": InMemoryFlag("on", {"on": True, "off": False}), @@ -24,7 +33,6 @@ def test_openfeature_integration( api.set_provider(InMemoryProvider(flags)) client = api.get_client() - uninstall_integration(OpenFeatureIntegration.identifier) if use_global_client: sentry_init(integrations=[OpenFeatureIntegration()]) else: @@ -48,7 +56,7 @@ def test_openfeature_integration( def test_openfeature_integration_threaded( - sentry_init, capture_events, uninstall_integration + sentry_init, capture_events, reset_openfeature ): flags = { "hello": InMemoryFlag("on", {"on": True, "off": False}), @@ -57,7 +65,6 @@ def test_openfeature_integration_threaded( api.set_provider(InMemoryProvider(flags)) client = api.get_client() - uninstall_integration(OpenFeatureIntegration.identifier) sentry_init(integrations=[OpenFeatureIntegration(client=client)]) events = capture_events() @@ -104,7 +111,7 @@ def task(flag): @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") def test_openfeature_integration_asyncio( - sentry_init, capture_events, uninstall_integration + sentry_init, capture_events, reset_openfeature ): """Assert concurrently evaluated flags do not pollute one another.""" @@ -117,7 +124,6 @@ def test_openfeature_integration_asyncio( api.set_provider(InMemoryProvider(flags)) client = api.get_client() - uninstall_integration(OpenFeatureIntegration.identifier) sentry_init(integrations=[OpenFeatureIntegration(client=client)]) events = capture_events() @@ -160,3 +166,31 @@ async def runner(): {"flag": "world", "result": False}, ] } + + +def test_openfeature_integration_client_isolation( + sentry_init, capture_events, reset_openfeature +): + """ + If the integration is tracking a single client, evaluations from other clients should not be + captured. + """ + flags = { + "hello": InMemoryFlag("on", {"on": True, "off": False}), + "world": InMemoryFlag("off", {"on": True, "off": False}), + } + api.set_provider(InMemoryProvider(flags)) + client = api.get_client() + sentry_init(integrations=[OpenFeatureIntegration(client=client)]) + + other_client = api.get_client() + other_client.get_boolean_value("hello", default_value=False) + other_client.get_boolean_value("world", default_value=False) + other_client.get_boolean_value("other", default_value=True) + + events = capture_events() + sentry_sdk.set_tag("apple", "0") + sentry_sdk.capture_exception(Exception("something wrong!")) + + assert len(events) == 1 + assert events[0]["contexts"]["flags"] == {"values": []}