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
# 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.get_hostname
  @machine_ip = opts[:machine_ip] || SplitConfig.get_ip

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

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

  startup_log
end

Instance Attribute Details

#base_uri ⇒ String (readonly)

The base URL for split API end points

Returns:

  • (String) —

    The configured base URL for the split API end points



71
72
73
# File 'lib/splitclient-rb/split_config.rb', line 71

def base_uri
  @base_uri
end

#block_until_ready ⇒ Integer (readonly)

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

Returns:

  • (Integer) —

    /[FalseClass]



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

def block_until_ready
  @block_until_ready
end

#cache_adapter ⇒ Object (readonly)

The cache adapter to store splits/segments in

Returns:

  • (Object) —

    Cache adapter instance



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

def cache_adapter
  @cache_adapter
end

#connection_timeout ⇒ Int (readonly)

The connection timeout for network connections in seconds.

Returns:

  • (Int) —

    The connect timeout in seconds.



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

def connection_timeout
  @connection_timeout
end

#debug_enabled ⇒ Boolean (readonly)

The boolean that represents the state of the debug log level

Returns:

  • (Boolean) —

    The value for the debug flag



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

def debug_enabled
  @debug_enabled
end

#events_uri ⇒ String (readonly)

The base URL for split events API end points

Returns:

  • (String) —

    The configured URL for the events API end points



77
78
79
# File 'lib/splitclient-rb/split_config.rb', line 77

def events_uri
  @events_uri
end

#features_refresh_rate ⇒ Object (readonly)

Returns the value of attribute features_refresh_rate.



151
152
153
# File 'lib/splitclient-rb/split_config.rb', line 151

def features_refresh_rate
  @features_refresh_rate
end

#impressions_adapter ⇒ Object (readonly)

The cache adapter to store impressions in

Returns:

  • (Object) —

    Impressions adapter instance



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

def impressions_adapter
  @impressions_adapter
end

#impressions_queue_size ⇒ Integer (readonly)

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

Returns:

  • (Integer)


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

def impressions_queue_size
  @impressions_queue_size
end

#impressions_refresh_rate ⇒ Object (readonly)

Returns the value of attribute impressions_refresh_rate.



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

def impressions_refresh_rate
  @impressions_refresh_rate
end

#labels_enabled ⇒ Boolean (readonly)

Enable logging labels and sending potentially sensitive information

Returns:

  • (Boolean) —

    The value for the labels enabled flag



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

def labels_enabled
  @labels_enabled
end

#language ⇒ Object (readonly)

Returns the value of attribute language.



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

def language
  @language
end

#logger ⇒ Logger (readonly)

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

Returns:

  • (Logger) —

    The configured logger



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

def logger
  @logger
end

#machine_ip ⇒ Object (readonly)

Returns the value of attribute machine_ip.



145
146
147
# File 'lib/splitclient-rb/split_config.rb', line 145

def machine_ip
  @machine_ip
end

#machine_name ⇒ Object (readonly)

Returns the value of attribute machine_name.



146
147
148
# File 'lib/splitclient-rb/split_config.rb', line 146

def machine_name
  @machine_name
end

#metrics_adapter ⇒ Symbol (readonly)

The cache adapter to store metrics in

Returns:

  • (Symbol) —

    Metrics adapter



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

def metrics_adapter
  @metrics_adapter
end

#metrics_refresh_rate ⇒ Object (readonly)

Returns the value of attribute metrics_refresh_rate.



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

def metrics_refresh_rate
  @metrics_refresh_rate
end

#mode ⇒ Symbol (readonly)

The mode SDK will run

Returns:

  • (Symbol) —

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



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

def mode
  @mode
end

#read_timeout ⇒ Int (readonly)

The read timeout for network connections in seconds.

Returns:

  • (Int) —

    The timeout in seconds.



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

def read_timeout
  @read_timeout
end

#redis_namespace ⇒ Object (readonly)

Returns the value of attribute redis_namespace.



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

def redis_namespace
  @redis_namespace
end

#redis_url ⇒ Object (readonly)

Returns the value of attribute redis_url.



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

def redis_url
  @redis_url
end

#segments_refresh_rate ⇒ Object (readonly)

Returns the value of attribute segments_refresh_rate.



152
153
154
# File 'lib/splitclient-rb/split_config.rb', line 152

