Sha256: b152eed77d17fb198001d27acdb0e5556e3c84b37fc7a4fa39cef29f9917648b

Contents?: true

Size: 1.96 KB

Versions: 9

Compression:

Stored size: 1.96 KB

Contents

# Stores StoreReportingPeriod objects in a file before sending them to the server.
# 1. A centralized store for multiple Agent processes. This way, only 1 checkin is sent to Scout rather than 1 per-process.
module ScoutApm
  class Layaway
    attr_accessor :file

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

    # We're changing the format, so detect if we're loading an old formatted
    # file, and just drop it if so. There's no important data there, since it's
    # used mostly for just syncronizing between processes
    def verify_layaway_file_contents
      file.read_and_write do |existing_data|
        existing_data ||= {}
        if existing_data.keys.all?{|k| k.is_a? StoreReportingPeriodTimestamp } &&
            existing_data.values.all? {|v| v.is_a? StoreReportingPeriod }
          existing_data
        else
          {}
        end
      end
    end

    def add_reporting_period(time, reporting_period)
      file.read_and_write do |existing_data|
        existing_data ||= Hash.new
        existing_data.merge(time => reporting_period) {|key, old_val, new_val|
          old_val.merge_metrics!(new_val.metrics_payload).merge_slow_transactions!(new_val.slow_transactions)
        }
      end
    end

    REPORTING_INTERVAL = 60 # seconds

    # Returns an array of ReportingPeriod objects that are ready to be pushed to the server
    def periods_ready_for_delivery
      ready_for_delivery = []

      file.read_and_write do |existing_data|
        existing_data ||= {}
        ready_for_delivery = existing_data.select {|time, rp| should_send?(rp) } # Select off the values we want

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

      return ready_for_delivery.values
    end

    # We just want to send anything older than X
    def should_send?(reporting_period)
      reporting_period.timestamp.age_in_seconds > (REPORTING_INTERVAL * 2)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
scout_apm-1.2.3 lib/scout_apm/layaway.rb
scout_apm-1.2.3.pre lib/scout_apm/layaway.rb
scout_apm-1.2.2 lib/scout_apm/layaway.rb
scout_apm-1.2.1 lib/scout_apm/layaway.rb
scout_apm-1.2.0 lib/scout_apm/layaway.rb
scout_apm-1.2.0.pre13 lib/scout_apm/layaway.rb
scout_apm-1.2.0.pre12 lib/scout_apm/layaway.rb
scout_apm-1.2.0.pre11 lib/scout_apm/layaway.rb
scout_apm-1.2.0.pre10 lib/scout_apm/layaway.rb