-
Notifications
You must be signed in to change notification settings - Fork 571
feat(flags): Add LaunchDarkly Integration #3679
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
cmanallen
merged 31 commits into
cmanallen/flags-open-feature-integration
from
aliu/launch-darkly
Oct 30, 2024
Merged
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
c25b802
Initial src and dependency code
aliu39 e60e1a4
Move get_ldclient to a top-level helper
aliu39 597004b
Add to requirements-testing
aliu39 75a3442
Split up static version from latest
aliu39 43332d2
Merge branch 'cmanallen/flags-open-feature-integration' of https://gi…
aliu39 a2e3383
Fix import
aliu39 5165ffb
Pass in client to Integration initializer and basic unit test
aliu39 c9daf17
Add threaded, asyncio, and global ldclient tests
aliu39 d7ae9f5
Change metadata, test not enabled cases
aliu39 7740f43
Add versioned tests to workflows
aliu39 0309b82
Rm doc references
aliu39 cec37dc
Fix split-tox-gh-actions GROUPS
aliu39 22d1024
Add doc references
aliu39 d9775b8
Formatting from pr comments. Max line length=100
aliu39 91eb352
Move hook registration to setup_once
aliu39 2f59b47
Merge branch 'cmanallen/flags-open-feature-integration' into aliu/lau…
cmanallen a9d5099
Fix typing and extract error_processor to common module
cmanallen 50d2dae
Raise if the integration was not enabled before setup_once is called
cmanallen 44aebf3
Rename parameter
cmanallen 144e064
Move hook registration to the init method
cmanallen 13434c3
Update tox to use 3.8 or greater
cmanallen 77d4055
Fix name
cmanallen 8a1a20e
Remove duplicate definition
cmanallen 2dab8c3
Remove another dupe and change naming
cmanallen c97e102
Restrict versions
cmanallen ead840f
Remove integration init
cmanallen bb678c2
Rename extras_require for launchdarkly
cmanallen 08289c2
Try resetting the client
cmanallen a3d90bd
Remove launchdarkly from testing requirements
cmanallen 711fe55
Revert "Remove integration init"
cmanallen 5218c7a
Remove client reset
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 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 |
|---|---|---|
|
|
@@ -16,3 +16,4 @@ 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,4 @@ socksio | |
| httpcore[http2] | ||
| setuptools | ||
| Brotli | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,6 +125,7 @@ | |
| "tornado", | ||
| ], | ||
| "Miscellaneous": [ | ||
| "ldclient", | ||
| "loguru", | ||
| "openfeature", | ||
| "opentelemetry", | ||
|
|
||
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,70 @@ | ||
| from typing import TYPE_CHECKING | ||
| import sentry_sdk | ||
|
|
||
| from sentry_sdk.integrations import DidNotEnable, Integration | ||
|
|
||
| try: | ||
| import ldclient | ||
| from ldclient.hook import Hook | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ldclient import LDClient | ||
| from ldclient.hook import EvaluationSeriesContext, Metadata | ||
| from ldclient.evaluation import EvaluationDetail | ||
|
|
||
| from sentry_sdk._types import Event, ExcInfo | ||
| from typing import Any, Optional | ||
| except ImportError: | ||
| raise DidNotEnable("LaunchDarkly is not installed") | ||
|
|
||
|
|
||
| def _get_ldclient(): | ||
| # type: () -> LDClient | ||
| try: | ||
| client = ldclient.get() | ||
| except Exception as exc: | ||
| sentry_sdk.capture_exception(exc) | ||
| raise DidNotEnable("Failed to find LaunchDarkly client instance. " + str(exc)) | ||
|
|
||
| if client and client.is_initialized(): | ||
| return client | ||
| raise DidNotEnable("LaunchDarkly client is not initialized") | ||
|
|
||
|
|
||
| class LaunchDarklyIntegration(Integration): | ||
| identifier = "launchdarkly" | ||
|
|
||
| @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 with the global launchdarkly client. | ||
| client = _get_ldclient() | ||
| client.add_hook(LaunchDarklyHook()) | ||
|
|
||
|
|
||
| class LaunchDarklyHook(Hook): | ||
|
|
||
| @property | ||
| def metadata(self): | ||
| # type: () -> Metadata | ||
| return Metadata(name="sentry-on-error-hook") | ||
|
|
||
| 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): | ||
aliu39 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # 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
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") | ||
aliu39 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
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.