Class: ScoutRailsProxy::Layaway
- Inherits:
-
Object
- Object
- ScoutRailsProxy::Layaway
- Defined in:
- lib/scout_rails_proxy/layaway.rb
Overview
Stores metrics in a file before sending them to the server. Two uses:
-
A centralized store for multiple Agent processes. This way, only 1 checkin is sent to Scout rather than 1 per-process.
-
Bundling up reports from multiple timeslices to make updates more efficent server-side.
Metrics are stored in a Hash, where the keys are Time.to_i on the minute. When depositing data, metrics are either merged with an existing time or placed in a new key.
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
Instance Method Summary collapse
- #deposit_and_deliver ⇒ Object
-
#initialize ⇒ Layaway
constructor
A new instance of Layaway.
- #slot ⇒ Object
-
#validate_data(data) ⇒ Object
Ensures the data we’re sending to the server isn’t stale.
Constructor Details
#initialize ⇒ Layaway
Returns a new instance of Layaway.
9 10 11 |
# File 'lib/scout_rails_proxy/layaway.rb', line 9 def initialize @file = ScoutRailsProxy::LayawayFile.new end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
8 9 10 |
# File 'lib/scout_rails_proxy/layaway.rb', line 8 def file @file end |
Instance Method Details
#deposit_and_deliver ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/scout_rails_proxy/layaway.rb', line 13 def deposit_and_deliver new_data = ScoutRailsProxy::Agent.instance.store.metric_hash controller_count = 0 new_data.each do |,stats| if .metric_name =~ /\AController/ controller_count += stats.call_count end end ScoutRailsProxy::Agent.instance.logger.debug "Depositing #{controller_count} requests into #{Time.at(slot).strftime("%m/%d/%y %H:%M:%S %z")} slot." to_deliver = {} file.read_and_write do |old_data| old_data ||= Hash.new # merge data # if the previous minute has ended, its time to send those metrics if old_data.any? and old_data[slot].nil? to_deliver = old_data old_data = Hash.new elsif old_data.any? ScoutRailsProxy::Agent.instance.logger.debug "Not yet time to deliver metrics for slot [#{Time.at(old_data.keys.sort.last).strftime("%m/%d/%y %H:%M:%S %z")}]" else ScoutRailsProxy::Agent.instance.logger.debug "There is no data in the layaway file to deliver." end old_data[slot]=ScoutRailsProxy::Agent.instance.store.merge_data_and_clear(old_data[slot] || Hash.new) ScoutRailsProxy::Agent.instance.logger.debug "Saving the following #{old_data.size} time slots locally:" old_data.each do |k,v| controller_count = 0 new_data.each do |,stats| if .metric_name =~ /\AController/ controller_count += stats.call_count end end ScoutRailsProxy::Agent.instance.logger.debug "#{Time.at(k).strftime("%m/%d/%y %H:%M:%S %z")} => #{controller_count} requests" end old_data end to_deliver.any? ? validate_data(to_deliver) : {} end |
#slot ⇒ Object
71 72 73 74 75 |
# File 'lib/scout_rails_proxy/layaway.rb', line 71 def slot t = Time.now t -= t.sec t.to_i end |
#validate_data(data) ⇒ Object
Ensures the data we’re sending to the server isn’t stale. This can occur if the agent is collecting data, and app server goes down w/data in the local storage. When it is restarted later data will remain in local storage but it won’t be for the current reporting interval.
If the data is stale, an empty Hash is returned. Otherwise, the data from the most recent slot is returned.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/scout_rails_proxy/layaway.rb', line 57 def validate_data(data) data = data.to_a.sort now = Time.now if (most_recent = data.first.first) < now.to_i - 2*60 ScoutRailsProxy::Agent.instance.logger.debug "Local Storage is stale (#{Time.at(most_recent).strftime("%m/%d/%y %H:%M:%S %z")}). Not sending data." {} else data.first.last end rescue ScoutRailsProxy::Agent.instance.logger.debug $!. ScoutRailsProxy::Agent.instance.logger.debug $!.backtrace end |