Class: ScoutApm::Store

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



12
13
14
15
16
# File 'lib/scout_apm/store.rb', line 12

def initialize
  @mutex = Mutex.new
  @reporting_periods = Hash.new { |h,k| h[k] = StoreReportingPeriod.new(k) }
  @samplers = []
end

Instance Attribute Details

#reporting_periodsObject (readonly)

A hash of reporting periods. { StoreReportingPeriodTimestamp => StoreReportingPeriod }



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

def reporting_periods
  @reporting_periods
end

#samplersObject (readonly)

Used to pull metrics into each reporting period, as that reporting period is finished.



10
11
12
# File 'lib/scout_apm/store.rb', line 10

def samplers
  @samplers
end

Instance Method Details

#add_sampler(sampler) ⇒ Object

Sampler support



105
106
107
# File 'lib/scout_apm/store.rb', line 105

def add_sampler(sampler)
  @samplers << sampler
end

#collect_samplers(rp) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/scout_apm/store.rb', line 109

def collect_samplers(rp)
  @samplers.each do |sampler|
    begin
      sampler.metrics(rp.timestamp, self)
    rescue => e
      ScoutApm::Agent.instance.logger.info "Error reading #{sampler.human_name} for period: #{rp}"
      ScoutApm::Agent.instance.logger.debug "#{e.message}\n\t#{e.backtrace.join("\n\t")}"
    end
  end
end

#current_periodObject



22
23
24
# File 'lib/scout_apm/store.rb', line 22

def current_period
  reporting_periods[current_timestamp]
end

#current_timestampObject



18
19
20
# File 'lib/scout_apm/store.rb', line 18

def current_timestamp
  StoreReportingPeriodTimestamp.new
end

#track!(metrics, options = {}) ⇒ Object

Save newly collected metrics



27
28
29
30
31
32
33
34
35
36
# File 'lib/scout_apm/store.rb', line 27

def track!(metrics, options={})
  @mutex.synchronize {
    period = if options[:timestamp]
               @reporting_periods[options[:timestamp]]
             else
               current_period
             end
    period.absorb_metrics!(metrics)
  }
end

#track_histograms!(histograms, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/scout_apm/store.rb', line 38

def track_histograms!(histograms, options={})
  @mutex.synchronize {
    period = if options[:timestamp]
               @reporting_periods[options[:timestamp]]
             else
               current_period
             end
    period.merge_histograms!(histograms)
  }
end

#track_job!(job) ⇒ Object



64
65
66
67
68
69
# File 'lib/scout_apm/store.rb', line 64

def track_job!(job)
  return if job.nil?
  @mutex.synchronize {
    current_period.merge_jobs!(Array(job))
  }
end

#track_one!(type, name, value, options = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/scout_apm/store.rb', line 49

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) ⇒ Object



71
72
73
74
75
76
# File 'lib/scout_apm/store.rb', line 71

def track_slow_job!(job)
  return if job.nil?
  @mutex.synchronize {
    current_period.merge_slow_jobs!(Array(job))
  }
end

#track_slow_transaction!(slow_transaction) ⇒ Object

Save a new slow transaction



57
58
59
60
61
62
# File 'lib/scout_apm/store.rb', line 57

def track_slow_transaction!(slow_transaction)
  return unless slow_transaction
  @mutex.synchronize {
    current_period.merge_slow_transactions!(slow_transaction)
  }
end

#write_reporting_period(layaway, time, rp) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/scout_apm/store.rb', line 91

def write_reporting_period(layaway, time, rp)
  @mutex.synchronize {
    layaway.write_reporting_period(rp)
  }
rescue => e
  ScoutApm::Agent.instance.logger.warn("Failed writing data to layaway file: #{e.message} / #{e.backtrace}")
ensure
  ScoutApm::Agent.instance.logger.debug("Before delete, reporting periods length: #{reporting_periods.size}")
  deleted_items = reporting_periods.delete(time)
  ScoutApm::Agent.instance.logger.debug("After delete, reporting periods length: #{reporting_periods.size}. Did delete #{deleted_items}")
end

#write_to_layaway(layaway, force = false) ⇒ Object

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.



83
84
85
86
87
88
89
# File 'lib/scout_apm/store.rb', line 83

def write_to_layaway(layaway, force=false)
  ScoutApm::Agent.instance.logger.debug("Writing to layaway#{" (Forced)" if force}")

    reporting_periods.select { |time, rp| force || (time.timestamp < current_timestamp.timestamp) }.
                      each   { |time, rp| collect_samplers(rp) }.
                      each   { |time, rp| write_reporting_period(layaway, time, rp) }
end