Skip to content

Commit 9ca9f8f

Browse files
committed
Register LD hook in setup and don't check for initialization
1 parent f6281f5 commit 9ca9f8f

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

sentry_sdk/integrations/launchdarkly.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,28 @@
2020

2121
class LaunchDarklyIntegration(Integration):
2222
identifier = "launchdarkly"
23+
_ld_client = None # type: LDClient | None
2324

2425
def __init__(self, ld_client=None):
2526
# type: (LDClient | None) -> None
2627
"""
2728
:param client: An initialized LDClient instance. If a client is not provided, this
2829
integration will attempt to use the shared global instance.
2930
"""
31+
self.__class__._ld_client = ld_client
32+
33+
@staticmethod
34+
def setup_once():
35+
# type: () -> None
3036
try:
31-
client = ld_client or ldclient.get()
37+
client = LaunchDarklyIntegration._ld_client or ldclient.get()
38+
print("got client")
3239
except Exception as exc:
3340
raise DidNotEnable("Error getting LaunchDarkly client. " + repr(exc))
3441

35-
if not client.is_initialized():
36-
raise DidNotEnable("LaunchDarkly client is not initialized.")
37-
3842
# Register the flag collection hook with the LD client.
3943
client.add_hook(LaunchDarklyHook())
4044

41-
@staticmethod
42-
def setup_once():
43-
# type: () -> None
4445
scope = sentry_sdk.get_current_scope()
4546
scope.add_error_processor(flag_error_processor)
4647

tests/integrations/launchdarkly/test_launchdarkly.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,15 @@ async def runner():
168168
}
169169

170170

171-
def test_launchdarkly_integration_did_not_enable(monkeypatch):
172-
# Client is not passed in and set_config wasn't called.
173-
# TODO: Bad practice to access internals like this. We can skip this test, or remove this
174-
# case entirely (force user to pass in a client instance).
171+
def test_launchdarkly_integration_did_not_enable(sentry_init, uninstall_integration):
172+
# Using global client and set_config wasn't called.
175173
ldclient._reset_client()
176174
try:
177175
ldclient.__lock.lock()
178176
ldclient.__config = None
179177
finally:
180178
ldclient.__lock.unlock()
181179

180+
uninstall_integration(LaunchDarklyIntegration.identifier)
182181
with pytest.raises(DidNotEnable):
183-
LaunchDarklyIntegration()
184-
185-
# Client not initialized.
186-
client = LDClient(config=Config("sdk-key"))
187-
monkeypatch.setattr(client, "is_initialized", lambda: False)
188-
with pytest.raises(DidNotEnable):
189-
LaunchDarklyIntegration(ld_client=client)
182+
sentry_init(integrations=[LaunchDarklyIntegration()])

0 commit comments

Comments
 (0)