Skip to content

Commit 0a75f7e

Browse files
committed
Revert LD changes
1 parent b51d798 commit 0a75f7e

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

sentry_sdk/integrations/launchdarkly.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,39 @@
44
from sentry_sdk.integrations import DidNotEnable, Integration
55
from sentry_sdk.flag_utils import flag_error_processor
66

7-
if TYPE_CHECKING:
8-
from typing import Any, Optional
9-
107
try:
8+
import ldclient
119
from ldclient.hook import Hook, Metadata
1210

1311
if TYPE_CHECKING:
1412
from ldclient import LDClient
1513
from ldclient.hook import EvaluationSeriesContext
1614
from ldclient.evaluation import EvaluationDetail
15+
16+
from typing import Any
1717
except ImportError:
1818
raise DidNotEnable("LaunchDarkly is not installed")
1919

2020

2121
class LaunchDarklyIntegration(Integration):
2222
identifier = "launchdarkly"
23-
_client = None # type: Optional[LDClient]
23+
_ld_client = None # type: LDClient | None
2424

25-
def __init__(self, client):
26-
# type: (LDClient) -> None
25+
def __init__(self, ld_client=None):
26+
# type: (LDClient | None) -> None
2727
"""
28-
:param client: An initialized LDClient instance.
28+
:param client: An initialized LDClient instance. If a client is not provided, this
29+
integration will attempt to use the shared global instance.
2930
"""
30-
self.__class__._client = client
31+
self.__class__._ld_client = ld_client
3132

3233
@staticmethod
3334
def setup_once():
3435
# type: () -> None
35-
client = LaunchDarklyIntegration._client
36-
if not client:
37-
raise DidNotEnable("Error getting LDClient instance")
36+
try:
37+
client = LaunchDarklyIntegration._ld_client or ldclient.get()
38+
except Exception as exc:
39+
raise DidNotEnable("Error getting LaunchDarkly client. " + repr(exc))
3840

3941
# Register the flag collection hook with the LD client.
4042
client.add_hook(LaunchDarklyHook())

tests/integrations/launchdarkly/test_launchdarkly.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from ldclient.integrations.test_data import TestData
1111

1212
import sentry_sdk
13+
from sentry_sdk.integrations import DidNotEnable
1314
from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration
1415

1516

@@ -26,10 +27,11 @@ def test_launchdarkly_integration(
2627
uninstall_integration(LaunchDarklyIntegration.identifier)
2728
if use_global_client:
2829
ldclient.set_config(config)
30+
sentry_init(integrations=[LaunchDarklyIntegration()])
2931
client = ldclient.get()
3032
else:
3133
client = LDClient(config=config)
32-
sentry_init(integrations=[LaunchDarklyIntegration(client)])
34+
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
3335

3436
# Set test values
3537
td.update(td.flag("hello").variation_for_all(True))
@@ -61,7 +63,7 @@ def test_launchdarkly_integration_threaded(
6163
context = Context.create("user1")
6264

6365
uninstall_integration(LaunchDarklyIntegration.identifier)
64-
sentry_init(integrations=[LaunchDarklyIntegration(client)])
66+
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
6567
events = capture_events()
6668

6769
def task(flag_key):
@@ -120,7 +122,7 @@ def test_launchdarkly_integration_asyncio(
120122
context = Context.create("user1")
121123

122124
uninstall_integration(LaunchDarklyIntegration.identifier)
123-
sentry_init(integrations=[LaunchDarklyIntegration(client)])
125+
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
124126
events = capture_events()
125127

126128
async def task(flag_key):
@@ -164,3 +166,23 @@ async def runner():
164166
{"flag": "world", "result": False},
165167
]
166168
}
169+
170+
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+
179+
ldclient._reset_client()
180+
try:
181+
ldclient.__lock.lock()
182+
ldclient.__config = None
183+
finally:
184+
ldclient.__lock.unlock()
185+
186+
uninstall_integration(LaunchDarklyIntegration.identifier)
187+
with pytest.raises(DidNotEnable):
188+
sentry_init(integrations=[LaunchDarklyIntegration()])

0 commit comments

Comments
 (0)