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)

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



27
28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/splitclient-rb/split_config.rb', line 27

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, @redis_url, false
  )
  @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, @redis_url, @impressions_queue_size
  )

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

  @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
  @machine_name = opts[:machine_name] || SplitConfig.machine_hostname
  @machine_ip = opts[:machine_ip] || SplitConfig.machine_ip

  @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

  @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, @redis_url, @events_queue_size
  )

  startup_log
end

Instance Attribute Details

#base_uriString (readonly)

The base URL for split API end points

Returns:

  • (String)

    The configured base URL for the split API end points



82
83
84
# File 'lib/splitclient-rb/split_config.rb', line 82

def base_uri
  @base_uri
end

#block_until_readyInteger (readonly)

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

Returns:

  • (Integer)

    /[FalseClass]



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

def block_until_ready
  @block_until_ready
end

#cache_adapterObject (readonly)

The cache adapter to store splits/segments in

Returns:

  • (Object)

    Cache adapter instance



105
106
107
# File 'lib/splitclient-rb/split_config.rb', line 105

def cache_adapter
  @cache_adapter
end

#connection_timeoutInt (readonly)

The connection timeout for network connections in seconds.

Returns:

  • (Int)

    The connect timeout in seconds.



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

def connection_timeout
  @connection_timeout
end

#debug_enabledBoolean (readonly)

The boolean that represents the state of the debug log level

Returns:

  • (Boolean)

    The value for the debug flag



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

def debug_enabled
  @debug_enabled
end

#events_adapterObject (readonly)

The cache adapter to store events in

Returns:

  • (Object)

    Metrics adapter



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

def events_adapter
  @events_adapter
end

#events_push_rateInteger (readonly)

The schedule time for events flush after the first one

Returns:

  • (Integer)


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

def events_push_rate
  @events_push_rate
end

#events_queue_sizeInteger (readonly)

The max size of the events queue

Returns:

  • (Integer)


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

def events_queue_size
  @events_queue_size
end

#events_uriString (readonly)

The base URL for split events API end points

Returns:

  • (String)

    The configured URL for the events API end points



88
89
90
# File 'lib/splitclient-rb/split_config.rb', line 88

def events_uri
  @events_uri
end

#features_refresh_rateObject (readonly)

Returns the value of attribute features_refresh_rate.



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

def features_refresh_rate
  @features_refresh_rate
end

#impression_listenerObject (readonly)

Returns the value of attribute impression_listener.



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

def impression_listener
  @impression_listener
end

#impression_listener_refresh_rateObject (readonly)

Returns the value of attribute impression_listener_refresh_rate.



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

def impression_listener_refresh_rate
  @impression_listener_refresh_rate
end

#impressions_adapterObject (readonly)

The cache adapter to store impressions in

Returns:

  • (Object)

    Impressions adapter instance



111
112
113
# File 'lib/splitclient-rb/split_config.rb', line 111

def impressions_adapter
  @impressions_adapter
end

#impressions_queue_sizeInteger (readonly)

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

Returns:

  • (Integer)


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

def impressions_queue_size
  @impressions_queue_size
end

#impressions_refresh_rateObject (readonly)

Returns the value of attribute impressions_refresh_rate.



171
172
173
# File 'lib/splitclient-rb/split_config.rb', line 171

def impressions_refresh_rate
  @impressions_refresh_rate
end

#labels_enabledBoolean (readonly)

Enable logging labels and sending potentially sensitive information

Returns:

  • (Boolean)

    The value for the labels enabled flag



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

def labels_enabled
  @labels_enabled
end

#languageObject (readonly)

Returns the value of attribute language.



165
166
167
# File 'lib/splitclient-rb/split_config.rb', line 165

def language
  @language
end

#loggerLogger (readonly)

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

Returns:

  • (Logger)

    The configured logger



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

def logger
  @logger
end

#machine_ipObject (readonly)

Returns the value of attribute machine_ip.



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

def machine_ip
  @machine_ip
end

#machine_nameObject (readonly)

Returns the value of attribute machine_name.



163
164
165
# File 'lib/splitclient-rb/split_config.rb', line 163

def machine_name
  @machine_name
end

#metrics_adapterSymbol (readonly)

The cache adapter to store metrics in

Returns:

  • (Symbol)

    Metrics adapter



117
118
119
# File 'lib/splitclient-rb/split_config.rb', line 117

def metrics_adapter
  @metrics_adapter
end

#metrics_refresh_rateObject (readonly)

Returns the value of attribute metrics_refresh_rate.



170
171
172
# File 'lib/splitclient-rb/split_config.rb', line 170

def metrics_refresh_rate
  @metrics_refresh_rate
end

#modeSymbol (readonly)

The mode SDK will run

Returns:

  • (Symbol)

    One of the available SDK modes: standalone, consumer, producer



94
95
96
# File 'lib/splitclient-rb/split_config.rb', line 94

def mode
  @mode
end

#read_timeoutInt (readonly)

The read timeout for network connections in seconds.

Returns:

  • (Int)

    The timeout in seconds.



99
100
101
# File 'lib/splitclient-rb/split_config.rb', line 99

def read_timeout
  @read_timeout
end

#redis_namespaceObject (readonly)

Returns the value of attribute redis_namespace.



