Skip to content

feat: runtime tracking and feature usage callback setters - #172

Open
vazarkevych wants to merge 6 commits into
mainfrom
feat/runtime-callback-setters
Open

vazarkevych wants to merge 6 commits into
mainfrom
feat/runtime-callback-setters

Conversation

@vazarkevych

Copy link
Copy Markdown
Collaborator

What and why

Tracking and feature usage callbacks could only be passed to GrowthBook at construction. Anything that picks the sink later had no supported path. GrowthBookTrackingPlugin already did this by assigning the private _trackingCallback, guarded by hasattr, with a log warning as the failure mode.

What changed

  • New public API on GrowthBook: set_tracking_callback / get_tracking_callback and set_feature_usage_callback / get_feature_usage_callback. They take effect on the next evaluation. Pass None to detach. The getters let a consumer wrap the current sink instead of replacing it:

    original = gb.get_tracking_callback()
    
    def wrapper(experiment, result, user_context):
        my_own_sink(experiment, result)
        if original:
            original(experiment=experiment, result=result, user_context=user_context)
    
    gb.set_tracking_callback(wrapper)
  • Plugins get their own slot: GrowthBookTrackingPlugin registers 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, and get_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:

    • It stays per instance. An exposure already delivered isn't re-sent to a newly set callback, since re-sending would also reach the plugin and double-report.
    • The plugin gets each exposure once, including under concurrent evaluations.
    • A user callback that raises is logged and retried on the next evaluation, with or without the plugin.
  • No flush on set: unlike the TS SDK, setting a callback doesn't replay the defer_tracking buffer. That buffer exists to hand exposures to a client SDK via get_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 GrowthBook only. Per-user callbacks for GrowthBookClient come 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:

  • callbacks set after construction, wrapping via the getter, detaching;
  • no deferred-buffer replay;
  • a raising sink (retried with or without the plugin, the plugin gets it once);
  • the plugin still receiving after the user callback changes, plugin and constructor callback each receiving once;
  • a plugin registered twice tracking once, concurrent evaluations delivering to the plugin once;
  • two instances sharing one plugin, per-instance dedupe.

Full suite with current main merged: 1954 passed. The 2 cache fixture errors in test_growthbook_client.py also happen on main.

Size

Production code +109 / −20, tests +302, README +50.

Migration

None. New methods only. The plugin's switch to its own slot is internal.

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.
@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[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

Comment thread README.md Resolved
_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.
@vazarkevych

Copy link
Copy Markdown
Collaborator Author

@greptile review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant