22import sentry_sdk
33
44from sentry_sdk .integrations import DidNotEnable , Integration
5+ from sentry_sdk .flag_utils import flag_error_processor
56
67try :
78 import ldclient
1213 from ldclient .hook import EvaluationSeriesContext
1314 from ldclient .evaluation import EvaluationDetail
1415
15- from sentry_sdk ._types import Event , ExcInfo
16- from typing import Any , Optional
16+ from typing import Any
1717except ImportError :
1818 raise DidNotEnable ("LaunchDarkly is not installed" )
1919
2020
2121class LaunchDarklyIntegration (Integration ):
2222 identifier = "launchdarkly"
2323
24- def __init__ (self , client = None ):
24+ def __init__ (self , ld_client = None ):
2525 # type: (LDClient | None) -> None
2626 """
2727 :param client: An initialized LDClient instance. If a client is not provided, this
2828 integration will attempt to use the shared global instance.
2929 """
30- if client is None :
31- try :
32- client = ldclient .get () # global singleton.
33- except Exception as exc :
34- raise DidNotEnable ("Error getting LaunchDarkly client. " + repr (exc ))
30+ try :
31+ client = ld_client or ldclient .get ()
32+ except Exception as exc :
33+ raise DidNotEnable ("Error getting LaunchDarkly client. " + repr (exc ))
3534
3635 if not client .is_initialized ():
3736 raise DidNotEnable ("LaunchDarkly client is not initialized." )
38- self .ld_client = client
37+
38+ self .client = client
3939
4040 @staticmethod
4141 def setup_once ():
4242 # type: () -> None
43- def error_processor (event , _exc_info ):
44- # type: (Event, ExcInfo) -> Optional[Event]
45- scope = sentry_sdk .get_current_scope ()
46- event ["contexts" ]["flags" ] = {"values" : scope .flags .get ()}
47- return event
48-
4943 scope = sentry_sdk .get_current_scope ()
50- scope .add_error_processor (error_processor )
44+ scope .add_error_processor (flag_error_processor )
5145
5246 # Register the flag collection hook with the LD client.
53- ld_client = (
54- sentry_sdk .get_client ().get_integration (LaunchDarklyIntegration ).ld_client
55- )
56- ld_client .add_hook (LaunchDarklyHook ())
47+ client = sentry_sdk .get_client ().get_integration (LaunchDarklyIntegration ).client
48+ client .add_hook (LaunchDarklyHook ())
5749
5850
5951class LaunchDarklyHook (Hook ):
@@ -70,6 +62,6 @@ def after_evaluation(self, series_context, data, detail):
7062 flags .set (series_context .key , detail .value )
7163 return data
7264
73- def before_evaluation (self , _series_context , data ):
65+ def before_evaluation (self , series_context , data ):
7466 # type: (EvaluationSeriesContext, dict[Any, Any]) -> dict[Any, Any]
7567 return data # No-op.
0 commit comments