Skip to content

Commit 59a1811

Browse files
authored
Merge pull request #51 from launchdarkly/eb/summary-events-debug-index
fix behavior of debug & index events
2 parents ba74bb6 + 3d87ad2 commit 59a1811

File tree

2 files changed

+66
-26
lines changed

2 files changed

+66
-26
lines changed

lib/ldclient-rb/events.rb

+29-25
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,27 @@ def synchronize_for_testing(flush_workers)
158158
def dispatch_event(event, buffer)
159159
return if @disabled.value
160160

161+
# Always record the event in the summary.
162+
buffer.add_to_summary(event)
163+
164+
# Decide whether to add the event to the payload. Feature events may be added twice, once for
165+
# the event (if tracked) and once for debugging.
166+
will_add_full_event = false
167+
debug_event = nil
168+
if event[:kind] == "feature"
169+
will_add_full_event = event[:trackEvents]
170+
if should_debug_event(event)
171+
debug_event = event.clone
172+
debug_event[:debug] = true
173+
end
174+
else
175+
will_add_full_event = true
176+
end
177+
161178
# For each user we haven't seen before, we add an index event - unless this is already
162179
# an identify event for that user.
163-
if !@config.inline_users_in_events && event.has_key?(:user) && !notice_user(event[:user])
164-
if event[:kind] != "identify"
180+
if !(will_add_full_event && @config.inline_users_in_events)
181+
if event.has_key?(:user) && !notice_user(event[:user]) && event[:kind] != "identify"
165182
buffer.add_event({
166183
kind: "index",
167184
creationDate: event[:creationDate],
@@ -170,13 +187,8 @@ def dispatch_event(event, buffer)
170187
end
171188
end
172189

173-
# Always record the event in the summary.
174-
buffer.add_to_summary(event)
175-
176-
if should_track_full_event(event)
177-
# Queue the event as-is; we'll transform it into an output event when we're flushing.
178-
buffer.add_event(event)
179-
end
190+
buffer.add_event(event) if will_add_full_event
191+
buffer.add_event(debug_event) if !debug_event.nil?
180192
end
181193

182194
# Add to the set of users we've noticed, and return true if the user was already known to us.
@@ -188,21 +200,13 @@ def notice_user(user)
188200
end
189201
end
190202

191-
def should_track_full_event(event)
192-
if event[:kind] == "feature"
193-
if event[:trackEvents]
194-
true
195-
else
196-
debugUntil = event[:debugEventsUntilDate]
197-
if !debugUntil.nil?
198-
last_past = @last_known_past_time.value
199-
debugUntil > last_past && debugUntil > now_millis
200-
else
201-
false
202-
end
203-
end
203+
def should_debug_event(event)
204+
debug_until = event[:debugEventsUntilDate]
205+
if !debug_until.nil?
206+
last_past = @last_known_past_time.value
207+
debug_until > last_past && debug_until > now_millis
204208
else
205-
true
209+
false
206210
end
207211
end
208212

@@ -319,7 +323,7 @@ def make_output_events(events, summary)
319323
def make_output_event(event)
320324
case event[:kind]
321325
when "feature"
322-
is_debug = !event[:trackEvents] && event.has_key?(:debugEventsUntilDate)
326+
is_debug = event[:debug]
323327
out = {
324328
kind: is_debug ? "debug" : "feature",
325329
creationDate: event[:creationDate],
@@ -329,7 +333,7 @@ def make_output_event(event)
329333
out[:default] = event[:default] if event.has_key?(:default)
330334
out[:version] = event[:version] if event.has_key?(:version)
331335
out[:prereqOf] = event[:prereqOf] if event.has_key?(:prereqOf)
332-
if @inline_users
336+
if @inline_users || is_debug
333337
out[:user] = @user_filter.transform_user_props(event[:user])
334338
else
335339
out[:userKey] = event[:user][:key]

spec/events_spec.rb

+37-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@
108108
)
109109
end
110110

111+
it "still generates index event if inline_users is true but feature event was not tracked" do
112+
config = LaunchDarkly::Config.new(inline_users_in_events: true)
113+
@ep = subject.new("sdk_key", config, hc)
114+
flag = { key: "flagkey", version: 11 }
115+
fe = {
116+
kind: "feature", key: "flagkey", version: 11, user: user,
117+
variation: 1, value: "value", trackEvents: false
118+
}
119+
@ep.add_event(fe)
120+
121+
output = flush_and_get_events
122+
expect(output).to contain_exactly(
123+
eq(index_event(fe, user)),
124+
include(:kind => "summary")
125+
)
126+
end
127+
111128
it "sets event kind to debug if flag is temporarily in debug mode" do
112129
@ep = subject.new("sdk_key", default_config, hc)
113130
flag = { key: "flagkey", version: 11 }
@@ -121,7 +138,26 @@
121138
output = flush_and_get_events
122139
expect(output).to contain_exactly(
123140
eq(index_event(fe, user)),
124-
eq(feature_event(fe, flag, true, nil)),
141+
eq(feature_event(fe, flag, true, user)),
142+
include(:kind => "summary")
143+
)
144+
end
145+
146+
it "can be both debugging and tracking an event" do
147+
@ep = subject.new("sdk_key", default_config, hc)
148+
flag = { key: "flagkey", version: 11 }
149+
future_time = (Time.now.to_f * 1000).to_i + 1000000
150+
fe = {
151+
kind: "feature", key: "flagkey", version: 11, user: user,
152+
variation: 1, value: "value", trackEvents: true, debugEventsUntilDate: future_time
153+
}
154+
@ep.add_event(fe)
155+
156+
output = flush_and_get_events
157+
expect(output).to contain_exactly(
158+
eq(index_event(fe, user)),
159+
eq(feature_event(fe, flag, false, nil)),
160+
eq(feature_event(fe, flag, true, user)),
125161
include(:kind => "summary")
126162
)
127163
end

0 commit comments

Comments
 (0)