class ScoutApm::Store

Public Class Methods

new(context) click to toggle source
# File lib/scout_apm/store.rb, line 6
def initialize(context)
  @context = context
  @mutex = Monitor.new
  @reporting_periods = Hash.new { |h,k|
    @mutex.synchronize { h[k] = StoreReportingPeriod.new(k, @context) }
  }
  @samplers = []
end

Public Instance Methods

add_sampler(sampler_klass) click to toggle source

Sampler support

# File lib/scout_apm/store.rb, line 127
def add_sampler(sampler_klass)
  @samplers << sampler_klass.new(@context)
end
current_timestamp() click to toggle source
# File lib/scout_apm/store.rb, line 15
def current_timestamp
  StoreReportingPeriodTimestamp.new(Time.now)
end
tick!() click to toggle source

For each tick (minute), be sure we have a reporting period, and that samplers are run for it.

# File lib/scout_apm/store.rb, line 109
def tick!
  rp = current_period
  collect_samplers(rp)
end
track!(metrics, options={}) click to toggle source

Save newly collected metrics

# File lib/scout_apm/store.rb, line 34
def track!(metrics, options={})
  @mutex.synchronize {
    period = find_period(options[:timestamp])
    period.absorb_metrics!(metrics)
  }
end
track_db_query_metrics!(db_query_metric_set, options={}) click to toggle source
# File lib/scout_apm/store.rb, line 48
def track_db_query_metrics!(db_query_metric_set, options={})
  @mutex.synchronize {
    period = find_period(options[:timestamp])
    period.merge_db_query_metrics!(db_query_metric_set)
  }
end
track_external_service_metrics!(external_service_metric_set, options={}) click to toggle source
# File lib/scout_apm/store.rb, line 55
def track_external_service_metrics!(external_service_metric_set, options={})
  @mutex.synchronize {
    period = find_period(options[:timestamp])
    period.merge_external_service_metrics!(external_service_metric_set)
  }
end
track_histograms!(histograms, options={}) click to toggle source
# File lib/scout_apm/store.rb, line 41
def track_histograms!(histograms, options={})
  @mutex.synchronize {
    period = find_period(options[:timestamp])
    period.merge_histograms!(histograms)
  }
end
track_job!(job) click to toggle source
# File lib/scout_apm/store.rb, line 77
def track_job!(job)
  return if job.nil?
  @mutex.synchronize {
    current_period.merge_jobs!(Array(job))
  }
end
track_one!(type, name, value, options={}) click to toggle source
# File lib/scout_apm/store.rb, line 62
def track_one!(type, name, value, options={})
  meta = MetricMeta.new("#{type}/#{name}")
  stat = MetricStats.new(false)
  stat.update!(value)
  track!({meta => stat}, options)
end
track_slow_job!(job) click to toggle source
# File lib/scout_apm/store.rb, line 84
def track_slow_job!(job)
  return if job.nil?
  @mutex.synchronize {
    current_period.merge_slow_jobs!(Array(job))
  }
end
track_slow_transaction!(slow_transaction) click to toggle source

Save a new slow transaction

# File lib/scout_apm/store.rb, line 70
def track_slow_transaction!(slow_transaction)
  return unless slow_transaction
  @mutex.synchronize {
    current_period.merge_slow_transactions!(slow_transaction)
  }
end
write_to_layaway(layaway, force=false) click to toggle source

Take each completed reporting_period, and write it to the layaway passed

force - a boolean argument that forces this function to write current-minute metrics. Useful when we are shutting down the agent during a restart.

# File lib/scout_apm/store.rb, line 96
def write_to_layaway(layaway, force=false)
  logger.debug("Writing to layaway#{" (Forced)" if force}")

  to_report = @mutex.synchronize {
    @reporting_periods.select { |time, rp|
      force || (time.timestamp < current_timestamp.timestamp)
    }
  }

  to_report.each { |time, rp| write_reporting_period(layaway, time, rp) }
end

Private Instance Methods

collect_samplers(rp) click to toggle source
# File lib/scout_apm/store.rb, line 131
def collect_samplers(rp)
  @samplers.each do |sampler|
    begin
      sampler.metrics(rp.timestamp, self)
    rescue => e
      logger.info "Error reading #{sampler.human_name} for period: #{rp}"
      logger.debug "#{e.message}\n\t#{e.backtrace.join("\n\t")}"
    end
  end
end
current_period() click to toggle source
# File lib/scout_apm/store.rb, line 19
def current_period
  @reporting_periods[current_timestamp]
end
find_period(timestamp = nil) click to toggle source
# File lib/scout_apm/store.rb, line 24
def find_period(timestamp = nil)
  if timestamp
    @reporting_periods[timestamp]
  else
    current_period
  end
end
logger() click to toggle source
# File lib/scout_apm/store.rb, line 143
def logger
  @context.logger
end
write_reporting_period(layaway, time, rp) click to toggle source
# File lib/scout_apm/store.rb, line 114
def write_reporting_period(layaway, time, rp)
    layaway.write_reporting_period(rp)
rescue => e
  logger.warn("Failed writing data to layaway file: #{e.message} / #{e.backtrace}")
ensure
  logger.debug("Before delete, reporting periods length: #{@reporting_periods.size}")
  deleted_items = @mutex.synchronize { @reporting_periods.delete(time) }
  logger.debug("After delete, reporting periods length: #{@reporting_periods.size}. Did delete #{deleted_items}")
end