splitclient-rb 6.3.0 → 6.4.0.pre.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 155d0661d53fb4c60d68bd8e03a31bf925a3afbfc2ff3bf08283d45ea1ec655e
4
- data.tar.gz: 5242be792ab0dd2eaa54f191821ae9b3356a171f70c1cbcb549abc66e858c6e8
3
+ metadata.gz: 73ec7584b175071a904ecddbb077367364a288c179c8d45ee67f05803caf2632
4
+ data.tar.gz: 1beea7c46b71a7faa4c8c25ae4b0e473fcb5ada3f8a4cd7b5f95212fb7292445
5
5
  SHA512:
6
- metadata.gz: 10263a795124c996f120177474e397224a42216a326bb9358e7f4bd69d86d409c4ab69fa0f2f71db1f6f375ac79828f20c4d4d369bf27bab500f44759e2c778e
7
- data.tar.gz: 2298f840ae08656c85cea605eea58ef5b7e755ccf78d08a65b61366e54d745be646738cfa4e772196cd538ff70613771c712f0eaba4a3622d71d6478ed44ac0b
6
+ metadata.gz: 2020fd9cc79da50ea7e59151654d877488258b1a3b3f88b1eaa3c4dc25dcd12e9c7bd7e797b4a64bae89d7cfbd1db5621a7abe4af9eb7b145e5369788d5996b0
7
+ data.tar.gz: 60d9cc6f00a52bca4e7b89100c42f21d6ec21ec8780f656b861979a7673168c44fa74d4cbd0bfe721a2fc4baf3644463285fa534a4089df9bedf1894742d1530
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [ ![Codeship Status for splitio/ruby-client](https://app.codeship.com/projects/306c6b60-c164-0133-e179-16471d4e6045/status?branch=master)](https://app.codeship.com/projects/137510)
1
+ [![Build Status](https://travis-ci.com/splitio/ruby-client.svg?branch=master)](https://travis-ci.com/splitio/ruby-client)
2
2
 
3
3
  # Split Ruby SDK
4
4
 
@@ -35,7 +35,7 @@ Split has built and maintains a SDKs for:
35
35
 
36
36
  For a comprehensive list of opensource projects visit our [Github page](https://github.com/splitio?utf8=%E2%9C%93&query=%20only%3Apublic%20).
37
37
 
38
- **Learn more about Split:**
38
+ **Learn more about Split:**
39
39
 
40
40
  Visit [split.io/product](https://www.split.io/product) for an overview of Split, or visit our documentation at [docs.split.io](http://docs.split.io) for more detailed information.
41
41
 
@@ -39,6 +39,9 @@ module SplitIoClient
39
39
  items
40
40
  end
41
41
 
42
+ def length
43
+ @current_size.value
44
+ end
42
45
  end
43
46
  end
44
47
  end
@@ -3,27 +3,25 @@ module SplitIoClient
3
3
  module Repositories
4
4
  module Events
5
5
  class MemoryRepository < EventsRepository
6
- EVENTS_SLICE = 100
6
+ EVENTS_MAX_SIZE_BYTES = 5242880
7
7
 
8
8
  def initialize(adapter)
9
9
  @adapter = adapter
10
+ @size = 0
10
11
  end
11
12
 
12
- def add(key, traffic_type, event_type, time, value)
13
- @adapter.add_to_queue(m: metadata, e: event(key, traffic_type, event_type, time, value))
14
- rescue ThreadError # queue is full
15
- if SplitIoClient.configuration.debug_enabled
16
- SplitIoClient.configuration.logger.warn("Dropping events. Current size is #{SplitIoClient.configuration.events_queue_size}. " \
17
- "Consider increasing events_queue_size")
18
- end
19
- @adapter.clear
20
- end
13
+ def add(key, traffic_type, event_type, time, value, properties, event_size)
14
+ @adapter.add_to_queue(m: metadata, e: event(key, traffic_type, event_type, time, value, properties))
15
+ @size += event_size
16
+
17
+ post_events if @size >= EVENTS_MAX_SIZE_BYTES || @adapter.length == SplitIoClient.configuration.events_queue_size
21
18
 
22
- def batch
23
- @adapter.get_batch(EVENTS_SLICE)
19
+ rescue StandardError => error
20
+ SplitIoClient.configuration.log_found_exception(__method__.to_s, error)
24
21
  end
25
22
 
26
23
  def clear
24
+ @size = 0
27
25
  @adapter.clear
28
26
  end
29
27
  end
@@ -3,36 +3,26 @@ module SplitIoClient
3
3
  module Repositories
4
4
  module Events
5
5
  class RedisRepository < EventsRepository
6
- EVENTS_SLICE = 100
7
6
 
8
7
  def initialize(adapter)
9
8
  @adapter = adapter
10
9
  end
11
10
 
12
- def add(key, traffic_type, event_type, time, value)
11
+ def add(key, traffic_type, event_type, time, value, properties, size)
13
12
  @adapter.add_to_queue(
14
13
  namespace_key('.events'),
15
- { m: metadata, e: event(key, traffic_type, event_type, time, value) }.to_json,
14
+ { m: metadata, e: event(key, traffic_type, event_type, time, value, properties) }.to_json
16
15
  )
17
16
  end
18
17
 
19
- def get_events(number_of_events = 0)
20
- @adapter.get_from_queue(namespace_key('.events'), number_of_events).map do |e|
18
+ def clear
19
+ @adapter.get_from_queue(namespace_key('.events'), 0).map do |e|
21
20
  JSON.parse(e, symbolize_names: true)
22
21
  end
23
22
  rescue StandardError => e
24
23
  SplitIoClient.configuration.logger.error("Exception while clearing events cache: #{e}")
25
24
  []
26
25
  end
27
-
28
- def batch
29
- get_events(EVENTS_SLICE)
30
- end
31
-
32
- def clear
33
- get_events
34
- end
35
-
36
26
  end
37
27
  end
38
28
  end
@@ -4,15 +4,23 @@ module SplitIoClient
4
4
  # Repository which forwards events interface to the selected adapter
5
5
  class EventsRepository < Repository
6
6
  extend Forwardable
7
- def_delegators :@adapter, :add, :clear, :batch
7
+ def_delegators :@repository, :add, :clear
8
8
 
9
- def initialize(adapter)
10
- @adapter = case adapter.class.to_s
9
+ def initialize(adapter, api_key)
10
+ @repository = case adapter.class.to_s
11
11
  when 'SplitIoClient::Cache::Adapters::MemoryAdapter'
12
12
  Repositories::Events::MemoryRepository.new(adapter)
13
13
  when 'SplitIoClient::Cache::Adapters::RedisAdapter'
14
14
  Repositories::Events::RedisRepository.new(adapter)
15
15
  end
16
+
17
+ @api_key = api_key
18
+ end
19
+
20
+ def post_events
21
+ events_api.post(self.clear)
22
+ rescue StandardError => error
23
+ SplitIoClient.configuration.log_found_exception(__method__.to_s, error)
16
24
  end
17
25
 
18
26
  protected
@@ -25,15 +33,22 @@ module SplitIoClient
25
33
  }
26
34
  end
27
35
 
28
- def event(key, traffic_type, event_type, time, value)
36
+ def event(key, traffic_type, event_type, time, value, properties)
29
37
  {
30
38
  key: key,
31
39
  trafficTypeName: traffic_type,
32
40
  eventTypeId: event_type,
33
41
  value: value,
34
- timestamp: time
42
+ timestamp: time,
43
+ properties: properties
35
44
  }.reject { |_, v| v.nil? }
36
45
  end
46
+
47
+ private
48
+
49
+ def events_api
50
+ @events_api ||= SplitIoClient::Api::Events.new(@api_key)
51
+ end
37
52
  end
38
53
  end
39
54
  end
@@ -4,9 +4,8 @@ module SplitIoClient
4
4
  module Cache
5
5
  module Senders
6
6
  class EventsSender
7
- def initialize(events_repository, api_key)
7
+ def initialize(events_repository)
8
8
  @events_repository = events_repository
9
- @api_key = api_key
10
9
  end
11
10
 
12
11
  def call
@@ -31,7 +30,7 @@ module SplitIoClient
31
30
  SplitIoClient.configuration.logger.info('Starting events service')
32
31
 
33
32
  loop do
34
- post_events(false)
33
+ post_events
35
34
 
36
35
  sleep(SplitIoClient::Utilities.randomize_interval(SplitIoClient.configuration.events_push_rate))
37
36
  end
@@ -43,15 +42,8 @@ module SplitIoClient
43
42
  end
44
43
  end
45
44
 
46
- def post_events(fetch_all_events = true)
47
- events = fetch_all_events ? @events_repository.clear : @events_repository.batch
48
- events_api.post(events)
49
- rescue StandardError => error
50
- SplitIoClient.configuration.log_found_exception(__method__.to_s, error)
51
- end
52
-
53
- def events_api
54
- @events_api ||= SplitIoClient::Api::Events.new(@api_key)
45
+ def post_events
46
+ @events_repository.post_events
55
47
  end
56
48
  end
57
49
  end
@@ -1,4 +1,6 @@
1
1
  module SplitIoClient
2
+ EVENTS_SIZE_THRESHOLD = 32768
3
+ EVENT_AVERAGE_SIZE = 1024
2
4
 
3
5
  class SplitClient
4
6
  #
@@ -112,10 +114,22 @@ module SplitIoClient
112
114
  @impression_router ||= SplitIoClient::ImpressionRouter.new
113
115
  end
114
116
 
115
- def track(key, traffic_type_name, event_type, value = nil)
116
- return false unless valid_client && SplitIoClient::Validators.valid_track_parameters(key, traffic_type_name, event_type, value)
117
+ def track(key, traffic_type_name, event_type, value = nil, properties = nil)
118
+ return false unless valid_client && SplitIoClient::Validators.valid_track_parameters(key, traffic_type_name, event_type, value, properties)
119
+
120
+ properties_size = EVENT_AVERAGE_SIZE
121
+
122
+ if !properties.nil?
123
+ properties, size = validate_properties(properties)
124
+ properties_size += size
125
+ if (properties_size > EVENTS_SIZE_THRESHOLD)
126
+ SplitIoClient.configuration.logger.error("The maximum size allowed for the properties is #{EVENTS_SIZE_THRESHOLD}. Current is #{properties_size}. Event not queued")
127
+ return false
128
+ end
129
+ end
130
+
117
131
  begin
118
- @events_repository.add(key.to_s, traffic_type_name.downcase, event_type.to_s, (Time.now.to_f * 1000).to_i, value)
132
+ @events_repository.add(key.to_s, traffic_type_name.downcase, event_type.to_s, (Time.now.to_f * 1000).to_i, value, properties, properties_size)
119
133
  true
120
134
  rescue StandardError => error
121
135
  SplitIoClient.configuration.log_found_exception(__method__.to_s, error)
@@ -164,6 +178,30 @@ module SplitIoClient
164
178
 
165
179
  private
166
180
 
181
+ def validate_properties(properties)
182
+ properties_count = 0
183
+ size = 0
184
+
185
+ fixed_properties = properties.each_with_object({}) { |(key, value), result|
186
+
187
+ if(key.is_a?(String) || key.is_a?(Symbol))
188
+ properties_count += 1
189
+ size += variable_size(key)
190
+ if value.is_a?(String) || value.is_a?(Symbol) || value.is_a?(Numeric) || value.is_a?(TrueClass) || value.is_a?(FalseClass) || value.nil?
191
+ result[key] = value
192
+ size += variable_size(value)
193
+ else
194
+ SplitIoClient.configuration.logger.warn("Property #{key} is of invalid type. Setting value to nil")
195
+ result[key] = nil
196
+ end
197
+ end
198
+ }
199
+
200
+ SplitIoClient.configuration.logger.warn('Event has more than 300 properties. Some of them will be trimmed when processed') if properties_count > 300
201
+
202
+ return fixed_properties, size
203
+ end
204
+
167
205
  def valid_client
168
206
  if @destroyed
169
207
  SplitIoClient.configuration.logger.error('Client has already been destroyed - no calls possible')
@@ -279,5 +317,9 @@ module SplitIoClient
279
317
 
280
318
  parsed_treatment(multiple, treatment_data)
281
319
  end
320
+
321
+ def variable_size(value)
322
+ value.is_a?(String) ? value.length : 0
323
+ end
282
324
  end
283
325
  end
@@ -75,7 +75,7 @@ module SplitIoClient
75
75
 
76
76
  # Starts thread which loops constantly and sends events to the Split API
77
77
  def events_sender
78
- EventsSender.new(@events_repository, @api_key).call
78
+ EventsSender.new(@events_repository).call
79
79
  end
80
80
  end
81
81
  end
@@ -29,7 +29,7 @@ module SplitIoClient
29
29
  @segments_repository = SegmentsRepository.new(@cache_adapter)
30
30
  @impressions_repository = ImpressionsRepository.new(SplitIoClient.configuration.impressions_adapter)
31
31
  @metrics_repository = MetricsRepository.new(SplitIoClient.configuration.metrics_adapter)
32
- @events_repository = EventsRepository.new(SplitIoClient.configuration.events_adapter)
32
+ @events_repository = EventsRepository.new(SplitIoClient.configuration.events_adapter, @api_key)
33
33
 
34
34
  if SplitIoClient.configuration.mode == :standalone && SplitIoClient.configuration.block_until_ready > 0
35
35
  @sdk_blocker = SDKBlocker.new(@splits_repository, @segments_repository)
@@ -16,11 +16,12 @@ module SplitIoClient
16
16
  valid_split_names?(method, split_names)
17
17
  end
18
18
 
19
- def valid_track_parameters(key, traffic_type_name, event_type, value)
19
+ def valid_track_parameters(key, traffic_type_name, event_type, value, properties)
20
20
  valid_track_key?(key) &&
21
21
  valid_traffic_type_name?(traffic_type_name) &&
22
22
  valid_event_type?(event_type) &&
23
- valid_value?(value)
23
+ valid_value?(value) &&
24
+ valid_properties?(properties)
24
25
  end
25
26
 
26
27
  def valid_split_parameters(split_name)
@@ -253,5 +254,14 @@ module SplitIoClient
253
254
 
254
255
  true
255
256
  end
257
+
258
+ def valid_properties?(properties)
259
+ unless properties.is_a?(Hash) || properties.nil?
260
+ SplitIoClient.configuration.logger.error('track: properties must be a Hash')
261
+ return false
262
+ end
263
+
264
+ true
265
+ end
256
266
  end
257
267
  end
@@ -1,3 +1,3 @@
1
1
  module SplitIoClient
2
- VERSION = '6.3.0'
2
+ VERSION = '6.4.0.pre.rc1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: splitclient-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.3.0
4
+ version: 6.4.0.pre.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Split Software
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-30 00:00:00.000000000 Z
11
+ date: 2019-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: allocation_stats
@@ -403,12 +403,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
403
403
  version: '0'
404
404
  required_rubygems_version: !ruby/object:Gem::Requirement
405
405
  requirements:
406
- - - ">="
406
+ - - ">"
407
407
  - !ruby/object:Gem::Version
408
- version: '0'
408
+ version: 1.3.1
409
409
  requirements: []
410
- rubyforge_project:
411
- rubygems_version: 2.7.6
410
+ rubygems_version: 3.0.3
412
411
  signing_key:
413
412
  specification_version: 4
414
413
  summary: Ruby client for split SDK.