petef-statsd 0.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/statsd.rb +42 -31
  2. metadata +4 -4
@@ -15,7 +15,9 @@ end
15
15
 
16
16
  module StatsD
17
17
  @@timers = Hash.new { |h, k| h[k] = Array.new }
18
+ @@timers_mutex = Mutex.new
18
19
  @@counters = Hash.new { |h, k| h[k] = 0 }
20
+ @@counters_mutex = Mutex.new
19
21
  @@logger = Logger.new(STDERR)
20
22
  @@logger.progname = File.basename($0)
21
23
  @@flush_interval = 10
@@ -136,13 +138,17 @@ module StatsD
136
138
  end
137
139
 
138
140
  if fields[1] == "ms" # timer update
139
- @@timers[key] << fields[0].to_f
141
+ @@timers_mutex.synchronize do
142
+ @@timers[key] << fields[0].to_f
143
+ end
140
144
  elsif fields[1] == "c" # counter update
141
145
  count, sample_rate = fields[0].split("@", 2)
142
146
  sample_rate ||= 1
143
147
  #puts "count is #{count.to_f} (#{count})"
144
148
  #puts "multiplier is is #{1 / sample_rate.to_f}"
145
- @@counters[key] += count.to_f * (1 / sample_rate.to_f)
149
+ @@counters_mutex.synchronize do
150
+ @@counters[key] += count.to_f * (1 / sample_rate.to_f)
151
+ end
146
152
  else
147
153
  $stderr.puts "invalid field in update: #{bit}"
148
154
  end
@@ -153,40 +159,45 @@ module StatsD
153
159
  updates = []
154
160
  now = Time.now.to_i
155
161
 
156
- @@timers.each do |key, values|
157
- next if values.length == 0
158
- values.sort!
159
- min = values[0]
160
- max = values[-1]
161
- mean = min
162
- maxAtThreshold = min
163
- if values.length > 1
164
- threshold_index = ((100 - @@pct_threshold) / 100.0) * values.length
165
- threshold_count = values.length - threshold_index.round
166
- valid_values = values.slice(0, threshold_count)
167
- maxAtThreshold = valid_values[-1]
168
-
169
- sum = 0
170
- valid_values.each { |v| sum += v }
171
- mean = sum / valid_values.length
162
+ @@timers_mutex.synchronize do
163
+ @@timers.each do |key, values|
164
+ next if values.length == 0
165
+ values.sort!
166
+ min = values[0]
167
+ max = values[-1]
168
+ mean = min
169
+ maxAtThreshold = min
170
+ if values.length > 1
171
+ threshold_index = ((100 - @@pct_threshold) / 100.0) * values.length
172
+ threshold_count = values.length - threshold_index.round
173
+ valid_values = values.slice(0, threshold_count)
174
+ maxAtThreshold = valid_values[-1]
175
+
176
+ sum = 0
177
+ valid_values.each { |v| sum += v }
178
+ mean = sum / valid_values.length
179
+ end
180
+
181
+ suffix = @@key_suffix ? ".#{@@key_suffix}" : ""
182
+ updates << "stats.timers.#{key}.mean#{suffix} #{mean} #{now}"
183
+ updates << "stats.timers.#{key}.upper#{suffix} #{max} #{now}"
184
+ updates << "stats.timers.#{key}.upper_#{@@pct_threshold}#{suffix} " \
185
+ "#{maxAtThreshold} #{now}"
186
+ updates << "stats.timers.#{key}.lower#{suffix} #{min} #{now}"
187
+ updates << "stats.timers.#{key}.count#{suffix} #{values.length} #{now}"
172
188
  end
173
189
 
174
- suffix = @@key_suffix ? ".#{@@key_suffix}" : ""
175
- updates << "stats.timers.#{key}.mean#{suffix} #{mean} #{now}"
176
- updates << "stats.timers.#{key}.upper#{suffix} #{max} #{now}"
177
- updates << "stats.timers.#{key}.upper_#{@@pct_threshold}#{suffix} " \
178
- "#{maxAtThreshold} #{now}"
179
- updates << "stats.timers.#{key}.lower#{suffix} #{min} #{now}"
180
- updates << "stats.timers.#{key}.count#{suffix} #{values.length} #{now}"
190
+ @@timers.each { |k, v| @@timers[k] = [] }
181
191
  end
182
192
 
183
- @@counters.each do |key, value|
184
- suffix = @@key_suffix ? ".#{@@key_suffix}" : ""
185
- updates << "stats.#{key}#{suffix} #{value / @@flush_interval} #{now}"
186
- end
193
+ @@counters_mutex.synchronize do
194
+ @@counters.each do |key, value|
195
+ suffix = @@key_suffix ? ".#{@@key_suffix}" : ""
196
+ updates << "stats.#{key}#{suffix} #{value / @@flush_interval} #{now}"
197
+ end
187
198
 
188
- @@timers.each { |k, v| @@timers[k] = [] }
189
- @@counters.each { |k, v| @@counters[k] = 0 }
199
+ @@counters.each { |k, v| @@counters[k] = 0 }
200
+ end
190
201
 
191
202
  return updates.length == 0 ? nil : updates.join("\n") + "\n"
192
203
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: petef-statsd
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
9
- version: "0.4"
8
+ - 5
9
+ version: "0.5"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Pete Fritchman
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-12-16 00:00:00 -08:00
17
+ date: 2011-12-28 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency