class Observed::Plugins::Gauge
Public Instance Methods
prepare_rrd(args)
click to toggle source
# File lib/observed/gauge.rb, line 35 def prepare_rrd(args) start = args[:start] log_debug "Creating a rrd file named '#{args[:rrd]}' with options {:start => #{start}}" result = RRD::Builder.new(args[:rrd], start: start, step: step.seconds).tap do |builder| builder.datasource data_source, :type => :gauge, :heartbeat => period.seconds, :min => 0, :max => :unlimited builder.archive :average, :every => period.seconds, :during => period.seconds builder.save end log_debug "Builder#save returned: #{result.inspect}" end
translate(data, options)
click to toggle source
# File lib/observed/gauge.rb, line 23 def translate(data, options) time = options[:time] || Time.now rewrote = update_value_for_key_path(data, key_path) do |v| sample = coerce.call(v) average = get_cdp_updated_with(time, sample) average end unless fetch_value_for_key_path(rewrote, key_path).nan? rewrote end end
Private Instance Methods
data_source()
click to toggle source
# File lib/observed/gauge.rb, line 91 def data_source self.key_path.gsub('.', '_') end
fetch_value_for_key_path(data, key_path)
click to toggle source
# File lib/observed/gauge.rb, line 70 def fetch_value_for_key_path(data, key_path) first, *rest = split_key_path(key_path) dug_data = data[first] || data[first.intern] if rest.empty? dug_data else fetch_value_for_key_path(dug_data, rest) end end
get_cdp_updated_with(time, value)
click to toggle source
@param [Time] time
# File lib/observed/gauge.rb, line 96 def get_cdp_updated_with(time, value) rrd_path = self.rrd t = time.to_i rrd = RRD::Base.new(rrd_path) unless File.exist? rrd_path prepare_rrd(rrd: rrd_path, start: t) end log_debug "Updating the data source '#{data_source}' with the value #{value} with timestamp #{t}" rrd.update t, value log_debug rrd.fetch!(:average)[-2..-1] rrd.fetch(:average)[-2..-1].first.last end
hash_update(hash, key, value)
click to toggle source
# File lib/observed/gauge.rb, line 62 def hash_update(hash, key, value) if hash[key] hash[key] = value else hash[key.intern] = value end end
split_key_path(key_path)
click to toggle source
# File lib/observed/gauge.rb, line 80 def split_key_path(key_path) case key_path when Array key_path when String key_path.split('.') else fail "Unexpected type of key_path met. Expected an Array or a String, but it was a(n) #{key_path.class}" end end
update_value_for_key_path(data, key_path, &block)
click to toggle source
# File lib/observed/gauge.rb, line 48 def update_value_for_key_path(data, key_path, &block) first, *rest = split_key_path(key_path) data = data.dup dug_data = data[first] || data[first.intern] if rest.empty? hash_update(data, first, block.call(dug_data)) else hash_update(data, first, update_value_for_key_path(dug_data, rest, &block)) end data end