-
Notifications
You must be signed in to change notification settings - Fork 3
feat!: implement experimentation tracking #37
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
Open
Zaimwa9
wants to merge
14
commits into
main
Choose a base branch
from
feat/experimentation
base: main
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.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3292203
build: require flagsmith >=5.5 for the experimentation API
Zaimwa9 dc991ae
feat: populate reason, variant and flag_metadata on resolutions
Zaimwa9 521d98c
feat: support transient identities via the evaluation context
Zaimwa9 b39a66c
fix!: track() crashed on flagsmith >=5.4 and hid value in metadata
Zaimwa9 c70cedd
feat: route feature_flag.exposure tracking events to Flagsmith
Zaimwa9 f0682e0
feat: add FlagsmithExposureHook for evaluation-coupled exposures
Zaimwa9 1c3ab22
feat: re-export provider, hook and exposure constant from package root
Zaimwa9 f40e0d5
docs: document exposure tiers and fix broken tracking example
Zaimwa9 7f7a9e4
feat: emit SPLIT for multivariate assignments and gate exposures on it
Zaimwa9 2682781
test: pin the cross-SDK wire contract with literal assertions
Zaimwa9 d070cb4
feat: remove ia slop
Zaimwa9 d4f42f6
fix: warn and drop exposures carrying a non-string variant
Zaimwa9 8e81177
ci: run the test suite against the minimum supported flagsmith
Zaimwa9 59ec7b6
refactor: drop the hook dedupe LRU in favor of SDK-side deduplication
Zaimwa9 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from openfeature_flagsmith.hooks import FlagsmithExposureHook | ||
| from openfeature_flagsmith.provider import FlagsmithProvider | ||
| from openfeature_flagsmith.tracking import EXPOSURE_TRACKING_EVENT | ||
|
|
||
| __all__ = [ | ||
| "EXPOSURE_TRACKING_EVENT", | ||
| "FlagsmithExposureHook", | ||
| "FlagsmithProvider", | ||
| ] |
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,75 @@ | ||
| import logging | ||
| import typing | ||
|
|
||
| from openfeature.flag_evaluation import FlagEvaluationDetails, Reason | ||
| from openfeature.hook import Hook, HookContext, HookHints | ||
| from openfeature.track import TrackingEventDetails | ||
|
|
||
| from openfeature_flagsmith.tracking import EXPOSURE_TRACKING_EVENT | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from openfeature_flagsmith.provider import FlagsmithProvider | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _is_split_reason(reason: typing.Union[str, Reason, None]) -> bool: | ||
| # The engine annotates reasons ("SPLIT; weight=30"); compare the | ||
| # leading token. | ||
| if reason is None: | ||
| return False | ||
| return str(reason).split(";", 1)[0].strip() == Reason.SPLIT.value | ||
|
|
||
|
|
||
| class FlagsmithExposureHook(Hook): | ||
| """ | ||
| Records a Flagsmith exposure as a side effect of a flag evaluation:: | ||
|
|
||
| hook = FlagsmithExposureHook(provider) | ||
| client.get_string_details( | ||
| "my_experiment_flag", | ||
| "control", | ||
| context, | ||
| FlagEvaluationOptions(hooks=[hook]), | ||
| ) | ||
|
|
||
| Attaching the hook at a call site is the experiment declaration: | ||
| evaluations without it never record exposures. Exposures only fire for | ||
| flags resolved with a variant and reason ``SPLIT``; duplicate exposures | ||
| are deduplicated downstream. | ||
| """ | ||
|
|
||
| def __init__(self, provider: "FlagsmithProvider") -> None: | ||
| self._provider = provider | ||
|
|
||
| def after( | ||
| self, | ||
| hook_context: HookContext, | ||
| details: FlagEvaluationDetails, | ||
| hints: HookHints, | ||
| ) -> None: | ||
| # An uncaught after-hook error would flip the evaluation to ERROR. | ||
| try: | ||
| variant = details.variant | ||
| if not isinstance(variant, str): | ||
| return | ||
| if not _is_split_reason(details.reason): | ||
| logger.debug( | ||
| 'Exposure for "%s" skipped: resolution reason is %s, not SPLIT.', | ||
| details.flag_key, | ||
| details.reason, | ||
| ) | ||
| return | ||
| self._provider.track( | ||
| EXPOSURE_TRACKING_EVENT, | ||
| hook_context.evaluation_context, | ||
| TrackingEventDetails( | ||
| attributes={"flag_key": details.flag_key, "variant": variant} | ||
| ), | ||
| ) | ||
| except Exception: | ||
| logger.warning( | ||
| 'Failed to record the exposure for "%s".', | ||
| details.flag_key, | ||
| exc_info=True, | ||
| ) |
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.
nit: We should really have a matrix analogous to what we have in the Python SDK repo. Can be a follow-up issue/PR.