bugsnag / bugsnag-ruby

BugSnag error monitoring & reporting software for rails, sinatra, rack and ruby
https://docs.bugsnag.com/platforms/ruby
MIT License
246 stars 174 forks source link

Scope feature flags to the current thread #755

Closed imjoehaines closed 1 year ago

imjoehaines commented 1 year ago

Goal

This allows users to add feature flags to Bugsnag whenever they interact with their feature flag service, e.g.

flag = get_flag_from_service('new-login-page')

if flag.enabled?
  Bugsnag.add_feature_flag('new-login-page')
  # render new login page
else
  Bugsnag.clear_feature_flag('new-login-page')
  # render old login page
end

Before this PR, the above snippet would affect all web requests/queued jobs because Bugsnag.add_feature_flag would write to a global set of flags (similar to Bugsnag.add_metadata vs event.add_metadata)

With this PR, feature flags are stored in a thread local (just like breadcrumbs), meaning each web request or queued job will have a separate set of flags

This is important because feature flags will usually be user-specific, e.g. to allow rolling out features to small groups of users at a time. With the previous implementation this was still possible but meant relying on adding all flags for the user at once in an on_error callback, whereas the new implementation allows for adding flags individually when they are used

Changeset