Skip to content
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

feat(flags): add Unleash feature flagging integration #3888

Merged
merged 26 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c6aff34
feat(flags): add Unleash feature flagging integration
aliu39 Dec 21, 2024
d4c9b81
Finish class attr implementation and basic test
aliu39 Dec 22, 2024
e974188
Test get_variant
aliu39 Dec 22, 2024
3b78bd6
Test additional variant payload types and split up to 1 file per method
aliu39 Dec 22, 2024
cf0be41
Add to split_tox_gh_actions.py
aliu39 Dec 22, 2024
da2a21e
Update gh workflow config by running split_tox_gh_actions
aliu39 Dec 22, 2024
2ef2f7d
Rename tests and rm test type annotations
aliu39 Dec 22, 2024
2a443b9
Patch class method instead of instance, so passing in client is not n…
aliu39 Dec 22, 2024
0e638b9
Rename uninstall_integration
aliu39 Dec 22, 2024
08f4e3d
test wrapper attributes
aliu39 Dec 22, 2024
4ee3667
importorskip in test module
aliu39 Dec 22, 2024
38f279b
handle not installed import error
aliu39 Dec 22, 2024
da54e49
wraps_original test
aliu39 Dec 23, 2024
0538bce
ref uninstall_integration fixture
aliu39 Dec 23, 2024
47d29c3
Threaded and asyncio tests
aliu39 Dec 23, 2024
35b8dc2
Rm unused conftest import
aliu39 Dec 23, 2024
4cc1cae
Track all variants regardless of payload
aliu39 Dec 23, 2024
43a8306
Merge branch 'master' into aliu/unleash
aliu39 Dec 23, 2024
b773e49
Merge branch 'master' into aliu/unleash
antonpirker Dec 23, 2024
c8104b8
Patch an UnleashClient instance instead of the class
aliu39 Dec 23, 2024
3946bf2
Add client isolation test and fix init typing
aliu39 Dec 27, 2024
800354d
Merge branch 'master' into aliu/unleash
aliu39 Dec 27, 2024
633b3ed
Wrap UnleashClient directly
cmanallen Jan 3, 2025
9ebee27
Use identifier
cmanallen Jan 3, 2025
cffb475
whitespace
antonpirker Jan 7, 2025
95c72e9
Merge branch 'master' into aliu/unleash
antonpirker Jan 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/test-integrations-misc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ jobs:
run: |
set -x # print commands that are executed
./scripts/runtox.sh "py${{ matrix.python-version }}-typer-latest"
- name: Test unleash latest
run: |
set -x # print commands that are executed
./scripts/runtox.sh "py${{ matrix.python-version }}-unleash-latest"
- name: Generate coverage XML (Python 3.6)
if: ${{ !cancelled() && matrix.python-version == '3.6' }}
run: |
Expand Down Expand Up @@ -163,6 +167,10 @@ jobs:
run: |
set -x # print commands that are executed
./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-typer"
- name: Test unleash pinned
run: |
set -x # print commands that are executed
./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-unleash"
- name: Generate coverage XML (Python 3.6)
if: ${{ !cancelled() && matrix.python-version == '3.6' }}
run: |
Expand Down
1 change: 1 addition & 0 deletions requirements-linting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ pre-commit # local linting
httpcore
openfeature-sdk
launchdarkly-server-sdk
UnleashClient
typer
1 change: 1 addition & 0 deletions scripts/split_tox_gh_actions/split_tox_gh_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"pure_eval",
"trytond",
"typer",
"unleash",
],
}

Expand Down
55 changes: 55 additions & 0 deletions sentry_sdk/integrations/unleash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from functools import wraps
from typing import Any

import sentry_sdk
from sentry_sdk.flag_utils import flag_error_processor
from sentry_sdk.integrations import Integration, DidNotEnable

try:
from UnleashClient import UnleashClient
except ImportError:
raise DidNotEnable("UnleashClient is not installed")


class UnleashIntegration(Integration):
identifier = "unleash"

@staticmethod
def setup_once():
# type: () -> None
# Wrap and patch evaluation methods (instance methods)
old_is_enabled = UnleashClient.is_enabled
old_get_variant = UnleashClient.get_variant

@wraps(old_is_enabled)
def sentry_is_enabled(self, feature, *args, **kwargs):
# type: (UnleashClient, str, *Any, **Any) -> Any
enabled = old_is_enabled(self, feature, *args, **kwargs)

# We have no way of knowing what type of unleash feature this is, so we have to treat
# it as a boolean / toggle feature.
flags = sentry_sdk.get_current_scope().flags
flags.set(feature, enabled)

return enabled

@wraps(old_get_variant)
def sentry_get_variant(self, feature, *args, **kwargs):
# type: (UnleashClient, str, *Any, **Any) -> Any
variant = old_get_variant(self, feature, *args, **kwargs)
enabled = variant.get("enabled", False)

# Payloads are not always used as the feature's value for application logic. They
# may be used for metrics or debugging context instead. Therefore, we treat every
# variant as a boolean toggle, using the `enabled` field.
flags = sentry_sdk.get_current_scope().flags
flags.set(feature, enabled)

return variant

UnleashClient.is_enabled = sentry_is_enabled # type: ignore
UnleashClient.get_variant = sentry_get_variant # type: ignore

# Error processor
scope = sentry_sdk.get_current_scope()
scope.add_error_processor(flag_error_processor)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def get_file_text(file_name):
"starlette": ["starlette>=0.19.1"],
"starlite": ["starlite>=1.48"],
"tornado": ["tornado>=6"],
"unleash": ["UnleashClient>=6.0.1"],
},
entry_points={
"opentelemetry_propagator": [
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pytest
import jsonschema


try:
import gevent
except ImportError:
Expand Down
3 changes: 3 additions & 0 deletions tests/integrations/unleash/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pytest

pytest.importorskip("UnleashClient")
Loading
Loading