Skip to content

Commit d78664d

Browse files
committed
Revert "Illustrate moving the flags from the scope to the integration"
This reverts commit 430387f.
1 parent 430387f commit d78664d

File tree

4 files changed

+29
-104
lines changed

4 files changed

+29
-104
lines changed

sentry_sdk/flag_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
FlagData = TypedDict("FlagData", {"flag": str, "result": bool})
1010

1111

12-
# DEFAULT_FLAG_CAPACITY = 100
12+
DEFAULT_FLAG_CAPACITY = 100
1313

1414

1515
class FlagBuffer:
Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import TYPE_CHECKING
22
import sentry_sdk
33

4-
from sentry_sdk.flag_utils import FlagBuffer
54
from sentry_sdk.integrations import DidNotEnable, Integration
65

76
try:
@@ -17,55 +16,35 @@
1716
raise DidNotEnable("OpenFeature is not installed")
1817

1918

20-
DEFAULT_FLAG_CAPACITY = 100
21-
22-
2319
class OpenFeatureIntegration(Integration):
2420
identifier = "openfeature"
2521

26-
def __init__(self, max_flags=DEFAULT_FLAG_CAPACITY):
27-
# type: (OpenFeatureIntegration, int) -> None
28-
self._max_flags = max_flags
29-
self._flags = None # type: Optional[FlagBuffer]
30-
3122
@staticmethod
3223
def setup_once():
3324
# 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+
31+
scope = sentry_sdk.get_current_scope()
32+
scope.add_error_processor(error_processor)
33+
3434
# Register the hook within the global openfeature hooks list.
3535
api.add_hooks(hooks=[OpenFeatureHook()])
3636

37-
@property
38-
def flags(self):
39-
# type: () -> FlagBuffer
40-
if self._flags is None:
41-
max_flags = self._max_flags or DEFAULT_FLAG_CAPACITY
42-
self._flags = FlagBuffer(capacity=max_flags)
43-
return self._flags
44-
4537

4638
class OpenFeatureHook(Hook):
39+
4740
def after(self, hook_context, details, hints):
4841
# type: (HookContext, FlagEvaluationDetails[bool], HookHints) -> None
49-
integration = sentry_sdk.get_client().get_integration(OpenFeatureIntegration)
50-
if integration is None:
51-
return
52-
5342
if isinstance(details.value, bool):
54-
integration.flags.set(details.flag_key, details.value)
43+
flags = sentry_sdk.get_current_scope().flags
44+
flags.set(details.flag_key, details.value)
5545

5646
def error(self, hook_context, exception, hints):
5747
# type: (HookContext, Exception, HookHints) -> None
58-
integration = sentry_sdk.get_client().get_integration(OpenFeatureIntegration)
59-
if integration is None:
60-
return
61-
62-
def error_processor(event, exc_info):
63-
# type: (Event, ExcInfo) -> Optional[Event]
64-
event["contexts"]["flags"] = {"values": integration.flags.get()}
65-
return event
66-
67-
scope = sentry_sdk.get_current_scope()
68-
scope.add_error_processor(error_processor)
69-
7048
if isinstance(hook_context.default_value, bool):
71-
integration.flags.set(hook_context.flag_key, hook_context.default_value)
49+
flags = sentry_sdk.get_current_scope().flags
50+
flags.set(hook_context.flag_key, hook_context.default_value)

sentry_sdk/scope.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
from sentry_sdk.attachments import Attachment
1313
from sentry_sdk.consts import DEFAULT_MAX_BREADCRUMBS, FALSE_VALUES, INSTRUMENTER
14-
15-
# from sentry_sdk.flag_utils import FlagBuffer, DEFAULT_FLAG_CAPACITY
14+
from sentry_sdk.flag_utils import FlagBuffer, DEFAULT_FLAG_CAPACITY
1615
from sentry_sdk.profiler.continuous_profiler import try_autostart_continuous_profiler
1716
from sentry_sdk.profiler.transaction_profiler import Profile
1817
from sentry_sdk.session import Session
@@ -194,7 +193,7 @@ class Scope:
194193
"client",
195194
"_type",
196195
"_last_event_id",
197-
# "_flags",
196+
"_flags",
198197
)
199198

200199
def __init__(self, ty=None, client=None):
@@ -252,7 +251,7 @@ def __copy__(self):
252251

253252
rv._last_event_id = self._last_event_id
254253

255-
# rv._flags = copy(self._flags)
254+
rv._flags = copy(self._flags)
256255

257256
return rv
258257

@@ -690,7 +689,7 @@ def clear(self):
690689

691690
# self._last_event_id is only applicable to isolation scopes
692691
self._last_event_id = None # type: Optional[str]
693-
# self._flags = None # type: Optional[FlagBuffer]
692+
self._flags = None # type: Optional[FlagBuffer]
694693

695694
@_attr_setter
696695
def level(self, value):
@@ -1552,16 +1551,16 @@ def __repr__(self):
15521551
self._type,
15531552
)
15541553

1555-
# @property
1556-
# def flags(self):
1557-
# # type: () -> FlagBuffer
1558-
# if self._flags is None:
1559-
# max_flags = (
1560-
# self.get_client().options["_experiments"].get("max_flags")
1561-
# or DEFAULT_FLAG_CAPACITY
1562-
# )
1563-
# self._flags = FlagBuffer(capacity=max_flags)
1564-
# return self._flags
1554+
@property
1555+
def flags(self):
1556+
# type: () -> FlagBuffer
1557+
if self._flags is None:
1558+
max_flags = (
1559+
self.get_client().options["_experiments"].get("max_flags")
1560+
or DEFAULT_FLAG_CAPACITY
1561+
)
1562+
self._flags = FlagBuffer(capacity=max_flags)
1563+
return self._flags
15651564

15661565

15671566
@contextmanager

tests/integrations/openfeature/test_openfeature.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,12 @@
11
import asyncio
22
import concurrent.futures as cf
3-
43
import sentry_sdk
54

65
from openfeature import api
76
from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider
87
from sentry_sdk.integrations.openfeature import OpenFeatureIntegration
98

109

11-
def test_openfeature_integration_flags_on_integration(sentry_init, capture_events):
12-
sentry_init(integrations=[OpenFeatureIntegration()])
13-
14-
flags = {
15-
"hello": InMemoryFlag("on", {"on": True, "off": False}),
16-
"world": InMemoryFlag("off", {"on": True, "off": False}),
17-
}
18-
api.set_provider(InMemoryProvider(flags))
19-
20-
client = api.get_client()
21-
client.get_boolean_value("hello", default_value=False)
22-
client.get_boolean_value("world", default_value=False)
23-
client.get_boolean_value("other", default_value=True)
24-
25-
events = capture_events()
26-
27-
sentry_sdk.capture_exception(Exception("test"))
28-
29-
(event,) = events
30-
31-
assert event["contexts"]["flags"]["values"] == [
32-
{"flag": "hello", "result": True},
33-
{"flag": "world", "result": False},
34-
{"flag": "other", "result": True},
35-
]
36-
37-
38-
def test_openfeature_integration_max_flags(sentry_init, capture_events):
39-
sentry_init(integrations=[OpenFeatureIntegration(max_flags=2)])
40-
41-
flags = {
42-
"hello": InMemoryFlag("on", {"on": True, "off": False}),
43-
"world": InMemoryFlag("off", {"on": True, "off": False}),
44-
}
45-
api.set_provider(InMemoryProvider(flags))
46-
47-
client = api.get_client()
48-
client.get_boolean_value("hello", default_value=False)
49-
client.get_boolean_value("world", default_value=False)
50-
client.get_boolean_value("other", default_value=True)
51-
52-
events = capture_events()
53-
54-
sentry_sdk.capture_exception(Exception("test"))
55-
56-
(event,) = events
57-
assert event["contexts"]["flags"]["values"] == [
58-
{"flag": "world", "result": False},
59-
{"flag": "other", "result": True},
60-
]
61-
62-
6310
def test_openfeature_integration(sentry_init):
6411
sentry_init(integrations=[OpenFeatureIntegration()])
6512

0 commit comments

Comments
 (0)