Skip to content

Commit a9d5099

Browse files
committed
Fix typing and extract error_processor to common module
1 parent 2f59b47 commit a9d5099

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

sentry_sdk/flag_utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from copy import copy
22
from typing import TYPE_CHECKING
33

4+
import sentry_sdk
45
from sentry_sdk._lru_cache import LRUCache
56

67
if TYPE_CHECKING:
7-
from typing import TypedDict
8+
from typing import TypedDict, Optional
9+
from sentry_sdk._types import Event, ExcInfo
810

911
FlagData = TypedDict("FlagData", {"flag": str, "result": bool})
1012

@@ -36,3 +38,10 @@ def get(self):
3638
def set(self, flag, result):
3739
# type: (str, bool) -> None
3840
self.buffer.set(flag, result)
41+
42+
43+
def flag_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

sentry_sdk/integrations/launchdarkly.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sentry_sdk
33

44
from sentry_sdk.integrations import DidNotEnable, Integration
5+
from sentry_sdk.flag_utils import flag_error_processor
56

67
try:
78
import ldclient
@@ -12,48 +13,39 @@
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
1717
except ImportError:
1818
raise DidNotEnable("LaunchDarkly is not installed")
1919

2020

2121
class 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

5951
class 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.

sentry_sdk/integrations/openfeature.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sentry_sdk
33

44
from sentry_sdk.integrations import DidNotEnable, Integration
5+
from sentry_sdk.flag_utils import flag_error_processor
56

67
try:
78
from openfeature import api
@@ -10,8 +11,6 @@
1011
if TYPE_CHECKING:
1112
from openfeature.flag_evaluation import FlagEvaluationDetails
1213
from openfeature.hook import HookContext, HookHints
13-
from sentry_sdk._types import Event, ExcInfo
14-
from typing import Optional
1514
except ImportError:
1615
raise DidNotEnable("OpenFeature is not installed")
1716

@@ -22,14 +21,8 @@ class OpenFeatureIntegration(Integration):
2221
@staticmethod
2322
def setup_once():
2423
# type: () -> None
25-
def error_processor(event, exc_info):
26-
# type: (Event, ExcInfo) -> Optional[Event]
27-
scope = sentry_sdk.get_current_scope()
28-
event["contexts"]["flags"] = {"values": scope.flags.get()}
29-
return event
30-
3124
scope = sentry_sdk.get_current_scope()
32-
scope.add_error_processor(error_processor)
25+
scope.add_error_processor(flag_error_processor)
3326

3427
# Register the hook within the global openfeature hooks list.
3528
api.add_hooks(hooks=[OpenFeatureHook()])

0 commit comments

Comments
 (0)