Skip to content

Commit 255fdf2

Browse files
feat(ruby): Add feature flag support (#3108)
* feat: Add feature flag support Add Sentry.add_feature_flag and Scope#add_feature_flag to record boolean flag evaluations. Flags are kept in a 100-entry LRU buffer on the scope, attached to error events as contexts.flags.values (not transactions), and recorded on the active span as flag.evaluation.* data (max 10 per span). * deep_dup flags * add a ring buffer --------- Co-authored-by: Neel Shah <neel.shah@sentry.io>
1 parent 5def2ff commit 255fdf2

6 files changed

Lines changed: 232 additions & 3 deletions

File tree

‎sentry-ruby/lib/sentry-ruby.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ def set_context(*args)
223223
get_current_scope.set_context(*args)
224224
end
225225

226+
# Records a feature flag evaluation on the current scope and active span.
227+
# Only boolean results are recorded.
228+
# @param name [String, Symbol] the flag name
229+
# @param result [Boolean] the evaluation result
230+
# @return [void]
231+
def add_feature_flag(name, result)
232+
return unless initialized?
233+
get_current_scope.add_feature_flag(name, result)
234+
end
235+
226236
# @!method set_attributes
227237
# Updates the current scope's attributes by merging with the old value.
228238
# @param attributes_hash [Hash]

‎sentry-ruby/lib/sentry/scope.rb‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "sentry/breadcrumb_buffer"
55
require "sentry/propagation_context"
66
require "sentry/attachment"
7+
require "sentry/utils/ring_buffer"
78
require "etc"
89

910
module Sentry
@@ -27,9 +28,13 @@ class Scope
2728
:session,
2829
:attachments,
2930
:propagation_context,
30-
:attributes
31+
:attributes,
32+
:flags
3133
]
3234

35+
# Maximum number of feature flag evaluations kept on a scope.
36+
MAX_FLAGS = 100
37+
3338
attr_reader(*ATTRIBUTES)
3439

3540
# @param max_breadcrumbs [Integer] the maximum number of breadcrumbs to be stored in the scope.
@@ -61,6 +66,7 @@ def apply_to_event(event, hint = nil)
6166
event.breadcrumbs = breadcrumbs
6267
event.rack_env = rack_env if rack_env
6368
event.attachments = attachments
69+
apply_flags_to_event(event)
6470
end
6571

6672
trace_context = get_trace_context
@@ -151,6 +157,7 @@ def dup
151157
copy.attachments = attachments.dup
152158
copy.event_processors = event_processors.dup
153159
copy.attributes = attributes.deep_dup
160+
copy.flags = flags.dup
154161
copy
155162
end
156163

@@ -170,6 +177,7 @@ def update_from_scope(scope)
170177
self.propagation_context = scope.propagation_context
171178
self.attachments = scope.attachments
172179
self.attributes = scope.attributes
180+
self.flags = scope.flags.dup
173181
end
174182

175183
# Updates the scope's data from the given options.
@@ -298,6 +306,22 @@ def remove_attribute(key)
298306
@attributes.delete(key.to_s)
299307
end
300308

309+
# Records a feature flag evaluation on the scope (and the active span, if any).
310+
# Only boolean results are supported; other values are ignored.
311+
# The scope keeps the most recent {MAX_FLAGS} distinct flags (oldest evicted first).
312+
# @param name [String, Symbol] the flag name
313+
# @param result [Boolean] the evaluation result
314+
# @return [void]
315+
def add_feature_flag(name, result)
316+
return unless result == true || result == false
317+
318+
name = name.to_s
319+
flags.set(name, result)
320+
321+
add_feature_flag_to_span(name, result)
322+
nil
323+
end
324+
301325
# Sets the scope's level attribute.
302326
# @param level [String, Symbol]
303327
# @return [void]
@@ -389,6 +413,19 @@ def add_attachment(**opts)
389413

390414
private
391415

