-
Notifications
You must be signed in to change notification settings - Fork 549
Add LaunchDarkly and OpenFeature integration #3648
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
Merged
Merged
Changes from all commits
Commits
Show all changes
38 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 b1a0973
Merge branch 'master' into cmanallen/flags-open-feature-integration
antonpirker 430387f
Illustrate moving the flags from the scope to the integration
antonpirker d78664d
Revert "Illustrate moving the flags from the scope to the integration"
cmanallen 6e34299
Merge branch 'master' into cmanallen/flags-open-feature-integration
cmanallen 018cb0f
feat(flags): Add LaunchDarkly Integration (#3679)
aliu39 2ee0729
Merge branch 'master' into cmanallen/flags-open-feature-integration
antonpirker 200917f
Merge branch 'master' into cmanallen/flags-open-feature-integration
antonpirker 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 hidden or 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 hidden or 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 hidden or 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,5 @@ flake8-bugbear | |
pep8-naming | ||
pre-commit # local linting | ||
httpcore | ||
openfeature-sdk | ||
launchdarkly-server-sdk |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,47 @@ | ||
from copy import copy | ||
from typing import TYPE_CHECKING | ||
|
||
import sentry_sdk | ||
from sentry_sdk._lru_cache import LRUCache | ||
|
||
if TYPE_CHECKING: | ||
from typing import TypedDict, Optional | ||
from sentry_sdk._types import Event, ExcInfo | ||
|
||
FlagData = TypedDict("FlagData", {"flag": str, "result": bool}) | ||
|
||
|
||
DEFAULT_FLAG_CAPACITY = 100 | ||
|
||
|
||
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) | ||
|
||
|
||
def flag_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 |
This file contains hidden or 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,64 @@ | ||
from typing import TYPE_CHECKING | ||
import sentry_sdk | ||
|
||
from sentry_sdk.integrations import DidNotEnable, Integration | ||
from sentry_sdk.flag_utils import flag_error_processor | ||
|
||
try: | ||
import ldclient | ||
from ldclient.hook import Hook, Metadata | ||
|
||
if TYPE_CHECKING: | ||
from ldclient import LDClient | ||
from ldclient.hook import EvaluationSeriesContext | ||
from ldclient.evaluation import EvaluationDetail | ||
|
||
from typing import Any | ||
except ImportError: | ||
raise DidNotEnable("LaunchDarkly is not installed") | ||
|
||
|
||
class LaunchDarklyIntegration(Integration): | ||
identifier = "launchdarkly" | ||
|
||
def __init__(self, ld_client=None): | ||
# type: (LDClient | None) -> None | ||
""" | ||
:param client: An initialized LDClient instance. If a client is not provided, this | ||
integration will attempt to use the shared global instance. | ||
""" | ||
try: | ||
client = ld_client or ldclient.get() | ||
except Exception as exc: | ||
raise DidNotEnable("Error getting LaunchDarkly client. " + repr(exc)) | ||
|
||
if not client.is_initialized(): | ||
raise DidNotEnable("LaunchDarkly client is not initialized.") | ||
|
||
# Register the flag collection hook with the LD client. | ||
client.add_hook(LaunchDarklyHook()) | ||
|
||
@staticmethod | ||
def setup_once(): | ||
# type: () -> None | ||
scope = sentry_sdk.get_current_scope() | ||
scope.add_error_processor(flag_error_processor) | ||
|
||
|
||
class LaunchDarklyHook(Hook): | ||
|
||
@property | ||
def metadata(self): | ||
# type: () -> Metadata | ||
return Metadata(name="sentry-feature-flag-recorder") | ||
|
||
def after_evaluation(self, series_context, data, detail): | ||
# type: (EvaluationSeriesContext, dict[Any, Any], EvaluationDetail) -> dict[Any, Any] | ||
if isinstance(detail.value, bool): | ||
flags = sentry_sdk.get_current_scope().flags | ||
flags.set(series_context.key, detail.value) | ||
return data | ||
|
||
def before_evaluation(self, series_context, data): | ||
# type: (EvaluationSeriesContext, dict[Any, Any]) -> dict[Any, Any] | ||
return data # No-op. |
This file contains hidden or 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 typing import TYPE_CHECKING | ||
import sentry_sdk | ||
|
||
from sentry_sdk.integrations import DidNotEnable, Integration | ||
from sentry_sdk.flag_utils import flag_error_processor | ||
|
||
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 | ||
except ImportError: | ||
raise DidNotEnable("OpenFeature is not installed") | ||
|
||
|
||
class OpenFeatureIntegration(Integration): | ||
identifier = "openfeature" | ||
|
||
@staticmethod | ||
def setup_once(): | ||
# type: () -> None | ||
scope = sentry_sdk.get_current_scope() | ||
scope.add_error_processor(flag_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 hidden or 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 hidden or 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 hidden or 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("ldclient") |
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.
Uh oh!
There was an error while loading. Please reload this page.