Class: ScoutApm::Layaway

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/layaway.rb

Constant Summary collapse

REPORTING_INTERVAL =

seconds

60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLayaway

Returns a new instance of Layaway.



7
8
9
# File 'lib/scout_apm/layaway.rb', line 7

def initialize
  @file = ScoutApm::LayawayFile.new
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/scout_apm/layaway.rb', line 5

def file
  @file
end

Instance Method Details

#add_reporting_period(time, reporting_period) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/scout_apm/layaway.rb', line 11

def add_reporting_period(time, reporting_period)
  file.read_and_write do |existing_data|
    existing_data ||= Hash.new
    ScoutApm::Agent.instance.logger.debug("AddReportingPeriod: Adding a reporting_period with timestamp: #{reporting_period.timestamp.to_s}, and #{reporting_period.request_count} requests")

    existing_data = existing_data.merge(time => reporting_period) {|key, old_val, new_val|
      old_req = old_val.request_count
      new_req = new_val.request_count
      ScoutApm::Agent.instance.logger.debug("Merging Two reporting periods (#{old_val.timestamp.to_s}, #{new_val.timestamp.to_s}): old req #{old_req}, new req #{new_req}")

      old_val.merge_metrics!(new_val.metrics_payload).merge_slow_transactions!(new_val.slow_transactions)
    }

    ScoutApm::Agent.instance.logger.debug("AddReportingPeriod: AfterMerge Timestamps: #{existing_data.keys.map(&:to_s).inspect}")
    existing_data
  end
end

#periods_ready_for_deliveryObject

Returns an array of ReportingPeriod objects that are ready to be pushed to the server



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/scout_apm/layaway.rb', line 32

def periods_ready_for_delivery
  ready_for_delivery = []
  file.read_and_write do |existing_data|
    existing_data ||= {}

    ScoutApm::Agent.instance.logger.debug("PeriodsReadyForDeliver: All Timestamps: #{existing_data.keys.map(&:to_s).inspect}")

    ready_for_delivery = existing_data.to_a.select {|time, rp| should_send?(rp) } # Select off the values we want. to_a is needed for compatibility with Ruby 1.8.7.

    # Rewrite anything not plucked out back to the file
    existing_data.reject {|k, v| ready_for_delivery.map(&:first).include?(k) }
  end

  return ready_for_delivery.map(&:last)
end

#should_send?(reporting_period) ⇒ Boolean

We just want to send anything older than X

Returns:

  • (Boolean)


49
50
51
# File 'lib/scout_apm/layaway.rb', line 49

def should_send?(reporting_period)
  reporting_period.timestamp.age_in_seconds > (REPORTING_INTERVAL * 2)
end