416+
def add_feature_flag_to_span(name, result)
417+
return unless span
418+
419+
span.set_flag(name, result)
420+
end
421+
422+
def apply_flags_to_event(event)
423+
return unless event.is_a?(ErrorEvent) && !flags.empty?
424+
425+
values = flags.values.map { |flag, result| { flag: flag, result: result } }
426+
event.contexts[:flags] = { values: values }
427+
end
428+
392429
def set_default_value
393430
@contexts = { os: self.class.os_context, runtime: self.class.runtime_context }
394431
@extra = {}
@@ -404,6 +441,7 @@ def set_default_value
404441
@session = nil
405442
@attachments = []
406443
@attributes = {}
444+
@flags = Utils::RingBuffer.new(MAX_FLAGS)
407445
generate_propagation_context
408446
set_new_breadcrumb_buffer
409447
end

‎sentry-ruby/lib/sentry/span.rb‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ module DataConventions
7070
}
7171

7272
DEFAULT_SPAN_ORIGIN = "manual"
73+
MAX_FLAGS = 10
74+
SPAN_FLAG_ATTRIBUTE_PREFIX = "flag.evaluation."
7375

7476
# An uuid that can be used to identify a trace.
7577
# @return [String]
@@ -104,6 +106,9 @@ module DataConventions
104106
# Span data
105107
# @return [Hash]
106108
attr_reader :data
109+
# Feature flag data
110+
# @return [Hash]
111+
attr_reader :flags
107112
# Span origin that tracks what kind of instrumentation created a span
108113
# @return [String]
109114
attr_reader :origin
@@ -142,6 +147,7 @@ def initialize(
142147
@op = op
143148
@status = status
144149
@data = {}
150+
@flags = {}
145151
@tags = {}
146152
@origin = origin || DEFAULT_SPAN_ORIGIN
147153
end
@@ -187,7 +193,7 @@ def to_h
187193
op: @op,
188194
status: @status,
189195
tags: @tags,
190-
data: @data,
196+
data: serialized_data,
191197
origin: @origin
192198
}
193199
end
@@ -203,7 +209,7 @@ def get_trace_context
203209
op: @op,
204210
status: @status,
205211
origin: @origin,
206-
data: @data
212+
data: serialized_data
207213
}
208214
end
209215

@@ -293,6 +299,14 @@ def set_data(key, value)
293299
@data[key] = value
294300
end
295301

