Skip to content

Commit c3516db

Browse files
aliu39antonpirker
andauthored
ref(flags): register LD hook in setup instead of init, and don't check for initialization (#3890)
--------- Co-authored-by: Anton Pirker <[email protected]>
1 parent 60fb6fc commit c3516db

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

sentry_sdk/integrations/launchdarkly.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@
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()
3238
except Exception as exc:
3339
raise DidNotEnable("Error getting LaunchDarkly client. " + repr(exc))
3440

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

41-
@staticmethod
42-
def setup_once():
43-
# type: () -> None
4444
scope = sentry_sdk.get_current_scope()
4545
scope.add_error_processor(flag_error_processor)
4646

tests/integrations/launchdarkly/test_launchdarkly.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,21 @@ 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+
"""
173+
Setup should fail when using global client and ldclient.set_config wasn't called.
174+
175+
We're accessing ldclient internals to set up this test, so it might break if launchdarkly's
176+
implementation changes.
177+
"""
178+
175179
ldclient._reset_client()
176180
try:
177181
ldclient.__lock.lock()
178182
ldclient.__config = None
179183
finally:
180184
ldclient.__lock.unlock()
181185

186+
uninstall_integration(LaunchDarklyIntegration.identifier)
182187
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)
188+
sentry_init(integrations=[LaunchDarklyIntegration()])

0 commit comments

Comments
 (0)