183
184
185
# File 'lib/splitclient-rb/split_config.rb', line 183

def redis_namespace
  @redis_namespace
end

#redis_urlObject (readonly)

Returns the value of attribute redis_url.



182
183
184
# File 'lib/splitclient-rb/split_config.rb', line 182

def redis_url
  @redis_url
end

#segments_refresh_rateObject (readonly)

Returns the value of attribute segments_refresh_rate.



169
170
171
# File 'lib/splitclient-rb/split_config.rb', line 169

def segments_refresh_rate
  @segments_refresh_rate
end

#threadsObject

Returns the value of attribute threads.



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

def threads
  @threads
end

#transport_debug_enabledBoolean (readonly)

Enable to log the content retrieved from endpoints

Returns:

  • (Boolean)

    The value for the debug flag



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

def transport_debug_enabled
  @transport_debug_enabled
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.defaultConfig

The default split client configuration

Returns:

  • (Config)

    The default split client configuration.



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

def self.default
  SplitConfig.new
end

.default_base_uristring

The default base uri for api calls

Returns:

  • (string)

    The default base uri



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

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

.default_cache_adapterLocalStore

Returns configuration value for local cache store.

Returns:

  • (LocalStore)

    configuration value for local cache store



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

def self.default_cache_adapter
  :memory
end

.default_connection_timeoutint

The default connection timeout value

Returns:

  • (int)


268
269
270
# File 'lib/splitclient-rb/split_config.rb', line 268

def self.default_connection_timeout
  5
end

.default_debugboolean

The default debug value

Returns:

  • (boolean)


316
317
318
# File 'lib/splitclient-rb/split_config.rb', line 316

def self.default_debug
  false
end

.default_events_push_rateObject



296
297
298
# File 'lib/splitclient-rb/split_config.rb', line 296

def self.default_events_push_rate
  60
end

.default_events_queue_sizeObject



300
301
302
# File 'lib/splitclient-rb/split_config.rb', line 300

def self.default_events_queue_size
  500
end

.default_events_uriObject



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

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

.default_features_refresh_rateObject



272
273
274
# File 'lib/splitclient-rb/split_config.rb', line 272

def self.default_features_refresh_rate
  30
end

.default_impression_listener_refresh_rateObject



288
289
290
# File 'lib/splitclient-rb/split_config.rb', line 288

def self.default_impression_listener_refresh_rate
  0
end

.default_impressions_queue_sizeObject



292
293
294
# File 'lib/splitclient-rb/split_config.rb', line 292

def self.default_impressions_queue_size
  5000
end

.default_impressions_refresh_rateObject



284
285
286
# File 'lib/splitclient-rb/split_config.rb', line 284

def self.default_impressions_refresh_rate
  60
end

.default_labels_loggingboolean

The default labels logging value

Returns:

  • (boolean)


324
325
326
# File 'lib/splitclient-rb/split_config.rb', line 324

def self.default_labels_logging
  true
end

.default_loggerobject

The default logger object

Returns:

  • (object)


308
309
310
# File 'lib/splitclient-rb/split_config.rb', line 308

def self.default_logger
  (defined?(Rails) && Rails.logger) ? Rails.logger : Logger.new($stdout)
end

.default_metrics_adapterObject



252
253
254
# File 'lib/splitclient-rb/split_config.rb', line 252

def self.default_metrics_adapter
  :memory
end

.default_metrics_refresh_rateObject



280
281
282
# File 'lib/splitclient-rb/split_config.rb', line 280

def self.default_metrics_refresh_rate
  60
end

.default_modeObject



243
244
245
# File 'lib/splitclient-rb/split_config.rb', line 243

def self.default_mode
  :standalone
end

.default_read_timeoutint

The default read timeout value

Returns:

  • (int)


260
261
262
# File 'lib/splitclient-rb/split_config.rb', line 260

def self.default_read_timeout
  5
end

.default_redis_namespaceObject



332
333
334
# File 'lib/splitclient-rb/split_config.rb', line 332

def self.default_redis_namespace
  'SPLITIO'
end

.default_redis_urlObject



328
329
330
# File 'lib/splitclient-rb/split_config.rb', line 328

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

.default_segments_refresh_rateObject



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

def self.default_segments_refresh_rate
  60
end

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/splitclient-rb/split_config.rb', line 219

def self.init_cache_adapter(adapter, data_structure, redis_url = nil, queue_size = 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_hostnamestring

gets the hostname where the sdk gem is running

Returns:

  • (string)


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

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

.machine_ipstring

gets the ip where the sdk gem is running

Returns:

  • (string)


382
383
384
385
386
387
388
389
# File 'lib/splitclient-rb/split_config.rb', line 382

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



234
235
236
237
238
239
240
241
# File 'lib/splitclient-rb/split_config.rb', line 234

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

.transport_debugboolean

The default transport_debug_enabled value

Returns:

  • (boolean)


340
341
342
# File 'lib/splitclient-rb/split_config.rb', line 340

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



348
349
350
351
352
353
354
355
# File 'lib/splitclient-rb/split_config.rb', line 348

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_logvoid

This method returns an undefined value.

log which cache class was loaded and SDK mode



361
362
363
364
365
366
# File 'lib/splitclient-rb/split_config.rb', line 361

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