302+
# Sets a feature flag on the span.
303+
# @param name [String] the flag name
304+
# @param result [Boolean] the evaluation result
305+
def set_flag(name, result)
306+
flag = "#{SPAN_FLAG_ATTRIBUTE_PREFIX}#{name}"
307+
@flags[flag] = result if @flags.size < MAX_FLAGS
308+
end
309+
296310
# Sets a tag to the span.
297311
# @param key [String, Symbol]
298312
# @param value [String]
@@ -305,5 +319,13 @@ def set_tag(key, value)
305319
def set_origin(origin)
306320
@origin = origin
307321
end
322+
323+
private
324+
325+
def serialized_data
326+
return @data if @flags.empty?
327+
328+
@flags.merge(@data)
329+
end
308330
end
309331
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require "sentry/core_ext/object/deep_dup"
4+
5+
module Sentry
6+
module Utils
7+
class RingBuffer
8+
attr_reader :values
9+
10+
def initialize(capacity)
11+
raise ArgumentError, "capacity must be positive" unless capacity.positive?
12+
13+
@capacity = capacity
14+
@values = {}
15+
end
16+
17+
def set(key, value)
18+
@values.delete(key)
19+
@values[key] = value
20+
@values.shift if @values.size > @capacity
21+
end
22+
23+
def clear
24+
@values.clear
25+
end
26+
27+
def empty?
28+
@values.empty?
29+
end
30+
31+
def dup
32+
copy = self.class.new(@capacity)
33+
copy.values.merge!(@values)
34+
copy
35+
end
36+
end
37+
end
38+
end
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe Sentry::Scope do
6+
subject(:scope) { described_class.new }
7+
8+
before do
9+
perform_basic_setup { |config| config.traces_sample_rate = 1.0 }
10+
end
11+
12+
describe "#add_feature_flag" do
13+
it "adds flags to event contexts" do
14+
scope.add_feature_flag("a", true)
15+
scope.add_feature_flag(:b, false)
16+
17+
event = scope.apply_to_event(Sentry::ErrorEvent.new(configuration: Sentry.configuration))
18+
expect(event.contexts[:flags]).to eq(values: [{ flag: "a", result: true }, { flag: "b", result: false }])
19+
end
20+
21+
it "ignores non-boolean results" do
22+
scope.add_feature_flag("a", "yes")
23+
scope.add_feature_flag("b", nil)
24+
expect(scope.flags.values).to be_empty
25+
end
26+
27+
it "moves a re-evaluated flag to the end with its new result" do
28+
scope.add_feature_flag("a", true)
29+
scope.add_feature_flag("b", true)
30+
scope.add_feature_flag("a", false)
31+
expect(scope.flags.values).to eq("b" => true, "a" => false)
32+
end
33+
34+
it "evicts the oldest flag beyond the limit" do
35+
(Sentry::Scope::MAX_FLAGS + 1).times { |i| scope.add_feature_flag("f#{i}", true) }
36+
expect(scope.flags.values.size).to eq(Sentry::Scope::MAX_FLAGS)
37+
expect(scope.flags.values.first.first).to eq("f1")
38+
end
39+
40+
it "does not add flags to transaction events" do
41+
scope.add_feature_flag("a", true)
42+
transaction = Sentry.start_transaction(name: "foo", op: "bar")
43+
event = scope.apply_to_event(Sentry::TransactionEvent.new(configuration: Sentry.configuration, transaction: transaction))
44+
expect(event.contexts).not_to have_key(:flags)
45+
end
46+
47+
it "does not add flags to check-in events" do
48+
scope.add_feature_flag("a", true)
49+
event = Sentry::CheckInEvent.new(configuration: Sentry.configuration, slug: "foo", status: :ok)
50+
scope.apply_to_event(event)
51+
expect(event.contexts).not_to have_key(:flags)
52+
end
53+
54+
it "is isolated in duplicated and updated scopes and cleared by #clear" do
55+
scope.add_feature_flag("a", true)
56+
copy = scope.dup
57+
copy.add_feature_flag("b", true)
58+
copy.add_feature_flag("a", false)
59+
expect(scope.flags.values).to eq("a" => true)
60+
61+
updated = described_class.new
62+
updated.update_from_scope(scope)
63+
updated.add_feature_flag("b", true)
64+
expect(scope.flags.values).to eq("a" => true)
65+
66+
scope.clear
67+
expect(scope.flags.values).to be_empty
68+
end
69+
70+
context "with an active span" do
71+
let(:span) { Sentry::Span.new(transaction: nil) }
72+
before { scope.set_span(span) }
73+
74+
it "records flag.evaluation.* span data, capped per span" do
75+
scope.add_feature_flag("a", true)
76+
expect(span.to_h[:data]["flag.evaluation.a"]).to eq(true)
77+
78+
12.times { |i| scope.add_feature_flag("g#{i}", true) }
79+
keys = span.to_h[:data].keys.select { |k| k.start_with?("flag.evaluation.") }
80+
expect(keys.size).to eq(Sentry::Span::MAX_FLAGS)
81+
82+
scope.add_feature_flag("a", false)
83+
expect(span.to_h[:data]["flag.evaluation.a"]).to eq(true)
84+
end
85+
end
86+
end
87+
88+
describe "Sentry.add_feature_flag" do
89+
it "adds to the current scope" do
90+
Sentry.add_feature_flag("x", true)
91+
expect(Sentry.get_current_scope.flags.values).to eq("x" => true)
92+
end
93+
end
94+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe Sentry::Utils::RingBuffer do
6+
subject(:buffer) { described_class.new(2) }
7+
8+
it "keeps the most recent unique values" do
9+
buffer.set("a", 1)
10+
buffer.set("b", 2)
11+
buffer.set("a", 3)
12+
buffer.set("c", 4)
13+
14+
expect(buffer.values).to eq("a" => 3, "c" => 4)
15+
end
16+
17+
it "copies and clears independently" do
18+
buffer.set("a", 1)
19+
copy = buffer.dup
20+
21+
copy.set("b", 2)
22+
copy.clear
23+
24+
expect(buffer.values).to eq("a" => 1)
25+
expect(copy).to be_empty
26+
end
27+
end

0 commit comments

Comments
 (0)