Skip to content

updated engine classes and repo helper #539

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
merged 9 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions lib/splitclient-rb/engine/common/impressions_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ def initialize(config,
@unique_keys_tracker = unique_keys_tracker
end

def build_impression(matching_key, bucketing_key, split_name, treatment, params = {})
def build_impression(matching_key, bucketing_key, split_name, treatment, impressions_disabled, params = {})
impression_data = impression_data(matching_key, bucketing_key, split_name, treatment, params[:time])

begin
case @config.impressions_mode
when :debug # In DEBUG mode we should calculate the pt only.
impression_data[:pt] = @impression_observer.test_and_set(impression_data)
when :none # In NONE mode we should track the total amount of evaluations and the unique keys.
if @config.impressions_mode == :none || impressions_disabled
@impression_counter.inc(split_name, impression_data[:m])
@unique_keys_tracker.track(split_name, matching_key)
elsif @config.impressions_mode == :debug # In DEBUG mode we should calculate the pt only.
impression_data[:pt] = @impression_observer.test_and_set(impression_data)
else # In OPTIMIZED mode we should track the total amount of evaluations and deduplicate the impressions.
impression_data[:pt] = @impression_observer.test_and_set(impression_data)

@impression_counter.inc(split_name, impression_data[:m]) unless impression_data[:pt].nil?
end
rescue StandardError => e
Expand All @@ -40,24 +37,25 @@ def build_impression(matching_key, bucketing_key, split_name, treatment, params
impression(impression_data, params[:attributes])
end

def track(impressions)
return if impressions.empty?

stats = { dropped: 0, queued: 0, dedupe: 0 }
begin
case @config.impressions_mode
when :none
return
when :debug
track_debug_mode(impressions, stats)
when :optimized
track_optimized_mode(impressions, stats)
def track(impressions_decorator)
return if impressions_decorator.empty?

impressions_decorator.each do |impression_decorator|
stats = { dropped: 0, queued: 0, dedupe: 0 }
begin
next if @config.impressions_mode == :none || impression_decorator[:disabled]

if @config.impressions_mode == :debug
track_debug_mode([impression_decorator[:impression]], stats)
else
track_optimized_mode([impression_decorator[:impression]], stats)
end
rescue StandardError => e
@config.log_found_exception(__method__.to_s, e)
ensure
record_stats(stats)
impression_router.add_bulk([impression_decorator[:impression]])
end
rescue StandardError => e
@config.log_found_exception(__method__.to_s, e)
ensure
record_stats(stats)
impression_router.add_bulk(impressions)
end
end

Expand Down Expand Up @@ -126,11 +124,10 @@ def track_debug_mode(impressions, stats)

def track_optimized_mode(impressions, stats)
optimized_impressions = impressions.select { |imp| should_queue_impression?(imp[:i]) }

stats[:dedupe] = impressions.length - optimized_impressions.length
return if optimized_impressions.empty?

stats[:dropped] = @impressions_repository.add_bulk(optimized_impressions)
stats[:dedupe] = impressions.length - optimized_impressions.length
stats[:queued] = optimized_impressions.length - stats[:dropped]
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/splitclient-rb/engine/synchronizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def start_periodic_data_recording
events_sender
start_telemetry_sync_task
end

impressions_count_sender
start_unique_keys_tracker_task
end
Expand Down Expand Up @@ -175,7 +175,7 @@ def attempt_splits_sync(target_cn, fetch_options, max_retries, retry_delay_secon

# Starts thread which loops constantly and sends impressions to the Split API
def impressions_sender
ImpressionsSender.new(@impressions_repository, @config, @impressions_api).call unless @config.impressions_mode == :none
ImpressionsSender.new(@impressions_repository, @config, @impressions_api).call
end

# Starts thread which loops constantly and sends events to the Split API
Expand All @@ -185,7 +185,7 @@ def events_sender

# Starts thread which loops constantly and sends impressions count to the Split API
def impressions_count_sender
ImpressionsCountSender.new(@config, @impression_counter, @impressions_sender_adapter).call unless @config.impressions_mode == :debug
ImpressionsCountSender.new(@config, @impression_counter, @impressions_sender_adapter).call
end

def start_telemetry_sync_task
Expand All @@ -203,7 +203,7 @@ def sync_result(success, remaining_attempts, segment_names = nil)
def sync_splits_and_segments
@config.logger.debug('Synchronizing feature flags and segments ...') if @config.debug_enabled
splits_result = @split_fetcher.fetch_splits

splits_result[:success] && @segment_fetcher.fetch_segments
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/splitclient-rb/helpers/repository_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def self.update_feature_flag_repository(feature_flag_repository, feature_flags,
next
end

unless feature_flag.key?(:impressionsDisabled)
feature_flag[:impressionsDisabled] = false
if config.debug_enabled
config.logger.debug("feature flag (#{feature_flag[:name]}) does not have impressionsDisabled field, setting it to false")
end
end

config.logger.debug("storing feature flag (#{feature_flag[:name]})") if config.debug_enabled
to_add.push(feature_flag)
end
Expand Down
Loading