def segments_refresh_rate
  @segments_refresh_rate
end

#transport_debug_enabled ⇒ Boolean (readonly)

Enable to log the content retrieved from endpoints

Returns:

  • (Boolean) —

    The value for the debug flag



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

def transport_debug_enabled
  @transport_debug_enabled
end

#version ⇒ Object (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.default ⇒ Config

The default split client configuration

Returns:

  • (Config) —

    The default split client configuration.



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

def self.default
  SplitConfig.new
end

.default_base_uri ⇒ string

The default base uri for api calls

Returns:

  • (string) —

    The default base uri



177
178
179
# File 'lib/splitclient-rb/split_config.rb', line 177

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



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

def self.default_cache_adapter
  :memory
end

.default_connection_timeout ⇒ int

The default connection timeout value

Returns:

  • (int)


234
235
236
# File 'lib/splitclient-rb/split_config.rb', line 234

def self.default_connection_timeout
  5
end

.default_debug ⇒ boolean

The default debug value

Returns:

  • (boolean)


270
271
272
# File 'lib/splitclient-rb/split_config.rb', line 270

def self.default_debug
  false
end

.default_events_uri ⇒ Object



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

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

.default_features_refresh_rate ⇒ Object



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

def self.default_features_refresh_rate
  30
end

.default_impressions_queue_size ⇒ Object



254
255
256
# File 'lib/splitclient-rb/split_config.rb', line 254

def self.default_impressions_queue_size
  5000
end

.default_impressions_refresh_rate ⇒ Object



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

def self.default_impressions_refresh_rate
  60
end

.default_labels_logging ⇒ boolean

The default labels logging value

Returns:

  • (boolean)


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

def self.default_labels_logging
  true
end

.default_logger ⇒ object

The default logger object

Returns:

  • (object)


262
263
264
# File 'lib/splitclient-rb/split_config.rb', line 262

def self.default_logger
  Logger.new($stdout)
end

.default_metrics_adapter ⇒ Object



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

def self.default_metrics_adapter
  :memory
end

.default_metrics_refresh_rate ⇒ Object



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

def self.default_metrics_refresh_rate
  60
end

.default_mode ⇒ Object



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

def self.default_mode
  :standalone
end

.default_read_timeout ⇒ int

The default read timeout value

Returns:

  • (int)


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

def self.default_read_timeout
  5
end

.default_redis_namespace ⇒ Object



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

def self.default_redis_namespace
  'SPLITIO'
end

.default_redis_url ⇒ Object



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

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

.default_segments_refresh_rate ⇒ Object



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

def self.default_segments_refresh_rate
  60
end

.get_hostname ⇒ string

gets the hostname where the sdk gem is running

Returns:

  • (string)


323
324
325
326
327
328
329
330
# File 'lib/splitclient-rb/split_config.rb', line 323

def self.get_hostname
  begin
    Socket.gethostname
  rescue
    #unable to get hostname
    'localhost'
  end
end

.get_ip ⇒ string

gets the ip where the sdk gem is running

Returns:

  • (string)


336
337
338
339
340
# File 'lib/splitclient-rb/split_config.rb', line 336

def self.get_ip
  Socket.ip_address_list.detect { |intf| intf.ipv4_private? }.ip_address
rescue StandardError
  'unknown'
end

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/splitclient-rb/split_config.rb', line 185

def self.init_cache_adapter(adapter, data_structure, redis_url = nil, impressions_queue_size = nil)
  case adapter
  when :memory
    SplitIoClient::Cache::Adapters::MemoryAdapter.new(map_memory_adapter(data_structure, impressions_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

.map_memory_adapter(name, queue_size) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/splitclient-rb/split_config.rb', line 200

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_debug ⇒ boolean

The default transport_debug_enabled value

Returns:

  • (boolean)


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

def self.transport_debug
  false
end

Instance Method Details

#log_found_exception(caller, exn) ⇒ void

This method returns an undefined value.

custom logger of exceptions



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

def log_found_exception(caller, exn)
  error_traceback = "#{exn.inspect} #{exn}\n\t#{exn.backtrace.join("\n\t")}"
  error = "[splitclient-rb] Unexpected exception in #{caller}: #{error_traceback}"
  @logger.error(error)
end

#startup_log ⇒ void

This method returns an undefined value.

log which cache class was loaded and SDK mode



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

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