Repository navigation
feat(ruby): Add feature flag support #3108
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
Merged
sl0thentr0py
merged 3 commits into
getsentry:master
from
jespersandnielsen:feat/feature_flags
Nov 4, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,38 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "sentry/core_ext/object/deep_dup" | ||
|
|
||
| module Sentry | ||
| module Utils | ||
| class RingBuffer | ||
| attr_reader :values | ||
|
|
||
| def initialize(capacity) | ||
| raise ArgumentError, "capacity must be positive" unless capacity.positive? | ||
|
|
||
| @capacity = capacity | ||
| @values = {} | ||
| end | ||
|
|
||
| def set(key, value) | ||
| @values.delete(key) | ||
| @values[key] = value | ||
| @values.shift if @values.size > @capacity | ||
| end | ||
|
|
||
| def clear | ||
| @values.clear | ||
| end | ||
|
|
||
| def empty? | ||
| @values.empty? | ||
| end | ||
|
|
||
| def dup | ||
| copy = self.class.new(@capacity) | ||
| copy.values.merge!(@values) | ||
| copy | ||
| end | ||
| end | ||
| end | ||
| end |
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,94 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| RSpec.describe Sentry::Scope do | ||
| subject(:scope) { described_class.new } | ||
|
|
||
| before do | ||
| perform_basic_setup { |config| config.traces_sample_rate = 1.0 } | ||
| end | ||
|
|
||
| describe "#add_feature_flag" do | ||
| it "adds flags to event contexts" do | ||
| scope.add_feature_flag("a", true) | ||
| scope.add_feature_flag(:b, false) | ||
|
|
||
| event = scope.apply_to_event(Sentry::ErrorEvent.new(configuration: Sentry.configuration)) | ||
| expect(event.contexts[:flags]).to eq(values: [{ flag: "a", result: true }, { flag: "b", result: false }]) | ||
| end | ||
|
|
||
| it "ignores non-boolean results" do | ||
| scope.add_feature_flag("a", "yes") | ||
| scope.add_feature_flag("b", nil) | ||
| expect(scope.flags.values).to be_empty | ||
| end | ||
|
|
||
| it "moves a re-evaluated flag to the end with its new result" do | ||
| scope.add_feature_flag("a", true) | ||
| scope.add_feature_flag("b", true) | ||
| scope.add_feature_flag("a", false) | ||
| expect(scope.flags.values).to eq("b" => true, "a" => false) | ||
| end | ||
|
|
||
| it "evicts the oldest flag beyond the limit" do | ||
| (Sentry::Scope::MAX_FLAGS + 1).times { |i| scope.add_feature_flag("f#{i}", true) } | ||
| expect(scope.flags.values.size).to eq(Sentry::Scope::MAX_FLAGS) | ||
| expect(scope.flags.values.first.first).to eq("f1") | ||
| end | ||
|
|
||
| it "does not add flags to transaction events" do | ||
| scope.add_feature_flag("a", true) | ||
| transaction = Sentry.start_transaction(name: "foo", op: "bar") | ||
| event = scope.apply_to_event(Sentry::TransactionEvent.new(configuration: Sentry.configuration, transaction: transaction)) | ||
| expect(event.contexts).not_to have_key(:flags) | ||
| end | ||
|
|
||
| it "does not add flags to check-in events" do | ||
| scope.add_feature_flag("a", true) | ||
| event = Sentry::CheckInEvent.new(configuration: Sentry.configuration, slug: "foo", status: :ok) | ||
| scope.apply_to_event(event) | ||
| expect(event.contexts).not_to have_key(:flags) | ||
| end | ||
|
|
||
| it "is isolated in duplicated and updated scopes and cleared by #clear" do | ||
| scope.add_feature_flag("a", true) | ||
| copy = scope.dup | ||
| copy.add_feature_flag("b", true) | ||
| copy.add_feature_flag("a", false) | ||
| expect(scope.flags.values).to eq("a" => true) | ||
|
|
||
| updated = described_class.new | ||
| updated.update_from_scope(scope) | ||
| updated.add_feature_flag("b", true) | ||
| expect(scope.flags.values).to eq("a" => true) | ||
|
|
||
| scope.clear | ||
| expect(scope.flags.values).to be_empty | ||
| end | ||
|
|
||
| context "with an active span" do | ||
| let(:span) { Sentry::Span.new(transaction: nil) } | ||
| before { scope.set_span(span) } | ||
|
|
||
| it "records flag.evaluation.* span data, capped per span" do | ||
| scope.add_feature_flag("a", true) | ||
| expect(span.to_h[:data]["flag.evaluation.a"]).to eq(true) | ||
|
|
||
| 12.times { |i| scope.add_feature_flag("g#{i}", true) } | ||
| keys = span.to_h[:data].keys.select { |k| k.start_with?("flag.evaluation.") } | ||
| expect(keys.size).to eq(Sentry::Span::MAX_FLAGS) | ||
|
|
||
| scope.add_feature_flag("a", false) | ||
| expect(span.to_h[:data]["flag.evaluation.a"]).to eq(true) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "Sentry.add_feature_flag" do | ||
| it "adds to the current scope" do | ||
| Sentry.add_feature_flag("x", true) | ||
| expect(Sentry.get_current_scope.flags.values).to eq("x" => true) | ||
| end | ||
| end | ||
| end |
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,27 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| RSpec.describe Sentry::Utils::RingBuffer do | ||
| subject(:buffer) { described_class.new(2) } | ||
|
|
||
| it "keeps the most recent unique values" do | ||
| buffer.set("a", 1) | ||
| buffer.set("b", 2) | ||
| buffer.set("a", 3) | ||
| buffer.set("c", 4) | ||
|
|
||
| expect(buffer.values).to eq("a" => 3, "c" => 4) | ||
| end | ||
|
|
||
| it "copies and clears independently" do | ||
| buffer.set("a", 1) | ||
| copy = buffer.dup | ||
|
|
||
| copy.set("b", 2) | ||
| copy.clear | ||
|
|
||
| expect(buffer.values).to eq("a" => 1) | ||
| expect(copy).to be_empty | ||
| end | ||
| end |
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.
Span flags skip updates at cap
Medium Severity
Once a span already holds 10 feature flags,
set_flagrejects every write, including a new result for a name that is already stored. The scope buffer still keeps the latest evaluation, so error events and the active span can disagree on the sameflag.evaluation.*value after a re-evaluation.Reviewed by Cursor Bugbot for commit 28220bc. Configure here.
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.
matches python behavior 🤷 agree that the logic is a bit whatever