Repository navigation
feat: runtime tracking and feature usage callback setters - #172
Open
vazarkevych wants to merge 6 commits into
Open
vazarkevych wants to merge 6 commits into
vazarkevych wants to merge 6 commits into
Conversation
The tracking and feature usage callbacks could only be supplied to the single-context GrowthBook at construction time, so anything deciding the sink after the instance exists had no supported path. That is not hypothetical here: GrowthBookTrackingPlugin, shipped with the SDK, installed its wrapper by assigning the private `_trackingCallback`, guarded by `hasattr` with a log warning as the failure mode. This commit gives it a public API and switches it over. Adds set_tracking_callback / get_tracking_callback and set_feature_usage_callback / get_feature_usage_callback. The getters matter as much as the setters: consumers wrap rather than replace, and reading the existing sink to chain to it is exactly what the plugin does. No new wiring was needed in evaluation — the context is rebuilt per call, and the existing code already noted that a callback installed later is picked up. Both invocation sites already catch, so a throwing sink still cannot break an evaluation. Deliberately no flush-on-set, unlike the TS SDK. There the deferred buffer exists because no sink was installed, so flushing is the only way those calls are delivered. Here `defer_tracking=True` buffers exposures for hand-off to a client SDK through get_deferred_tracking_calls(), and the buffer records independently of the callback — replaying it into a newly installed sink would report the same exposures twice while also consuming the hand-off. Nothing is lost: buffered calls stay retrievable, and later evaluations reach the new callback normally.
GrowthBookTrackingPlugin installed itself by wrapping the tracking callback, so a later set_tracking_callback() silently replaced it and no exposures reached the ingestor. Plugins now register through an internal listener list (_add_tracking_listener / _remove_tracking_listener) that _track invokes before the user callback; set_tracking_callback() and get_tracking_callback() own only the user's sink. The plugin unregisters its listener on cleanup. Exposure dedupe stays per instance: an exposure already delivered is not re-sent to a newly installed callback, since re-sending would also reach the plugin listeners. Documented in the docstring and README and pinned by a test. Listeners are deduped separately so the user callback keeps its existing retry-if-it-raised behavior.
One plugin object passed to several instances (e.g. per-request instances sharing one batching plugin) keeps a single _gb_instance, so cleanup() from any instance's destroy() removed the listener of the last initialized instance and its exposures stopped reaching the ingestor. cleanup() can't tell which instance is being destroyed, so it no longer touches instance listeners; each instance drops the listeners registered on it in its own destroy().
…-setters # Conflicts: # tests/test_growthbook.py
- Make the plugin listener dedupe check-and-mark atomic, so concurrent evaluations of one exposure reach the tracking plugin once. - Register one bound method per tracking plugin, so a plugin listed twice on an instance doesn't send every exposure twice. - Remove _remove_tracking_listener: nothing calls it since destroy() clears the listeners itself. - Document and pin that a raising user callback is retried on the next evaluation with or without the plugin, while the plugin gets it once.
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge; the previous logging issue is fixed. Reviews (2) · Last reviewed commit: "fix: log feature usage callback errors i..." · Reviewed by Greptile |
_feature_usage swallowed exceptions with a bare pass, so a failing sink set via set_feature_usage_callback (or on_feature_usage) left no trace, while the README says callback errors are logged. Log them the way the async client already does.
Collaborator
Author
|
@greptile review |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What and why
Tracking and feature usage callbacks could only be passed to
GrowthBookat construction. Anything that picks the sink later had no supported path.GrowthBookTrackingPluginalready did this by assigning the private_trackingCallback, guarded byhasattr, with a log warning as the failure mode.What changed
New public API on
GrowthBook:set_tracking_callback/get_tracking_callbackandset_feature_usage_callback/get_feature_usage_callback. They take effect on the next evaluation. PassNoneto detach. The getters let a consumer wrap the current sink instead of replacing it:Plugins get their own slot:
GrowthBookTrackingPluginregisters an internal listener (_add_tracking_listener) that runs before the user callback. Setting or detaching the user callback doesn't stop exposures from reaching GrowthBook, andget_tracking_callback()never returns the plugin's wrapper. A plugin registered twice on one instance adds only one listener.Shared plugin: one plugin object can serve several instances on the same client key. Each instance drops its own listener in
destroy(), so the others keep reporting.Dedupe:
No flush on set: unlike the TS SDK, setting a callback doesn't replay the
defer_trackingbuffer. That buffer exists to hand exposures to a client SDK viaget_deferred_tracking_calls()and records independently of the callback, so replaying would report exposures twice.A raising callback can't break evaluation.
README: new "Setting Callbacks at Runtime" section and a note on sharing the plugin.
Scope: sync
GrowthBookonly. Per-user callbacks forGrowthBookClientcome with #141.How to test
pytest tests/test_growthbook.py -k "callback or tracking_plugin or dedupe or plugin_registered or concurrent_evaluations"13 new tests:
Full suite with current
mainmerged: 1954 passed. The 2cachefixture errors intest_growthbook_client.pyalso happen onmain.Size
Production code +109 / −20, tests +302, README +50.
Migration
None. New methods only. The plugin's switch to its own slot is internal.