Class: SplitIoClient::SplitConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/split_config.rb

Overview

This class manages configuration options for the split client library. If not custom configuration is required the default configuration values will be used

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ type

Constructor for creating custom split client config

Parameters:

  • opts (Hash) (defaults to: {}) —

    optional hash with configuration options

Options Hash (opts):

  • :base_uri (String) — default: "https://sdk.split.io/api/" —

    The base URL for split API end points

  • :events_uri (String) — default: "https://events.split.io/api/" —

    The events URL for events end points

  • :read_timeout (Int) — default: 10 —

    The read timeout for network connections in seconds.

  • :connection_timeout (Int) — default: 2 —

    The connect timeout for network connections in seconds.

  • :features_refresh_rate (Int) —

    The SDK polls Split servers for changes to feature roll-out plans. This parameter controls this polling period in seconds.

  • :segments_refresh_rate (Int)
  • :metrics_refresh_rate (Int)
  • :impressions_refresh_rate (Int)
  • :logger (Object) —

    a logger to user for messages from the client. Defaults to stdout

  • :debug_enabled (Boolean) — default: false —

    The value for the debug flag

  • :impressions_queue_size (Int) —

    Size of the impressions queue in the memory repository. Once reached, newer impressions will be dropped

  • :impressions_bulk_size (Int) —

    Max number of impressions to be sent to the backend on each post

  • :impression_listener (#log) —

    this object will capture all impressions and process them through #log

  • :cache_ttl (Int) —

    Time to live in seconds for the memory cache values when using Redis.

  • :max_cache_size (Int) —

    Max number of items to be held in the memory cache before prunning when using Redis.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/splitclient-rb/split_config.rb', line 39

def initialize(opts = {})
  @base_uri = (opts[:base_uri] || SplitConfig.default_base_uri).chomp('/')
  @events_uri = (opts[:events_uri] || SplitConfig.default_events_uri).chomp('/')
  @mode = opts[:mode] || SplitConfig.default_mode
  @redis_url = opts[:redis_url] || SplitConfig.default_redis_url
  @redis_namespace = opts[:redis_namespace] ? "#{opts[:redis_namespace]}.#{SplitConfig.default_redis_namespace}" : SplitConfig.default_redis_namespace
  @cache_adapter = SplitConfig.init_cache_adapter(
    opts[:cache_adapter] || SplitConfig.default_cache_adapter, :map_adapter, nil, @redis_url
  )
  @connection_timeout = opts[:connection_timeout] || SplitConfig.default_connection_timeout
  @read_timeout = opts[:read_timeout] || SplitConfig.default_read_timeout
  @features_refresh_rate = opts[:features_refresh_rate] || SplitConfig.default_features_refresh_rate
  @segments_refresh_rate = opts[:segments_refresh_rate] || SplitConfig.default_segments_refresh_rate
  @metrics_refresh_rate = opts[:metrics_refresh_rate] || SplitConfig.default_metrics_refresh_rate

  @impressions_refresh_rate = opts[:impressions_refresh_rate] || SplitConfig.default_impressions_refresh_rate
  @impressions_queue_size = opts[:impressions_queue_size] || SplitConfig.default_impressions_queue_size
  @impressions_adapter = SplitConfig.init_cache_adapter(
    opts[:cache_adapter] || SplitConfig.default_cache_adapter, :queue_adapter, @impressions_queue_size, @redis_url
  )
  #Safeguard for users of older SDK versions.
  @disable_impressions = @impressions_queue_size == -1
  #Safeguard for users of older SDK versions.
  @impressions_bulk_size = opts[:impressions_bulk_size] || @impressions_queue_size > 0 ? @impressions_queue_size : 0

  @metrics_adapter = SplitConfig.init_cache_adapter(
    opts[:cache_adapter] || SplitConfig.default_cache_adapter, :map_adapter, nil, @redis_url
  )

  @logger = opts[:logger] || SplitConfig.default_logger
  @debug_enabled = opts[:debug_enabled] || SplitConfig.default_debug
  @transport_debug_enabled = opts[:transport_debug_enabled] || SplitConfig.default_debug
  @block_until_ready = opts[:ready] || opts[:block_until_ready] || 0

  @logger.warn 'no ready parameter has been set - incorrect control treatments could be logged' if block_until_ready == 0

  @machine_name = opts[:machine_name] || SplitConfig.machine_hostname
  @machine_ip = opts[:machine_ip] || SplitConfig.machine_ip

  @cache_ttl = opts[:cache_ttl] || SplitConfig.cache_ttl
  @max_cache_size = opts[:max_cache_size] || SplitConfig.max_cache_size

  @language = opts[:language] || 'ruby'
  @version = opts[:version] || SplitIoClient::VERSION

  @labels_enabled = opts[:labels_enabled].nil? ? SplitConfig.default_labels_logging : opts[:labels_enabled]

  @impression_listener = opts[:impression_listener]
  @impression_listener_refresh_rate = opts[:impression_listener_refresh_rate] || SplitConfig.default_impression_listener_refresh_rate

  @max_key_size = SplitConfig.max_key_size

  @threads = {}

  @events_push_rate = opts[:events_push_rate] || SplitConfig.default_events_push_rate
  @events_queue_size = opts[:events_queue_size] || SplitConfig.default_events_queue_size
  @events_adapter = SplitConfig.init_cache_adapter(
    opts[:cache_adapter] || SplitConfig.default_cache_adapter, :queue_adapter, @events_queue_size, @redis_url
  )
  @valid_mode = true

  startup_log
end

Instance Attribute Details

#base_uri ⇒ String

The base URL for split API end points

Returns:

  • (String) —

    The configured base URL for the split API end points



107
108
109
# File 'lib/splitclient-rb/split_config.rb', line 107

def base_uri
  @base_uri
end

#block_until_ready ⇒ Integer

The number of seconds to wait for SDK readiness or false to disable waiting

Returns:

  • (Integer) —

    /[FalseClass]



185
186
187
# File 'lib/splitclient-rb/split_config.rb', line 185

def block_until_ready
  @block_until_ready
end

#cache_adapter ⇒ Object

The cache adapter to store splits/segments in

Returns:

  • (Object) —

    Cache adapter instance



130
131
132
# File 'lib/splitclient-rb/split_config.rb', line 130

def cache_adapter
  @cache_adapter
end

#cache_ttl ⇒ Object

Returns the value of attribute cache_ttl.



190
191
192
# File 'lib/splitclient-rb/split_config.rb', line 190

def cache_ttl
  @cache_ttl
end

#connection_timeout ⇒ Int

The connection timeout for network connections in seconds.

Returns:

  • (Int) —

    The connect timeout in seconds.



154
155
156
# File 'lib/splitclient-rb/split_config.rb', line 154

def connection_timeout
  @connection_timeout
end

#debug_enabled ⇒ Boolean

The boolean that represents the state of the debug log level

Returns:

  • (Boolean) —

    The value for the debug flag



167
168
169
# File 'lib/splitclient-rb/split_config.rb', line 167

def debug_enabled
  @debug_enabled
end

#disable_impressions ⇒ Object

Returns the value of attribute disable_impressions.



212
213
214
# File 'lib/splitclient-rb/split_config.rb', line 212

def disable_impressions
  @disable_impressions
end

#events_adapter ⇒ Object

The cache adapter to store events in

Returns:

  • (Object) —

    Metrics adapter



148
149
150
# File 'lib/splitclient-rb/split_config.rb', line 148

def events_adapter
  @events_adapter
end

#events_push_rate ⇒ Integer

The schedule time for events flush after the first one

Returns:

  • (Integer)


225
226
227
# File 'lib/splitclient-rb/split_config.rb', line 225

def events_push_rate
  @events_push_rate
end

#events_queue_size ⇒ Integer

The max size of the events queue

Returns:

  • (Integer)


231
232
233
# File 'lib/splitclient-rb/split_config.rb', line 231

def events_queue_size
  @events_queue_size
end

#events_uri ⇒ String

The base URL for split events API end points

Returns:

  • (String) —

    The configured URL for the events API end points



113
114
115
# File 'lib/splitclient-rb/split_config.rb', line 113

def events_uri
  @events_uri
end

#features_refresh_rate ⇒ Object

Returns the value of attribute features_refresh_rate.



198
199
200
# File 'lib/splitclient-rb/split_config.rb', line 198

def features_refresh_rate
  @features_refresh_rate
end

#impression_listener ⇒ Object

Returns the value of attribute impression_listener.



203
204
205
# File 'lib/splitclient-rb/split_config.rb', line 203

def impression_listener
  @impression_listener
end

#impression_listener_refresh_rate ⇒ Object

Returns the value of attribute impression_listener_refresh_rate.



204
205
206
# File 'lib/splitclient-rb/split_config.rb', line 204

def impression_listener_refresh_rate
  @impression_listener_refresh_rate
end

#impressions_adapter ⇒ Object

The cache adapter to store impressions in

Returns:

  • (Object) —

    Impressions adapter instance



136
137
138
# File 'lib/splitclient-rb/split_config.rb', line 136

def impressions_adapter
  @impressions_adapter
end

#impressions_bulk_size ⇒ Object

Returns the value of attribute impressions_bulk_size.



211
212
213
# File 'lib/splitclient-rb/split_config.rb', line 211

def impressions_bulk_size
  @impressions_bulk_size
end

#impressions_queue_size ⇒ Integer

How big the impressions queue is before dropping impressions. -1 to disable it.

Returns:

  • (Integer)


210
211
212
# File 'lib/splitclient-rb/split_config.rb', line 210

def impressions_queue_size
  @impressions_queue_size
end

#impressions_refresh_rate ⇒ Object

Returns the value of attribute impressions_refresh_rate.



201
202
203
# File 'lib/splitclient-rb/split_config.rb', line 201

def impressions_refresh_rate
  @impressions_refresh_rate
end

#labels_enabled ⇒ Boolean

Enable logging labels and sending potentially sensitive information

Returns:

  • (Boolean) —

    The value for the labels enabled flag



179
180
181
# File 'lib/splitclient-rb/split_config.rb', line 179

def labels_enabled
  @labels_enabled
end

#language ⇒ Object

Returns the value of attribute language.



195
196
197
# File 'lib/splitclient-rb/split_config.rb', line 195

def language
  @language
end

#logger ⇒ Logger

The configured logger. The client library uses the log to print warning and error messages.

Returns:

  • (Logger) —

    The configured logger



161
162
163
# File 'lib/splitclient-rb/split_config.rb', line 161

def logger
  @logger
end

#machine_ip ⇒ Object

Returns the value of attribute machine_ip.



187
188
189
# File 'lib/splitclient-rb/split_config.rb', line 187

def machine_ip
  @machine_ip
end

#machine_name ⇒ Object

Returns the value of attribute machine_name.



188
189
190
# File 'lib/splitclient-rb/split_config.rb', line 188

def machine_name
  @machine_name
end

#max_cache_size ⇒ Object

Returns the value of attribute max_cache_size.



191
192
193
# File 'lib/splitclient-rb/split_config.rb', line 191

def max_cache_size
  @max_cache_size
end

#max_key_size ⇒ Object

Returns the value of attribute max_key_size.



193
194
195
# File 'lib/splitclient-rb/split_config.rb', line 193

def max_key_size
  @max_key_size
end

#metrics_adapter ⇒ Symbol

The cache adapter to store metrics in

Returns:

  • (Symbol) —

    Metrics adapter



142
143
144
# File 'lib/splitclient-rb/split_config.rb', line 142

def metrics_adapter
  @metrics_adapter
end

#metrics_refresh_rate ⇒ Object

Returns the value of attribute metrics_refresh_rate.



200
201
202
# File 'lib/splitclient-rb/split_config.rb', line 200

def metrics_refresh_rate
  @metrics_refresh_rate
end

#mode ⇒ Symbol

The mode SDK will run

Returns:

  • (Symbol) —

    One of the available SDK modes: standalone, consumer



119
120
121
# File 'lib/splitclient-rb/split_config.rb', line 119

def mode
  @mode
end

#read_timeout ⇒ Int

The read timeout for network connections in seconds.

Returns:

  • (Int) —

    The timeout in seconds.



124
125
126
# File 'lib/splitclient-rb/split_config.rb', line 124

def read_timeout
  @read_timeout
end

#redis_namespace ⇒ Object

Returns the value of attribute redis_namespace.



215
216
217
# File 'lib/splitclient-rb/split_config.rb', line 215

def redis_namespace
  @redis_namespace
end

#redis_url ⇒ Object

Returns the value of attribute redis_url.



214
215
216
# File 'lib/splitclient-rb/split_config.rb', line 214

def redis_url
  @redis_url
end

#segments_refresh_rate ⇒ Object

Returns the value of attribute segments_refresh_rate.



199
200
201
# File 'lib/splitclient-rb/split_config.rb', line 199

def segments_refresh_rate
  @segments_refresh_rate
end

#threads ⇒ Object

Returns the value of attribute threads.



217
218
219
# File 'lib/splitclient-rb/split_config.rb', line 217

def threads
  @threads
end

#transport_debug_enabled ⇒ Boolean

Enable to log the content retrieved from endpoints

Returns:

  • (Boolean) —

    The value for the debug flag



173
174
175
# File 'lib/splitclient-rb/split_config.rb', line 173

def transport_debug_enabled
  @transport_debug_enabled
end

#valid_mode ⇒ Object

Returns the value of attribute valid_mode.



219
220
221
# File 'lib/splitclient-rb/split_config.rb', line 219

def valid_mode
  @valid_mode
end

#version ⇒ Object

Returns the value of attribute version.



196
197
198
# File 'lib/splitclient-rb/split_config.rb', line 196

def version
  @version
end

Class Method Details

.cache_ttl ⇒ int

The default cache time to live

Returns:

  • (int)


390
391
392
# File 'lib/splitclient-rb/split_config.rb', line 390

def self.cache_ttl
  5
end

.default ⇒ Config

The default split client configuration

Returns:

  • (Config) —

    The default split client configuration.



237
238
239
# File 'lib/splitclient-rb/split_config.rb', line 237

def self.default
  SplitConfig.new
end

.default_base_uri ⇒ string

The default base uri for api calls

Returns:

  • (string) —

    The default base uri



245
246
247
# File 'lib/splitclient-rb/split_config.rb', line 245

def self.default_base_uri
  'https://sdk.split.io/api/'
end

.default_cache_adapter ⇒ LocalStore

Returns configuration value for local cache store.

Returns:

  • (LocalStore) —

    configuration value for local cache store



282
283
284
# File 'lib/splitclient-rb/split_config.rb', line 282

def self.default_cache_adapter
  :memory
end

.default_connection_timeout ⇒ int

The default connection timeout value

Returns:

  • (int)


302
303
304
# File 'lib/splitclient-rb/split_config.rb', line 302

def self.default_connection_timeout
  5
end

.default_debug ⇒ boolean

The default debug value

Returns:

  • (boolean)


358
359
360
# File 'lib/splitclient-rb/split_config.rb', line 358

def self.default_debug
  false
end

.default_events_push_rate ⇒ Object



330
331
332
# File 'lib/splitclient-rb/split_config.rb', line 330

def self.default_events_push_rate
  60
end

.default_events_queue_size ⇒ Object



334
335
336
# File 'lib/splitclient-rb/split_config.rb', line 334

def self.default_events_queue_size
  500
end

.default_events_uri ⇒ Object



249
250
251
# File 'lib/splitclient-rb/split_config.rb', line 249

def self.default_events_uri
  'https://events.split.io/api/'
end

.default_features_refresh_rate ⇒ Object



306
307
308
# File 'lib/splitclient-rb/split_config.rb', line 306

def self.default_features_refresh_rate
  30
end

.default_impression_listener_refresh_rate ⇒ Object



322
323
324
# File 'lib/splitclient-rb/split_config.rb', line 322

def self.default_impression_listener_refresh_rate
  0
end

.default_impressions_queue_size ⇒ Object



326
327
328
# File 'lib/splitclient-rb/split_config.rb', line 326

def self.default_impressions_queue_size
  5000
end

.default_impressions_refresh_rate ⇒ Object



318
319
320
# File 'lib/splitclient-rb/split_config.rb', line 318

def self.default_impressions_refresh_rate
  60
end

.default_labels_logging ⇒ boolean

The default labels logging value

Returns:

  • (boolean)


366
367
368
# File 'lib/splitclient-rb/split_config.rb', line 366

def self.default_labels_logging
  true
end

.default_logger ⇒ object

The default logger object

Returns:

  • (object)


342
343
344
345
346
347
348
349
350
# File 'lib/splitclient-rb/split_config.rb', line 342

def self.default_logger
  if defined?(Rails) && Rails.logger
    Rails.logger
  elsif ENV['SPLITCLIENT_ENV'] == 'test'
    Logger.new('/dev/null')  
  else
   Logger.new($stdout)
   end
end

.default_metrics_adapter ⇒ Object



286
287
288
# File 'lib/splitclient-rb/split_config.rb', line 286

def self.default_metrics_adapter
  :memory
end

.default_metrics_refresh_rate ⇒ Object



314
315
316
# File 'lib/splitclient-rb/split_config.rb', line 314

def self.default_metrics_refresh_rate
  60
end

.default_mode ⇒ Object



277
278
279
# File 'lib/splitclient-rb/split_config.rb', line 277

def self.default_mode
  :standalone
end

.default_read_timeout ⇒ int

The default read timeout value

Returns:

  • (int)


294
295
296
# File 'lib/splitclient-rb/split_config.rb', line 294

def self.default_read_timeout
  5
end

.default_redis_namespace ⇒ Object



374
375
376
# File 'lib/splitclient-rb/split_config.rb', line 374

def self.default_redis_namespace
  'SPLITIO'
end

.default_redis_url ⇒ Object



370
371
372
# File 'lib/splitclient-rb/split_config.rb', line 370

def self.default_redis_url
  'redis://127.0.0.1:6379/0'
end

.default_segments_refresh_rate ⇒ Object



310
311
312
# File 'lib/splitclient-rb/split_config.rb', line 310

def self.default_segments_refresh_rate
  60
end

.init_cache_adapter(adapter, data_structure, queue_size = nil, redis_url = nil) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/splitclient-rb/split_config.rb', line 253

def self.init_cache_adapter(adapter, data_structure, queue_size = nil, redis_url = nil)
  case adapter
  when :memory
    SplitIoClient::Cache::Adapters::MemoryAdapter.new(map_memory_adapter(data_structure, queue_size))
  when :redis
    begin
      require 'redis'
    rescue LoadError
      fail StandardError, 'To use Redis as a cache adapter you must include it in your Gemfile'
    end

    SplitIoClient::Cache::Adapters::RedisAdapter.new(redis_url)
  end
end

.machine_hostname ⇒ string

gets the hostname where the sdk gem is running

Returns:

  • (string)


436
437
438
439
440
# File 'lib/splitclient-rb/split_config.rb', line 436

def self.machine_hostname
  Socket.gethostname
rescue
  'localhost'.freeze
end

.machine_ip ⇒ string

gets the ip where the sdk gem is running

Returns:

  • (string)


446
447
448
449
450
451
452
453
# File 'lib/splitclient-rb/split_config.rb', line 446

def self.machine_ip
  loopback_ip = Socket.ip_address_list.find { |ip| ip.ipv4_loopback? }
  private_ip = Socket.ip_address_list.find { |ip| ip.ipv4_private? }

  addr_info = private_ip || loopback_ip

  addr_info.ip_address
end

.map_memory_adapter(name, queue_size) ⇒ Object



268
269
270
271
272
273
274
275
# File 'lib/splitclient-rb/split_config.rb', line 268

def self.map_memory_adapter(name, queue_size)
  case name
  when :map_adapter
    SplitIoClient::Cache::Adapters::MemoryAdapters::MapAdapter.new
  when :queue_adapter
    SplitIoClient::Cache::Adapters::MemoryAdapters::QueueAdapter.new(queue_size)
  end
end

.max_cache_size ⇒ int

The default max cache size

Returns:

  • (int)


397
398
399
# File 'lib/splitclient-rb/split_config.rb', line 397

def self.max_cache_size
  500
end

.max_key_size ⇒ int

The default max key size

Returns:

  • (int)


404
405
406
# File 'lib/splitclient-rb/split_config.rb', line 404

def self.max_key_size
  250
end

.transport_debug ⇒ boolean

The default transport_debug_enabled value

Returns:

  • (boolean)


382
383
384
# File 'lib/splitclient-rb/split_config.rb', line 382

def self.transport_debug
  false
end

Instance Method Details

#log_found_exception(caller, error) ⇒ void

This method returns an undefined value.

custom logger of exceptions



412
413
414
415
416
417
418
419
# File 'lib/splitclient-rb/split_config.rb', line 412

def log_found_exception(caller, error)
  message = ''

  message << "[splitclient-rb] Unexpected exception in #{caller}: #{error.inspect} #{error}"
  message << "\n\t#{error.backtrace.join("\n\t")}" if @debug_enabled

  @logger.warn(message)
end

#startup_log ⇒ void

This method returns an undefined value.

log which cache class was loaded and SDK mode



425
426
427
428
429
430
# File 'lib/splitclient-rb/split_config.rb', line 425

def startup_log
  return if ENV['SPLITCLIENT_ENV'] == 'test'

  @logger.info("Loaded Ruby SDK v#{VERSION} in the #{@mode} mode")
  @logger.info("Loaded cache class: #{@cache_adapter.class}")
end