Skip to content

Commit 711fe55

Browse files
committed
Revert "Remove integration init"
This reverts commit ead840f.
1 parent a3d90bd commit 711fe55

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

sentry_sdk/integrations/launchdarkly.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ldclient.hook import Hook, Metadata
1010

1111
if TYPE_CHECKING:
12+
from ldclient import LDClient
1213
from ldclient.hook import EvaluationSeriesContext
1314
from ldclient.evaluation import EvaluationDetail
1415

@@ -20,11 +21,14 @@
2021
class LaunchDarklyIntegration(Integration):
2122
identifier = "launchdarkly"
2223

23-
@staticmethod
24-
def setup_once():
25-
# type: () -> None
24+
def __init__(self, ld_client=None):
25+
# type: (LDClient | None) -> None
26+
"""
27+
:param client: An initialized LDClient instance. If a client is not provided, this
28+
integration will attempt to use the shared global instance.
29+
"""
2630
try:
27-
client = ldclient.get()
31+
client = ld_client or ldclient.get()
2832
except Exception as exc:
2933
raise DidNotEnable("Error getting LaunchDarkly client. " + repr(exc))
3034

@@ -34,6 +38,9 @@ def setup_once():
3438
# Register the flag collection hook with the LD client.
3539
client.add_hook(LaunchDarklyHook())
3640

41+
@staticmethod
42+
def setup_once():
43+
# type: () -> None
3744
scope = sentry_sdk.get_current_scope()
3845
scope.add_error_processor(flag_error_processor)
3946

tests/integrations/launchdarkly/test_launchdarkly.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@
44
import ldclient
55

66
import sentry_sdk
7+
import pytest
78

9+
from ldclient import LDClient
810
from ldclient.config import Config
911
from ldclient.context import Context
1012
from ldclient.integrations.test_data import TestData
1113

14+
from sentry_sdk.integrations import DidNotEnable
1215
from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration
1316

1417

15-
def test_launchdarkly_integration(sentry_init):
18+
@pytest.mark.parametrize(
19+
"use_global_client",
20+
(False, True),
21+
)
22+
def test_launchdarkly_integration(sentry_init, use_global_client):
1623
td = TestData.data_source()
1724
config = Config("sdk-key", update_processor_class=td)
18-
ldclient.set_config(config)
19-
client = ldclient.get()
20-
sentry_init(integrations=[LaunchDarklyIntegration()])
25+
if use_global_client:
26+
ldclient.set_config(config)
27+
sentry_init(integrations=[LaunchDarklyIntegration()])
28+
client = ldclient.get()
29+
else:
30+
client = LDClient(config=config)
31+
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
2132

2233
# Set test values
2334
td.update(td.flag("hello").variation_for_all(True))
@@ -39,8 +50,8 @@ def test_launchdarkly_integration(sentry_init):
3950

4051
def test_launchdarkly_integration_threaded(sentry_init):
4152
td = TestData.data_source()
42-
client = ldclient.get()
43-
sentry_init(integrations=[LaunchDarklyIntegration()])
53+
client = LDClient(config=Config("sdk-key", update_processor_class=td))
54+
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
4455
context = Context.create("user1")
4556

4657
def task(flag_key):
@@ -67,8 +78,8 @@ def task(flag_key):
6778
def test_launchdarkly_integration_asyncio(sentry_init):
6879
"""Assert concurrently evaluated flags do not pollute one another."""
6980
td = TestData.data_source()
70-
client = ldclient.get()
71-
sentry_init(integrations=[LaunchDarklyIntegration()])
81+
client = LDClient(config=Config("sdk-key", update_processor_class=td))
82+
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
7283
context = Context.create("user1")
7384

7485
async def task(flag_key):
@@ -87,4 +98,23 @@ async def runner():
8798
assert results[0] == ["hello", "world"]
8899
assert results[1] == ["hello", "other"]
89100

101+
102+
def test_launchdarkly_integration_did_not_enable(monkeypatch):
103+
# Client is not passed in and set_config wasn't called.
104+
# TODO: Bad practice to access internals like this. We can skip this test, or remove this
105+
# case entirely (force user to pass in a client instance).
90106
ldclient._reset_client()
107+
try:
108+
ldclient.__lock.lock()
109+
ldclient.__config = None
110+
finally:
111+
ldclient.__lock.unlock()
112+
113+
with pytest.raises(DidNotEnable):
114+
LaunchDarklyIntegration()
115+
116+
# Client not initialized.
117+
client = LDClient(config=Config("sdk-key"))
118+
monkeypatch.setattr(client, "is_initialized", lambda: False)
119+
with pytest.raises(DidNotEnable):
120+
LaunchDarklyIntegration(ld_client=client)

0 commit comments

Comments
 (0)