Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/amplitude/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def callback(events, code, message)
@configuration.callback.call(event, code, message) if @configuration.callback.respond_to?(:call)
event.callback(code, message)
rescue StandardError => e
@configuration.logger.exception("Error callback for event #{event}: #{e.message}")
@configuration.logger.error("Error callback for event #{event}: #{e.message}")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/amplitude/timeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def flush
@plugins[PluginType::DESTINATION].each do |destination|
destination_futures << destination.flush
rescue StandardError
logger.exception('Error for flush events')
logger.error('Error for flush events')
end
end
destination_futures
Expand Down
7 changes: 1 addition & 6 deletions lib/experiment/local/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ class LocalEvaluationClient
def initialize(api_key, config = nil)
@api_key = api_key
@config = config || LocalEvaluationConfig.new
@logger = @config.logger
@flags = nil
@flags_mutex = Mutex.new
@logger = Logger.new($stdout)
@logger.level = if @config.debug
Logger::DEBUG
else
Logger::INFO
end
raise ArgumentError, 'Experiment API key is empty' if @api_key.nil? || @api_key.empty?

@engine = Evaluation::Engine.new
Expand Down
24 changes: 21 additions & 3 deletions lib/experiment/local/config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'logger'

module AmplitudeExperiment
module ServerZone
US = 'US'.freeze
Expand All @@ -9,11 +11,17 @@ class LocalEvaluationConfig
# Default server url
DEFAULT_SERVER_URL = 'https://api.lab.amplitude.com'.freeze
EU_SERVER_URL = 'https://flag.lab.eu.amplitude.com'.freeze
DEFAULT_LOGDEV = $stdout
DEFAULT_LOG_LEVEL = Logger::ERROR

# Set to true to log some extra information to the console.
# @return [Boolean] the value of debug
attr_accessor :debug

# Set the client logger to a user defined [Logger]
# @return [Logger] the logger instance of the client
attr_accessor :logger

# The server endpoint from which to request variants.
# @return [String] the value of server url
attr_accessor :server_url
Expand All @@ -35,16 +43,26 @@ class LocalEvaluationConfig
attr_accessor :cohort_sync_config

