statful-client 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/client.rb +32 -6
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46498a341544954211b78c9d1f90141be0bbbf49
4
- data.tar.gz: 94f6d45010aafa42ba164fffee13a5e7fdd0af09
3
+ metadata.gz: 9d4790086f7421d9a53045ba31a4da10bb8b2ca7
4
+ data.tar.gz: 67a6103ba572fb7380fa0e6f79357840462deea0
5
5
  SHA512:
6
- metadata.gz: 9097c70c4dacb2e54769c4b1ea97bccdf3edfe47793c04db1f958a042e41f31778eb0f8685aecd41e61bbc1ac34b594f2c5e430677703acba93de1103bfb322a
7
- data.tar.gz: 00b54ada9449590172401c7bad55619436672c73a1a73e6d1dc02a598db2bd542f4f3d7d98f8fd6ca50a8b639cdc881396f1357adaddf089b8df2b6a7f65b685
6
+ metadata.gz: 0319bdf271d17dffeda24fc1b1cdcd743d04e41e4e3a9a4978e31b2a6e8783caae7ac2ea2583b8fae223e44171e3042573e634724edd0f97148294d9430562cf
7
+ data.tar.gz: c7846b4405e5a3599a783ef07865c305ebd25d9fe7f74bc8df0178e8740058c4fa4f732fc1cab6d9138f7a5b1519a1e7675ac0e6b3d442863e3774c243572d1b
data/lib/client.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'socket'
2
2
  require 'delegate'
3
3
  require 'net/http'
4
+ require 'concurrent'
4
5
 
5
6
  # Statful Client Instance
6
7
  #
@@ -27,6 +28,7 @@ class StatfulClient
27
28
  # @option config [Integer] :sample_rate Global sample rate (as a percentage), between: (1-100)
28
29
  # @option config [String] :namespace Global default namespace
29
30
  # @option config [Integer] :flush_size Buffer flush upper size limit
31
+ # @option config [Integer] :thread_pool_size Thread pool upper size limit
30
32
  # @return [Object] The Statful client
31
33
  def initialize(config = {})
32
34
  user_config = MyHash[config].symbolize_keys
@@ -50,12 +52,16 @@ class StatfulClient
50
52
  :tags => {},
51
53
  :sample_rate => 100,
52
54
  :namespace => 'application',
53
- :flush_size => 5
55
+ :flush_size => 5,
56
+ :thread_pool_size => 5
54
57
  }
55
58
 
56
59
  @config = default_config.merge(user_config)
57
60
  @logger = @config[:logger]
58
- @buffer = []
61
+
62
+ @buffer = MyQueue.new
63
+ @pool = Concurrent::FixedThreadPool.new(@config[:thread_pool_size])
64
+
59
65
  @http = Net::HTTP.new(@config[:host], @config[:port])
60
66
  @http.use_ssl = true # must enforce use of ssl, otherwise it will raise EOFError: end of file reached
61
67
 
@@ -230,7 +236,7 @@ class StatfulClient
230
236
  # @private
231
237
  def flush
232
238
  unless @buffer.empty?
233
- message = @buffer.join('\n')
239
+ message = @buffer.to_a.join('\n')
234
240
 
235
241
  # Handle socket errors by just logging if we have a logger instantiated
236
242
  # We could eventually save the buffer state but that would require us to manage the buffer size etc.
@@ -275,10 +281,18 @@ class StatfulClient
275
281
  'Content-Type' => 'application/json',
276
282
  'M-Api-Token' => @config[:token]
277
283
  }
278
- response = @http.send_request('PUT', '/tel/v2.0/metrics', message, headers)
279
284
 
280
- if response.code != '201'
281
- @logger.debug("Failed to flush message via http with: #{response.code} - #{response.msg}") unless @logger.nil?
285
+ @pool.post do
286
+ begin
287
+ response = @http.send_request('PUT', '/tel/v2.0/metrics', message, headers)
288
+
289
+ if response.code != '201'
290
+ @logger.debug("Failed to flush message via http with: #{response.code} - #{response.msg}") unless @logger.nil?
291
+ end
292
+ rescue StandardError => ex
293
+ @logger.debug("Statful: #{ex} on #{@config[:host]}:#{@config[:port]}") unless @logger.nil?
294
+ false
295
+ end
282
296
  end
283
297
  end
284
298
 
@@ -319,4 +333,16 @@ class StatfulClient
319
333
  symbolize[self]
320
334
  end
321
335
  end
336
+
337
+ # Custom Queue implementation to add a to_a method
338
+ #
339
+ # @private
340
+ class MyQueue < Queue
341
+ # Transform Queue to Array
342
+ #
343
+ # @return [Array] queue as array
344
+ def to_a
345
+ [].tap { |array| array << pop until empty? }
346
+ end
347
+ end
322
348
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statful-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Fonseca
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-06 00:00:00.000000000 Z
11
+ date: 2016-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby-ext
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement