Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a TTL of 1,000 for memcache sets and fix the way gauges are stored

  • Loading branch information...
commit 5963b0f5775b8059719f3d156cb88f17dd3c0d93 1 parent 5eeac38
@eric authored
View
1  Gemfile
@@ -6,7 +6,6 @@ gem 'faraday'
gem 'faraday_middleware'
gem 'dalli'
gem 'activesupport', '~> 2.3', :require => 'active_support'
-gem 'always_verify_ssl_certificates'
group :production do
gem 'pg'
View
2  Gemfile.lock
@@ -3,7 +3,6 @@ GEM
specs:
activesupport (2.3.11)
addressable (2.2.6)
- always_verify_ssl_certificates (0.3.0)
ansi (1.4.2)
dalli (1.1.4)
faraday (0.7.6)
@@ -39,7 +38,6 @@ PLATFORMS
DEPENDENCIES
activesupport (~> 2.3)
- always_verify_ssl_certificates
dalli
faraday
faraday_middleware
View
4 lib/metriks_log_webhook/app.rb
@@ -37,7 +37,9 @@ class App < Sinatra::Base
end
end
- settings.metrics_client.submit(metric_list.to_hash)
+ body = metric_list.to_hash
+
+ settings.metrics_client.submit(body)
end
end
end
View
19 lib/metriks_log_webhook/average_gauge.rb
@@ -1,9 +1,10 @@
module MetriksLogWebhook
class AverageGauge
- def initialize(name, time, options = {})
+ def initialize(name, time, source, cache)
@name = name
@time = time.to_i
- @source = options[:source]
+ @source = source
+ @cache = cache
@data = {
:count => 0,
@@ -12,8 +13,10 @@ def initialize(name, time, options = {})
:min => nil,
:max => nil
}
+
+ load
end
-
+
def mark(value)
@data[:count] += 1
@data[:sum] += value
@@ -30,7 +33,7 @@ def mark(value)
def merge!(other)
other = other.symbolize_keys
-
+
@data[:count] += other[:count]
@data[:sum] += other[:sum]
@data[:sum_of_squares] += other[:sum_of_squares]
@@ -48,14 +51,14 @@ def key
"average_gauge:#{@name}:#{@time}:#{@source}"
end
- def load(memcached)
- if value = memcached.get(key)
+ def load
+ if value = @cache.get(key)
merge!(value)
end
end
- def save(memcached)
- memcached.set(key, @data)
+ def save
+ @cache.set(key, @data, 1000)
end
def to_hash
View
10 lib/metriks_log_webhook/metric_list.rb
@@ -60,19 +60,17 @@ def to_hash
def average_gauge(name, time, source, value)
time = rounded_time(time)
key = [ name, time, source ].join('/')
- @gauges[key] ||= AverageGauge.new(name, time, :source => source)
- @gauges[key].load(@memcached)
+ @gauges[key] ||= AverageGauge.new(name, time, source, @memcached)
@gauges[key].mark(value)
- @gauges[key].save(@memcached)
+ @gauges[key].save
end
def sum_gauge(name, time, source, value)
time = rounded_time(time)
key = [ name, time, source ].join('/')
- @gauges[key] ||= SumGauge.new(name, time, :source => source)
- @gauges[key].load(@memcached)
+ @gauges[key] ||= SumGauge.new(name, time, source, @memcached)
@gauges[key].mark(value)
- @gauges[key].save(@memcached)
+ @gauges[key].save
end
def counter(name, time, source, value)
View
19 lib/metriks_log_webhook/sum_gauge.rb
@@ -1,22 +1,25 @@
module MetriksLogWebhook
class SumGauge
- def initialize(name, time, options = {})
+ def initialize(name, time, source, cache)
@name = name
@time = time.to_i
- @source = options[:source]
+ @source = source
+ @cache = cache
@data = {
:value => 0
}
+
+ load
end
-
+
def mark(value)
@data[:value] += value
end
def merge!(other)
other = other.symbolize_keys
-
+
@data[:value] += other[:value]
end
@@ -24,14 +27,14 @@ def key
"sum_gauge:#{@name}:#{@time}:#{@source}"
end
- def load(memcached)
- if value = memcached.get(key)
+ def load
+ if value = @cache.get(key)
merge!(value)
end
end
- def save(memcached)
- memcached.set(key, @data)
+ def save
+ @cache.set(key, @data, 1000)
end
def to_hash
View
28 test/metric_list_test.rb
@@ -11,7 +11,7 @@ def setup
def test_empty_to_hash
assert_equal({ :gauges => [], :counters => [] }, @list.to_hash)
end
-
+
def test_counter
data = {
:name => 'test',
@@ -20,12 +20,12 @@ def test_counter
:count => 8273,
:type => 'counter'
}
-
+
@list.add(data)
-
+
assert_equal 8273, @list.to_hash[:counters][0][:value]
end
-
+
def test_meter
data = {
:name => 'test',
@@ -35,13 +35,13 @@ def test_meter
:one_minute_rate => 822,
:type => 'meter'
}
-
+
@list.add(data)
-
- assert_match /test\..*/, @list.to_hash[:gauges][0][:name]
- assert_equal 2, @list.to_hash[:gauges].length
+
+ assert_match /test/, @list.to_hash[:gauges][0][:name]
+ assert_equal 1, @list.to_hash[:gauges].length
end
-
+
def test_timer
data = {
:name => 'test',
@@ -51,13 +51,13 @@ def test_timer
:one_minute_rate => 822,
:type => 'timer'
}
-
+
@list.add(data)
-
+
assert_match /test\..*/, @list.to_hash[:gauges][0][:name]
assert_equal 2, @list.to_hash[:gauges].length
end
-
+
def test_utilization_timer
data = {
:name => 'test',
@@ -68,9 +68,9 @@ def test_utilization_timer
:one_minute_utilization => 0.8,
:type => 'utilization_timer'
}
-
+
@list.add(data)
-
+
assert_match /test\..*/, @list.to_hash[:gauges][0][:name]
assert_equal 3, @list.to_hash[:gauges].length
end
Please sign in to comment.
Something went wrong with that request. Please try again.