Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "ref(flags): register LD hook in setup instead of init, and don't check for initialization" #3900

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions sentry_sdk/integrations/launchdarkly.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@

class LaunchDarklyIntegration(Integration):
identifier = "launchdarkly"
_ld_client = None # type: LDClient | None

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.
"""
self.__class__._ld_client = ld_client

@staticmethod
def setup_once():
# type: () -> None
try:
client = LaunchDarklyIntegration._ld_client or ldclient.get()
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)

Expand Down
21 changes: 11 additions & 10 deletions tests/integrations/launchdarkly/test_launchdarkly.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,22 @@ async def runner():
}


def test_launchdarkly_integration_did_not_enable(sentry_init, uninstall_integration):
"""
Setup should fail when using global client and ldclient.set_config wasn't called.

We're accessing ldclient internals to set up this test, so it might break if launchdarkly's
implementation changes.
"""

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()

uninstall_integration(LaunchDarklyIntegration.identifier)
with pytest.raises(DidNotEnable):
sentry_init(integrations=[LaunchDarklyIntegration()])
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)
Loading