Class: ServerMetrics::Collector
- Inherits:
-
Object
- Object
- ServerMetrics::Collector
- Defined in:
- lib/server_metrics/collector.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#collector_id ⇒ Object
readonly
Returns the value of attribute collector_id.
-
#data ⇒ Object
Returns the value of attribute data.
-
#error ⇒ Object
Returns the value of attribute error.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Object
see to_hash.
Instance Method Summary collapse
-
#convert_to_mb(value) ⇒ Object
Convert strings containing ‘T,G,M,or K’ to MB.
- #counter(name, value, options = {}, &block) ⇒ Object
-
#initialize(options = {}) ⇒ Collector
constructor
A new instance of Collector.
- #linux? ⇒ Boolean
-
#memory(name = nil) ⇒ Object
memory(:no_track) memory.delete(:no_track) memory.clear.
- #normalize_key(key) ⇒ Object
- #option(name) ⇒ Object
- #osx? ⇒ Boolean
-
#remember(hash) ⇒ Object
remember(name1: value1, name2: value2).
- #report(hash) ⇒ Object
- #run ⇒ Object
-
#to_hash ⇒ Object
returns a hash you can serialize and store on disk, or just hold onto and re-instantiate the collector later.
Constructor Details
#initialize(options = {}) ⇒ Collector
Returns a new instance of Collector.
19 20 21 22 23 24 25 |
# File 'lib/server_metrics/collector.rb', line 19 def initialize(={}) @options = @data={} @memory={} @collector_id = self.class.name+'-'+@options.to_a.sort_by { |a| a.first }.flatten.join('-') @error=nil end |
Instance Attribute Details
#collector_id ⇒ Object (readonly)
Returns the value of attribute collector_id.
16 17 18 |
# File 'lib/server_metrics/collector.rb', line 16 def collector_id @collector_id end |
#data ⇒ Object
Returns the value of attribute data.
17 18 19 |
# File 'lib/server_metrics/collector.rb', line 17 def data @data end |
#error ⇒ Object
Returns the value of attribute error.
17 18 19 |
# File 'lib/server_metrics/collector.rb', line 17 def error @error end |
Class Method Details
.from_hash(hash) ⇒ Object
see to_hash. The hash should contain :options and :memory keys
131 132 133 134 135 136 |
# File 'lib/server_metrics/collector.rb', line 131 def self.from_hash(hash) c=new(hash[:options]) c.instance_variable_set('@memory', hash[:memory]) c.instance_variable_set('@data', hash[:data]) c end |
Instance Method Details
#convert_to_mb(value) ⇒ Object
Convert strings containing ‘T,G,M,or K’ to MB. The result is a float only – units are NOT returned
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/server_metrics/collector.rb', line 101 def convert_to_mb(value) value = if value =~ /G/i value.to_f*1024.0 elsif value =~ /M/i value.to_f elsif value =~ /K/i (value.to_f/1024.0) elsif value =~ /T/i (value.to_f*1024.0*1024.0) else value.to_f end ("%.1f" % [value]).to_f end |
#counter(name, value, options = {}, &block) ⇒ Object
counter(:rkbps, stats / 2, :per => :second)
counter(:rpm, request_counter, :per => :minute)
counter(:swap_ins, vmstat['pswpin'], :per => :second, :round => true)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/server_metrics/collector.rb', line 64 def counter(name, value, = {}, &block) current_time = Time.now if data = memory("_counter_#{name}") last_time, last_value = data[:time], data[:value] elapsed_seconds = current_time - last_time # We won't log it if the value has wrapped or enough time hasn't # elapsed if value >= last_value && elapsed_seconds >= 1 if block result = block.call(last_value, value) else result = value - last_value end case [:per] when :second, 'second' result = result / elapsed_seconds.to_f when :minute, 'minute' result = result / elapsed_seconds.to_f * 60.0 else raise "Unknown option for ':per': #{[:per].inspect}" end if [:round] result = (result * (10 ** [:round])).round / (10 ** [:round]).to_f end report(name => result) end end remember("_counter_#{name}" => {:time => current_time, :value => value}) end |
#linux? ⇒ Boolean
138 139 140 |
# File 'lib/server_metrics/collector.rb', line 138 def linux? RbConfig::CONFIG['target_os'] == 'linux' end |
#memory(name = nil) ⇒ Object
memory(:no_track)
memory.delete(:no_track)
memory.clear
45 46 47 48 49 50 51 |
# File 'lib/server_metrics/collector.rb', line 45 def memory(name = nil) if name.nil? @memory else @memory[name] || @memory[name.is_a?(String) ? name.to_sym : String(name)] end end |
#normalize_key(key) ⇒ Object
117 118 119 |
# File 'lib/server_metrics/collector.rb', line 117 def normalize_key(key) (key.is_a?(String) ? key : key.to_s).downcase.gsub(" ", "_").gsub("%", "percent").to_sym end |
#option(name) ⇒ Object
27 28 29 |
# File 'lib/server_metrics/collector.rb', line 27 def option(name) @options[name] || @options[name.is_a?(String) ? name.to_sym : String(name)] end |
#osx? ⇒ Boolean
142 143 144 |
# File 'lib/server_metrics/collector.rb', line 142 def osx? RbConfig::CONFIG['target_os'] == 'darwin' end |
#remember(hash) ⇒ Object
remember(name1: value1, name2: value2)
56 57 58 |
# File 'lib/server_metrics/collector.rb', line 56 def remember(hash) @memory.merge!(hash) end |
#report(hash) ⇒ Object
37 38 39 |
# File 'lib/server_metrics/collector.rb', line 37 def report(hash) @data.merge!(hash) end |
#run ⇒ Object
31 32 33 34 35 |
# File 'lib/server_metrics/collector.rb', line 31 def run @data={} build_report @data end |
#to_hash ⇒ Object
returns a hash you can serialize and store on disk, or just hold onto and re-instantiate the collector later. Why you’d need to do this: to persist the memory (including counters) of a plugin instance.
Collector.from_hash(h) is the flipside of this: Collector.from_hash(plugin.to_hash) gets you essentially the same instance
126 127 128 |
# File 'lib/server_metrics/collector.rb', line 126 def to_hash {:options => @options, :memory => @memory, :data => @data, :plugin_id => @plugin_id} end |