# @param [Boolean] debug Set to true to log some extra information to the console.
# @param [Logger] logger instance to be used for all client logging behavior
# @param [String] server_url The server endpoint from which to request variants.
# @param [String] server_zone Location of the Amplitude data center to get flags and cohorts from, US or EU
# @param [Hash] bootstrap The value of bootstrap.
# @param [long] flag_config_polling_interval_millis The value of flag config polling interval in million seconds.
# @param [AssignmentConfig] assignment_config Configuration for automatically tracking assignment events after an evaluation.
# @param [CohortSyncConfig] cohort_sync_config Configuration for downloading cohorts required for flag evaluation
def initialize(server_url: DEFAULT_SERVER_URL, server_zone: ServerZone::US, bootstrap: {},
flag_config_polling_interval_millis: 30_000, debug: false, assignment_config: nil,
def initialize(server_url: DEFAULT_SERVER_URL,
server_zone: ServerZone::US,
bootstrap: {},
flag_config_polling_interval_millis: 30_000,
debug: false,
logger: nil,
assignment_config: nil,
cohort_sync_config: nil)
@debug = debug || false
@logger = logger
if logger.nil?
@logger = Logger.new(DEFAULT_LOGDEV)
@logger.level = debug ? Logger::DEBUG : DEFAULT_LOG_LEVEL
end
@server_url = server_url
@server_zone = server_zone
@cohort_sync_config = cohort_sync_config
Expand Down
7 changes: 1 addition & 6 deletions lib/experiment/remote/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ class RemoteEvaluationClient
def initialize(api_key, config = nil)
@api_key = api_key
@config = config || RemoteEvaluationConfig.new
@logger = Logger.new($stdout)
@logger.level = if @config.debug
Logger::DEBUG
else
Logger::INFO
end
@logger = @config.logger
endpoint = "#{@config.server_url}/sdk/v2/vardata?v=0"
@uri = URI(endpoint)
raise ArgumentError, 'Experiment API key is empty' if @api_key.nil? || @api_key.empty?
Expand Down
28 changes: 24 additions & 4 deletions lib/experiment/remote/config.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
require 'logger'

module AmplitudeExperiment
# Configuration
class RemoteEvaluationConfig
# Default server url
DEFAULT_SERVER_URL = 'https://api.lab.amplitude.com'.freeze
DEFAULT_LOGDEV = $stdout
DEFAULT_LOG_LEVEL = Logger::ERROR

# Set to true to log some extra information to the console.
# @return [Boolean] the value of debug
attr_accessor :debug

# Set the client logger to a user defined [Logger]
# @return [Logger] the logger instance of the client
attr_accessor :logger

# The server endpoint from which to request variants.
# @return [Boolean] the value of server url
attr_accessor :server_url
Expand Down Expand Up @@ -43,6 +51,7 @@ class RemoteEvaluationConfig
attr_accessor :fetch_retry_timeout_millis

# @param [Boolean] debug Set to true to log some extra information to the console.
# @param [Logger] logger instance to be used for all client logging behavior
# @param [String] server_url The server endpoint from which to request variants.
# @param [Integer] connect_timeout_millis The request connection open timeout, in milliseconds, used when
# fetching variants triggered by calling start() or setUser().
Expand All @@ -55,10 +64,21 @@ class RemoteEvaluationConfig
# greater than the max, the max is used for all subsequent retries.
# @param [Float] fetch_retry_backoff_scalar Scales the minimum backoff exponentially.
# @param [Integer] fetch_retry_timeout_millis The request timeout for retrying fetch requests.
def initialize(debug: false, server_url: DEFAULT_SERVER_URL, connect_timeout_millis: 60_000, fetch_timeout_millis: 10_000, fetch_retries: 0,
fetch_retry_backoff_min_millis: 500, fetch_retry_backoff_max_millis: 10_000,
fetch_retry_backoff_scalar: 1.5, fetch_retry_timeout_millis: 10_000)
@debug = debug
def initialize(debug: false,
logger: nil,
server_url: DEFAULT_SERVER_URL,
connect_timeout_millis: 60_000,
fetch_timeout_millis: 10_000,
fetch_retries: 0,
fetch_retry_backoff_min_millis: 500,
fetch_retry_backoff_max_millis: 10_000,
fetch_retry_backoff_scalar: 1.5,
fetch_retry_timeout_millis: 10_000)
@logger = logger
if logger.nil?
@logger = Logger.new(DEFAULT_LOGDEV)
@logger.level = debug ? Logger::DEBUG : DEFAULT_LOG_LEVEL
end
@server_url = server_url
@connect_timeout_millis = connect_timeout_millis
@fetch_timeout_millis = fetch_timeout_millis
Expand Down
24 changes: 24 additions & 0 deletions spec/experiment/local/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ def setup_stub
it 'error if api_key is empty' do
expect { LocalEvaluationClient.new('') }.to raise_error(ArgumentError)
end

it 'uses custom logger when provided' do
custom_logger = Logger.new($stdout)
config = LocalEvaluationConfig.new(logger: custom_logger)
client = LocalEvaluationClient.new(api_key, config)

expect(client.instance_variable_get(:@logger)).to eq(custom_logger)
end

it 'debug flag overrides logger level to DEBUG when not provided a custom logger ' do
config = LocalEvaluationConfig.new(debug: true)
client = LocalEvaluationClient.new(api_key, config)

expect(client.instance_variable_get(:@logger).level).to eq(Logger::DEBUG)
end

it 'debug flag does not modify logger level when provided a custom logger' do
custom_logger = Logger.new($stdout)
custom_logger.level = Logger::WARN
config = LocalEvaluationConfig.new(logger: custom_logger, debug: true)
client = LocalEvaluationClient.new(api_key, config)

expect(client.instance_variable_get(:@logger).level).to eq(Logger::WARN)
end
end

describe '#evaluation' do
Expand Down
24 changes: 24 additions & 0 deletions spec/experiment/remote/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ module AmplitudeExperiment
it 'raises an error if api_key is empty' do
expect { RemoteEvaluationClient.new('') }.to raise_error(ArgumentError)
end

it 'uses custom logger when provided' do
custom_logger = Logger.new($stdout)
config = RemoteEvaluationConfig.new(logger: custom_logger)
client = RemoteEvaluationClient.new(api_key, config)

expect(client.instance_variable_get(:@logger)).to eq(custom_logger)
end

it 'debug flag overrides logger level to DEBUG when not provided a custom logger ' do
config = RemoteEvaluationConfig.new(debug: true)
client = RemoteEvaluationClient.new(api_key, config)

expect(client.instance_variable_get(:@logger).level).to eq(Logger::DEBUG)
end

it 'debug flag does not modify logger level when provided a custom logger' do
custom_logger = Logger.new($stdout)
custom_logger.level = Logger::WARN
config = RemoteEvaluationConfig.new(logger: custom_logger, debug: true)
client = RemoteEvaluationClient.new(api_key, config)

expect(client.instance_variable_get(:@logger).level).to eq(Logger::WARN)
end
end

response_with_key = '{"sdk-ci-test":{"key":"on","payload":"payload"}}'
Expand Down