Skip to content

Commit

Permalink
Make OFClient optional arg
Browse files Browse the repository at this point in the history
  • Loading branch information
aliu39 committed Dec 27, 2024
1 parent 0a75f7e commit 16917f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
16 changes: 9 additions & 7 deletions sentry_sdk/integrations/openfeature.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Optional

try:
from openfeature import api
from openfeature.hook import Hook

if TYPE_CHECKING:
Expand All @@ -22,20 +23,21 @@ class OpenFeatureIntegration(Integration):
identifier = "openfeature"
_client = None # type: Optional[OpenFeatureClient]

def __init__(self, client):
# type: (OpenFeatureClient) -> None
def __init__(self, client=None):
# type: (Optional[OpenFeatureClient]) -> None
self.__class__._client = client

@staticmethod
def setup_once():
# type: () -> None

client = OpenFeatureIntegration._client
if not client:
raise DidNotEnable("Error getting OpenFeatureClient instance")

# Register the hook within the openfeature client.
client.add_hooks(hooks=[OpenFeatureHook()])
if client:
# Register the hook within the openfeature client.
client.add_hooks(hooks=[OpenFeatureHook()])
else:
# Register the hook within the global openfeature hooks list.
api.add_hooks(hooks=[OpenFeatureHook()])

scope = sentry_sdk.get_current_scope()
scope.add_error_processor(flag_error_processor)
Expand Down
17 changes: 13 additions & 4 deletions tests/integrations/openfeature/test_openfeature.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
from sentry_sdk.integrations.openfeature import OpenFeatureIntegration


def test_openfeature_integration(sentry_init, capture_events, uninstall_integration):
@pytest.mark.parametrize(
"use_global_client",
(False, True),
)
def test_openfeature_integration(
sentry_init, use_global_client, capture_events, uninstall_integration
):
flags = {
"hello": InMemoryFlag("on", {"on": True, "off": False}),
"world": InMemoryFlag("off", {"on": True, "off": False}),
Expand All @@ -19,7 +25,10 @@ def test_openfeature_integration(sentry_init, capture_events, uninstall_integrat
client = api.get_client()

uninstall_integration(OpenFeatureIntegration.identifier)
sentry_init(integrations=[OpenFeatureIntegration(client)])
if use_global_client:
sentry_init(integrations=[OpenFeatureIntegration()])
else:
sentry_init(integrations=[OpenFeatureIntegration(client=client)])

client.get_boolean_value("hello", default_value=False)
client.get_boolean_value("world", default_value=False)
Expand Down Expand Up @@ -49,7 +58,7 @@ def test_openfeature_integration_threaded(
client = api.get_client()

uninstall_integration(OpenFeatureIntegration.identifier)
sentry_init(integrations=[OpenFeatureIntegration(client)])
sentry_init(integrations=[OpenFeatureIntegration(client=client)])
events = capture_events()

# Capture an eval before we split isolation scopes.
Expand Down Expand Up @@ -109,7 +118,7 @@ def test_openfeature_integration_asyncio(
client = api.get_client()

uninstall_integration(OpenFeatureIntegration.identifier)
sentry_init(integrations=[OpenFeatureIntegration(client)])
sentry_init(integrations=[OpenFeatureIntegration(client=client)])
events = capture_events()

# Capture an eval before we split isolation scopes.
Expand Down

0 comments on commit 16917f7

Please sign in to comment.