statsd-metrics 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9bfc3544d39cfdd911ded83379e372a0d855aebd
4
- data.tar.gz: ff6354c12b1873bdbe16a79d4e76aa43ea8f2e75
3
+ metadata.gz: e5b6ac3f7f2b67249afdc25f685c03ef99662878
4
+ data.tar.gz: 6f5232afb0a99250aa742aedce4441afb91b3c25
5
5
  SHA512:
6
- metadata.gz: 8ec2b4ca1fe254bc20fc34d48fcfa5fe10cace43d617f50520539c8a3ca49157af5294597b48ff7fc8c41281516c509f0f6fd926c631289a9a5c55def002fff8
7
- data.tar.gz: 78c90ad5cf52879a33c45d56b59aeea810fee97288d8c67bf953a303c189f3be55617802ab0a1e0f63341985d595015329a389c2e9207b74baa143cc567b9ae9
6
+ metadata.gz: a0458b4c7d775eb0548bc455c8cf028a3cb3f1b665f64122e2d0acaa546aa7107fdc22e56d788d2d07d6fc1bb8d5087cf75d673366738aa7c8c731120936624c
7
+ data.tar.gz: 9d5128e213225bbbe0d1d28913cd13c11c07ed62a003ff4b905960af0b2a41e1d4ee3c6b8116ed4e96bf4d5be8eaaf5c36c4181c245990713d5bd54645567d28
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # StatsdMetrics
2
+ [![Gem Version](https://badge.fury.io/rb/statsd-metrics.svg)](http://badge.fury.io/rb/statsd-metrics)
2
3
 
3
4
  TODO: Write a gem description
4
5
 
@@ -3,46 +3,43 @@ require "thread"
3
3
  module Statsd
4
4
  class Reporter
5
5
 
6
- attr_accessor :queue, :workers, :running, :messages
7
- attr_reader :statsd_host, :batch_size
6
+ attr_accessor :queue, :messages
7
+ attr_reader :statsd_host, :batch_size, :pool_size
8
8
 
9
- def initialize(host, batch_size)
10
- @queue = Queue.new
11
- @workers = []
9
+ def initialize(statsd_host, batch_size, pool_size)
10
+ @pool_size = pool_size || 1
11
+ @statsd_host = statsd_host
12
12
  @messages = []
13
- @statsd_host = host
13
+ @queue = Queue.new
14
14
  @batch_size = batch_size
15
+ @pool_size.times { |i| Thread.new { Thread.current[:id] = i; spawn_thread_pool } }
15
16
  end
16
17
 
17
- def enqueue(metric)
18
- workers << Thread.new do
19
- queue << metric
20
- flush if queue.size >= batch_size
21
- end
22
- finish
23
- end
24
-
25
- private
26
-
27
- def flush
28
- begin
29
- while messages << queue.pop(true)
30
- unless messages.empty?
31
- statsd_host.send_to_socket messages.join("\n")
32
- messages.clear
18
+ def spawn_thread_pool
19
+ loop do
20
+ if queue.size >= batch_size
21
+ begin
22
+ while messages << queue.pop(true)
23
+ flush
24
+ end
25
+ rescue ThreadError
26
+ flush #flush pending queue messages
33
27
  end
34
28
  end
35
- rescue ThreadError
36
29
  end
37
30
  end
38
31
 
39
- def finish
40
- workers.each(&:join)
32
+ def enqueue(metric)
33
+ queue << metric
41
34
  end
42
35
 
43
- def kill
44
- workers.each(&:kill)
45
- puts "========> Killed!"
36
+ private
37
+
38
+ def flush
39
+ unless messages.empty?
40
+ send_to_socket messages.join("\n")
41
+ messages.clear
42
+ end
46
43
  end
47
44
 
48
45
  end
@@ -1,3 +1,3 @@
1
1
  module Statsd
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/statsd.rb CHANGED
@@ -2,6 +2,7 @@ require "statsd/version"
2
2
  require "statsd/reporter"
3
3
  require "socket"
4
4
  require "logger"
5
+ require 'forwardable'
5
6
 
6
7
  module Statsd
7
8
 
@@ -40,14 +41,10 @@ module Statsd
40
41
  end
41
42
 
42
43
  # Sends an arbitary gauge value for the given stat to the statsd server.
43
- #
44
44
  # This is useful for recording things like available disk space,
45
45
  # memory usage, and the like, which have different semantics than
46
46
  # counters.
47
47
  #
48
- # @param [String] stat stat name.
49
- # @param [Numeric] gauge value.
50
- # @param [Numeric] sample_rate sample rate, 1 for always
51
48
  # @example Report the current user count:
52
49
  # $statsd.gauge('user.count', User.count)
53
50
  def gauge(stat, value, sample_rate=1)
@@ -58,20 +55,12 @@ module Statsd
58
55
  # sample_rate determines what percentage of the time this report is sent. The
59
56
  # statsd server then uses the sample_rate to correctly track the average
60
57
  # timing for the stat.
61
- #
62
- # @param [String] stat stat name
63
- # @param [Integer] ms timing in milliseconds
64
- # @param [Numeric] sample_rate sample rate, 1 for always
65
58
  def timing(stat, ms, sample_rate=1)
66
59
  send_stat(stat, ms, :ms, sample_rate)
67
60
  end
68
61
 
69
62
  # Reports execution time of the provided block using {#timing}.
70
63
  #
71
- # @param [String] stat stat name
72
- # @param [Numeric] sample_rate sample rate, 1 for always
73
- # @yield The operation to be timed
74
- # @see #timing
75
64
  # @example Report the time (in ms) taken to activate an account
76
65
  # $statsd.time('account.activate') { @account.activate! }
77
66
  def time(stat, sample_rate=1)
@@ -103,14 +92,15 @@ module Statsd
103
92
 
104
93
  class Batch < Base
105
94
 
106
- attr_accessor :batch_size, :wait_time
95
+ extend Forwardable
96
+
97
+ attr_accessor :batch_size, :pool_size
98
+ def_delegators :@reporter, :spawn_thread_pool, :spawn_thread_pool
107
99
 
108
100
  def initialize(statsd, batch_size)
109
- @statsd = statsd
110
101
  @batch_size = batch_size
111
- @counter = statsd.counter
112
102
  @namespace = statsd.namespace
113
- @reporter = Statsd::Reporter.new(@statsd, batch_size)
103
+ @reporter = Statsd::Reporter.new(statsd, batch_size, pool_size)
114
104
  end
115
105
 
116
106
  protected
@@ -129,14 +119,10 @@ module Statsd
129
119
  if @reporter.queue.size == @reporter.batch_size
130
120
  logger = Logger.new('queue.log')
131
121
  logger.warn "Queue at Max Capacity !"
132
- #@wait_time = @counter * @counter
133
- #@counter += 1
134
122
  else
135
123
  @reporter.enqueue(msg)
136
- #@counter = 1
137
124
  end
138
125
  end
139
-
140
126
  end
141
127
 
142
128
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsd-metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - hindenbug
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-26 00:00:00.000000000 Z
11
+ date: 2015-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler