-
Notifications
You must be signed in to change notification settings - Fork 498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add OpenFeature integration #3648
Open
cmanallen
wants to merge
31
commits into
master
Choose a base branch
from
cmanallen/flags-open-feature-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+293
−0
Open
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
dd9c97e
Initial commit
cmanallen 92b0721
Store flags on the scope
cmanallen 514f69f
Use setup_once
cmanallen f5b2d51
Call copy on the flags property
cmanallen f5b4d8c
Fix error message
cmanallen 6b4df67
Remove docstring
cmanallen a8b9126
Add coverage for openfeature
cmanallen d1b6dd9
Update setup
cmanallen 0066faa
Fix typing
cmanallen 928350a
Add type hints
cmanallen ecd0a4a
Ignore subclass type error
cmanallen 9910049
Merge branch 'master' into cmanallen/flags-open-feature-integration
cmanallen 7d8a37f
Add openfeature to testing requirements
cmanallen 3bcafed
Constrain type
cmanallen 3bddcf1
Fix imports
cmanallen 83417e2
Add openfeature to tox.ini
cmanallen fd411a6
Update tox to install openfeature correctly
cmanallen d97c95e
Add 3.12
cmanallen fd5ab9a
Add openfeature to linting requirements
cmanallen 1f15e1f
Add openfeature to miscellaneous testing group
cmanallen e093c14
Use copy function
cmanallen 08fbf27
Update yaml files
cmanallen 8b2ede6
Fix typing
cmanallen 6db318e
Update version
cmanallen 71cb65c
Update version
cmanallen ed3eac9
Use LRU cache
cmanallen 3f7fdc8
Add more LRU cache coverage
cmanallen 9619c17
Merge branch 'master' into cmanallen/flags-open-feature-integration
antonpirker 38b6521
Set static version
cmanallen b3e3bf3
Set latest and 0.7 pinned version
cmanallen 8aede7e
Initialize max_flags from experimental init
cmanallen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ flake8-bugbear | |
pep8-naming | ||
pre-commit # local linting | ||
httpcore | ||
openfeature-sdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,7 @@ | |
], | ||
"Miscellaneous": [ | ||
"loguru", | ||
"openfeature", | ||
"opentelemetry", | ||
"potel", | ||
"pure_eval", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from copy import copy | ||
from typing import TYPE_CHECKING | ||
|
||
from sentry_sdk._lru_cache import LRUCache | ||
|
||
if TYPE_CHECKING: | ||
from typing import TypedDict | ||
|
||
FlagData = TypedDict("FlagData", {"flag": str, "result": bool}) | ||
|
||
|
||
class FlagBuffer: | ||
|
||
def __init__(self, capacity): | ||
# type: (int) -> None | ||
self.buffer = LRUCache(capacity) | ||
self.capacity = capacity | ||
|
||
def clear(self): | ||
# type: () -> None | ||
self.buffer = LRUCache(self.capacity) | ||
|
||
def __copy__(self): | ||
# type: () -> FlagBuffer | ||
buffer = FlagBuffer(capacity=self.capacity) | ||
buffer.buffer = copy(self.buffer) | ||
return buffer | ||
|
||
def get(self): | ||
# type: () -> list[FlagData] | ||
return [{"flag": key, "result": value} for key, value in self.buffer.get_all()] | ||
|
||
def set(self, flag, result): | ||
# type: (str, bool) -> None | ||
self.buffer.set(flag, result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import TYPE_CHECKING | ||
import sentry_sdk | ||
|
||
from sentry_sdk.integrations import DidNotEnable, Integration | ||
|
||
try: | ||
from openfeature import api | ||
from openfeature.hook import Hook | ||
|
||
if TYPE_CHECKING: | ||
from openfeature.flag_evaluation import FlagEvaluationDetails | ||
from openfeature.hook import HookContext, HookHints | ||
from sentry_sdk._types import Event, ExcInfo | ||
from typing import Optional | ||
except ImportError: | ||
raise DidNotEnable("OpenFeature is not installed") | ||
|
||
|
||
class OpenFeatureIntegration(Integration): | ||
identifier = "openfeature" | ||
|
||
@staticmethod | ||
def setup_once(): | ||
# type: () -> None | ||
def error_processor(event, exc_info): | ||
# type: (Event, ExcInfo) -> Optional[Event] | ||
scope = sentry_sdk.get_current_scope() | ||
event["contexts"]["flags"] = {"values": scope.flags.get()} | ||
return event | ||
|
||
scope = sentry_sdk.get_current_scope() | ||
scope.add_error_processor(error_processor) | ||
|
||
# Register the hook within the global openfeature hooks list. | ||
api.add_hooks(hooks=[OpenFeatureHook()]) | ||
|
||
|
||
class OpenFeatureHook(Hook): | ||
|
||
def after(self, hook_context, details, hints): | ||
# type: (HookContext, FlagEvaluationDetails[bool], HookHints) -> None | ||
if isinstance(details.value, bool): | ||
flags = sentry_sdk.get_current_scope().flags | ||
flags.set(details.flag_key, details.value) | ||
|
||
def error(self, hook_context, exception, hints): | ||
# type: (HookContext, Exception, HookHints) -> None | ||
if isinstance(hook_context.default_value, bool): | ||
flags = sentry_sdk.get_current_scope().flags | ||
flags.set(hook_context.flag_key, hook_context.default_value) | ||
cmanallen marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import pytest | ||
|
||
pytest.importorskip("openfeature") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import asyncio | ||
import concurrent.futures as cf | ||
import sentry_sdk | ||
|
||
from openfeature import api | ||
from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider | ||
from sentry_sdk.integrations.openfeature import OpenFeatureIntegration | ||
|
||
|
||
def test_openfeature_integration(sentry_init): | ||
sentry_init(integrations=[OpenFeatureIntegration()]) | ||
|
||
flags = { | ||
"hello": InMemoryFlag("on", {"on": True, "off": False}), | ||
"world": InMemoryFlag("off", {"on": True, "off": False}), | ||
} | ||
api.set_provider(InMemoryProvider(flags)) | ||
|
||
client = api.get_client() | ||
client.get_boolean_value("hello", default_value=False) | ||
client.get_boolean_value("world", default_value=False) | ||
client.get_boolean_value("other", default_value=True) | ||
|
||
assert sentry_sdk.get_current_scope().flags.get() == [ | ||
{"flag": "hello", "result": True}, | ||
{"flag": "world", "result": False}, | ||
{"flag": "other", "result": True}, | ||
] | ||
|
||
|
||
def test_openfeature_integration_threaded(sentry_init): | ||
sentry_init(integrations=[OpenFeatureIntegration()]) | ||
|
||
flags = { | ||
"hello": InMemoryFlag("on", {"on": True, "off": False}), | ||
"world": InMemoryFlag("off", {"on": True, "off": False}), | ||
} | ||
api.set_provider(InMemoryProvider(flags)) | ||
|
||
client = api.get_client() | ||
client.get_boolean_value("hello", default_value=False) | ||
|
||
def task(flag): | ||
# Create a new isolation scope for the thread. This means the flags | ||
with sentry_sdk.isolation_scope(): | ||
client.get_boolean_value(flag, default_value=False) | ||
return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] | ||
|
||
with cf.ThreadPoolExecutor(max_workers=2) as pool: | ||
results = list(pool.map(task, ["world", "other"])) | ||
|
||
assert results[0] == ["hello", "world"] | ||
assert results[1] == ["hello", "other"] | ||
|
||
|
||
def test_openfeature_integration_asyncio(sentry_init): | ||
"""Assert concurrently evaluated flags do not pollute one another.""" | ||
|
||
async def task(flag): | ||
with sentry_sdk.isolation_scope(): | ||
client.get_boolean_value(flag, default_value=False) | ||
return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] | ||
|
||
async def runner(): | ||
return asyncio.gather(task("world"), task("other")) | ||
|
||
sentry_init(integrations=[OpenFeatureIntegration()]) | ||
|
||
flags = { | ||
"hello": InMemoryFlag("on", {"on": True, "off": False}), | ||
"world": InMemoryFlag("off", {"on": True, "off": False}), | ||
} | ||
api.set_provider(InMemoryProvider(flags)) | ||
|
||
client = api.get_client() | ||
client.get_boolean_value("hello", default_value=False) | ||
|
||
results = asyncio.run(runner()).result() | ||
assert results[0] == ["hello", "world"] | ||
assert results[1] == ["hello", "other"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from sentry_sdk.flag_utils import FlagBuffer | ||
|
||
|
||
def test_flag_tracking(): | ||
"""Assert the ring buffer works.""" | ||
buffer = FlagBuffer(capacity=3) | ||
buffer.set("a", True) | ||
flags = buffer.get() | ||
assert len(flags) == 1 | ||
assert flags == [{"flag": "a", "result": True}] | ||
|
||
buffer.set("b", True) | ||
flags = buffer.get() | ||
assert len(flags) == 2 | ||
assert flags == [{"flag": "a", "result": True}, {"flag": "b", "result": True}] | ||
|
||
buffer.set("c", True) | ||
flags = buffer.get() | ||
assert len(flags) == 3 | ||
assert flags == [ | ||
{"flag": "a", "result": True}, | ||
{"flag": "b", "result": True}, | ||
{"flag": "c", "result": True}, | ||
] | ||
|
||
buffer.set("d", False) | ||
flags = buffer.get() | ||
assert len(flags) == 3 | ||
assert flags == [ | ||
{"flag": "b", "result": True}, | ||
{"flag": "c", "result": True}, | ||
{"flag": "d", "result": False}, | ||
] | ||
|
||
buffer.set("e", False) | ||
buffer.set("f", False) | ||
flags = buffer.get() | ||
assert len(flags) == 3 | ||
assert flags == [ | ||
{"flag": "d", "result": False}, | ||
{"flag": "e", "result": False}, | ||
{"flag": "f", "result": False}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we using 2 type hints here?First hint is for setup_once