Class: Scout::Metric
- Inherits:
-
Hashie::Mash
- Object
- Hashie::Mash
- Scout::Metric
- Defined in:
- lib/scout_api/metric.rb
Instance Attribute Summary collapse
-
#plugin ⇒ Object
Returns the value of attribute plugin.
-
#server ⇒ Object
Returns the value of attribute server.
Class Method Summary collapse
-
.all(options = {}) ⇒ Array
Finds all metrics that match the conditons specified via
options
. -
.average(id_or_name, options = {}) ⇒ Hash
The average value of a metric by ID or name (ex:
'disk_used'
). -
.first(id_or_options = nil) ⇒ Scout::Metric
Finds a single metric that meets the given conditions.
-
.maximum(id_or_name, options = {}) ⇒ Hash
The maximum value of a metric by ID or name (ex:
'last_minute'
). -
.minimum(id_or_name, options = {}) ⇒ Hash
The minimum value of a metric by ID or name (
ex: 'last_minute'
). -
.to_array(function, id_or_name, options = {}) ⇒ Array
Returns time series data.
-
.to_sparkline(function, id_or_name, options = {}) ⇒ String
Generates a URL to a Google chart sparkline representation of the time series data.
Instance Method Summary collapse
-
#average(opts = {}) ⇒ Hash
(also: #avg)
The average value for this metric.
-
#identifier ⇒ Object
Metrics are identified by either their given ID or their name.
-
#initialize(hash, ignore = nil) ⇒ Metric
constructor
2nd parameter is ignored/a hack because of this open Hashie issue: github.com/intridea/hashie/issues/14.
-
#maximum(opts = {}) ⇒ Hash
(also: #max)
The maximum value for this metric.
-
#minimum(opts = {}) ⇒ Hash
(also: #min)
The minimum value for this metric.
-
#options_for_relationship(opts = {}) ⇒ Object
Used to apply finder conditions.
Constructor Details
#initialize(hash, ignore = nil) ⇒ Metric
2nd parameter is ignored/a hack because of this open Hashie issue: github.com/intridea/hashie/issues/14
5 6 7 8 9 10 11 12 13 |
# File 'lib/scout_api/metric.rb', line 5 def initialize(hash, ignore=nil) #:nodoc: super(hash) @avg_calc = Scout::MetricCalculation.new(self,:AVG) @avg_calc.metric_name = identifier @min_calc = Scout::MetricCalculation.new(self,:MIN) @min_calc.metric_name = identifier @max_calc = Scout::MetricCalculation.new(self,:MAX) @max_calc.metric_name = identifier end |
Instance Attribute Details
#plugin ⇒ Object
Returns the value of attribute plugin.
2 3 4 |
# File 'lib/scout_api/metric.rb', line 2 def plugin @plugin end |
#server ⇒ Object
Returns the value of attribute server.
2 3 4 |
# File 'lib/scout_api/metric.rb', line 2 def server @server end |
Class Method Details
.all(options = {}) ⇒ Array
Finds all metrics that match the conditons specified via options
. Scout::MetricProxy uses this method to search for metrics. Refer to Scout::MetricProxy#all for examples.
48 49 50 51 52 |
# File 'lib/scout_api/metric.rb', line 48 def self.all( = {}) raise Scout::Error, "A finder condition is required" if .empty? response = Scout::Account.get("/descriptors.xml?name=#{CGI.escape([:name].to_s)}&ids=&plugin_ids=#{[:plugin_ids]}&server_ids=#{[:server_ids]}&group_ids=#{[:group_ids]}") response['ar_descriptors'] ? response['ar_descriptors'].map { |descriptor| Scout::Metric.new(descriptor) } : Array.new end |
.average(id_or_name, options = {}) ⇒ Hash
The average value of a metric by ID or name (ex: 'disk_used'
). If the metric couldn’t be found AND/OR hasn’t reported since options[:start]
, a [Scout::Error] is raised.
A 3-element Hash is returned with the following keys:
-
:value
-
:units
-
:label
Options:
-
:start
- The start time for grabbing metrics. Default is 1 hour ago. Times will be converted to UTC. -
:end
- The end time for grabbing metrics. Default isTime.now.utc
. Times will be converted to UTC. -
:aggregate
- Whether the metrics should be added together or an average across each metric should be returned. Default is false. Note that total is not necessary equal to the value on each server * num of servers.
When to use :aggregate
?
If you have a number of web servers, you may be interested in the total throughput for your application, not just the average on each server. For example:
-
Web Server No. 1 Average Throughput => 100 req/sec
-
Web Server No. 2 Average Throughput => 150 req/sec
:aggregate => true
will return ~ 250 req/sec, giving the total throughput for your entire app. The default, :aggregate => false
, will return ~ 125 req/sec, giving the average throughput across the web servers.
:aggregate => true
likely doesn’t make sense for any metric that is on a 0-100 scale (like CPU Usage, Disk Capacity, etc.).
Examples:
# What is the average request time across my servers?
Scout::Metric.average('request_time') => {:value => 0.20, :units => 'sec', :label => 'Request Time'}
# What is the average TOTAL throughput across my servers?
Scout::Metric.average('request_rate', :aggregate => true)
# How much average memory did my servers use yesterday?
# Scout::Metric.average('Memory Used', :start => Time.now-(24*60*60)*2, :end => Time.now-(24*60*60))
94 95 96 |
# File 'lib/scout_api/metric.rb', line 94 def self.average(id_or_name, = {}) calculate('AVG',id_or_name,) end |
.first(id_or_options = nil) ⇒ Scout::Metric
Finds a single metric that meets the given conditions. Possible parameter formats:
-
Scout::Metric.first
=> Finds the metric -
Scout::Metric.first(1)
=> Finds the metric with ID=1 -
Scout::Metric.first(:name => 'request_rate')
=> Finds the first metric where name=~‘request_rate’
For the :name
, a MySQL-formatted Regex can be used.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/scout_api/metric.rb', line 24 def self.first( = nil) if .nil? response = Scout::Account.get("/descriptors.xml?limit=1") Scout::Metric.new(response['ar_descriptors'].first) elsif .is_a?(Hash) if name=[:name] response = Scout::Account.get("/descriptors.xml?name=#{CGI.escape(name)}") raise Scout::Error, 'Not Found' if response['ar_descriptors'].nil? Scout::Metric.new(response['ar_descriptors'].first) else raise Scout::Error, "Invalid finder condition" end elsif .is_a?(Fixnum) response = Scout::Account.get("/descriptors/#{}.xml") Scout::Metric.new(response['ar_descriptor']) else raise Scout::Error, "Invalid finder condition" end end |
.maximum(id_or_name, options = {}) ⇒ Hash
103 104 105 |
# File 'lib/scout_api/metric.rb', line 103 def self.maximum(id_or_name, = {}) calculate('MAX',id_or_name,) end |
.minimum(id_or_name, options = {}) ⇒ Hash
112 113 114 |
# File 'lib/scout_api/metric.rb', line 112 def self.minimum(id_or_name, = {}) calculate('MIN',id_or_name,) end |
.to_array(function, id_or_name, options = {}) ⇒ Array
Returns time series data.
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/scout_api/metric.rb', line 119 def self.to_array(function,id_or_name, = {}) start_time,end_time=format_times() consolidate,name,ids=(id_or_name,) response = Scout::Account.get("/descriptors/series.xml?name=#{CGI.escape(name.to_s)}&ids=#{ids}&function=#{function}&consolidate=#{consolidate}&plugin_ids=#{[:plugin_ids]}&server_ids=#{[:server_ids]}&group_ids=#{[:group_ids]}&start=#{start_time}&end=#{end_time}") if response['records'] response['records'].values.flatten.map { |r| [Time.parse(r['time']),r['value'].to_f] } else [] end end |
.to_sparkline(function, id_or_name, options = {}) ⇒ String
Generates a URL to a Google chart sparkline representation of the time series data.
Options:
-
:size
- The size of the image in pixels. Default is'200x30'
. -
:line_color
- The color of the line. Default is'0077cc'
(blue). -
:line_width
- The width of the line in pixels. Default is <tt>2</t>.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/scout_api/metric.rb', line 140 def self.to_sparkline(function,id_or_name, = {}) start_time,end_time=format_times() consolidate,name,ids=(id_or_name,) response = Scout::Account.get("/descriptors/sparkline?name=#{CGI.escape(name.to_s)}&ids=#{ids}&function=#{function}&consolidate=#{consolidate}&plugin_ids=#{[:plugin_ids]}&server_ids=#{[:server_ids]}&group_ids=#{[:group_ids]}&start=#{start_time}&end=#{end_time}&size=#{[:size]}&line_color=#{[:line_color]}&line_width=#{[:line_width]}") if response['error'] raise Scout::Error, response['error'] else response.body end end |
Instance Method Details
#average(opts = {}) ⇒ Hash Also known as: avg
The average value for this metric. See #average for a list of options.
155 156 157 158 |
# File 'lib/scout_api/metric.rb', line 155 def average(opts = {}) @avg_calc. = opts @avg_calc end |
#identifier ⇒ Object
Metrics are identified by either their given ID or their name. Prefers ID.
180 181 182 |
# File 'lib/scout_api/metric.rb', line 180 def identifier #:nodoc: id? ? id : name end |
#maximum(opts = {}) ⇒ Hash Also known as: max
The maximum value for this metric. See #average for a list of options.
164 165 166 167 |
# File 'lib/scout_api/metric.rb', line 164 def maximum(opts = {}) @max_calc. = opts @max_calc end |
#minimum(opts = {}) ⇒ Hash Also known as: min
The minimum value for this metric. See #average for a list of options.
173 174 175 176 |
# File 'lib/scout_api/metric.rb', line 173 def minimum(opts = {}) @min_calc. = opts @min_calc end |
#options_for_relationship(opts = {}) ⇒ Object
Used to apply finder conditions
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/scout_api/metric.rb', line 185 def (opts = {}) #:nodoc: = {} if id? [:ids] = id elsif plugin [:plugin_ids] = plugin.id elsif server [:server_ids] = server.id end opts.merge() end |