Skip to content

Commit 5165ffb

Browse files
committed
Pass in client to Integration initializer and basic unit test
1 parent a2e3383 commit 5165ffb

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

sentry_sdk/integrations/launchdarkly.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
raise DidNotEnable("LaunchDarkly is not installed")
1919

2020

21-
def _get_ldclient():
22-
# type: () -> LDClient
23-
try:
24-
client = ldclient.get()
25-
except Exception as exc:
26-
sentry_sdk.capture_exception(exc)
27-
raise DidNotEnable("Error getting LaunchDarkly client. " + str(exc))
21+
class LaunchDarklyIntegration(Integration):
22+
identifier = "launchdarkly"
2823

29-
if client and client.is_initialized():
30-
return client
31-
raise DidNotEnable("LaunchDarkly client is not initialized")
24+
def __init__(self, client=None):
25+
# type: (LDClient | None) -> None
26+
if client is None:
27+
try:
28+
client = ldclient.get() # global singleton
29+
except Exception as exc:
30+
raise DidNotEnable("Error getting LaunchDarkly client. " + repr(exc))
3231

32+
if not client.is_initialized():
33+
raise DidNotEnable("LaunchDarkly client is not initialized")
3334

34-
class LaunchDarklyIntegration(Integration):
35-
identifier = "launchdarkly"
35+
# Register the flag collection hook with the given client.
36+
client.add_hook(LaunchDarklyHook())
3637

3738
@staticmethod
3839
def setup_once():
@@ -46,10 +47,6 @@ def error_processor(event, _exc_info):
4647
scope = sentry_sdk.get_current_scope()
4748
scope.add_error_processor(error_processor)
4849

49-
# Register the hook with the global launchdarkly client.
50-
client = _get_ldclient()
51-
client.add_hook(LaunchDarklyHook())
52-
5350

5451
class LaunchDarklyHook(Hook):
5552

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sentry_sdk
2+
3+
from ldclient import LDClient
4+
from ldclient.config import Config
5+
from ldclient.context import Context
6+
from ldclient.integrations.test_data import TestData
7+
8+
from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration
9+
10+
# Ref: https://docs.launchdarkly.com/sdk/features/test-data-sources#python
11+
12+
13+
def test_launchdarkly_integration(sentry_init):
14+
td = TestData.data_source()
15+
client = LDClient(config=Config("sdk-key", update_processor_class=td))
16+
sentry_init(integrations=[LaunchDarklyIntegration(client=client)])
17+
18+
# Set test values
19+
td.update(td.flag("hello").variation_for_all(True))
20+
td.update(td.flag("world").variation_for_all(True))
21+
td.update(td.flag("goodbye").variation_for_all(False))
22+
23+
# Evaluate
24+
client.variation("hello", Context.create("my-org", "organization"), False)
25+
client.variation("world", Context.create("user1", "user"), False)
26+
client.variation("goodbye", Context.create("user2", "user"), False)
27+
28+
assert sentry_sdk.get_current_scope().flags.get() == [
29+
{"flag": "hello", "result": True},
30+
{"flag": "world", "result": True},
31+
{"flag": "goodbye", "result": False},
32+
]

0 commit comments

Comments
 (0)