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



87
88
89
# File 'lib/scout_apm/store.rb', line 87

def add_sampler(sampler)
  @samplers << sampler
end

#collect_samplers(rp) ⇒ Object



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

def collect_samplers(rp)
  @samplers.each do |sampler|
    begin
      metrics = sampler.metrics(rp.timestamp)
      rp.absorb_metrics!(metrics)
    rescue => e
      ScoutApm::Agent.instance.logger.info "Error reading #{sampler.human_name} for period: #{rp}"
      ScoutApm::Agent.instance.logger.debug e.message
      ScoutApm::Agent.instance.logger.debug e.backtrace.join("\n")
    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
# File 'lib/scout_apm/store.rb', line 27

def track!(metrics, options={})
  @mutex.synchronize {
    current_period.absorb_metrics!(metrics)
  }
end

#track_job!(job) ⇒ Object



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

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

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



33
34
35
36
37
38
# File 'lib/scout_apm/store.rb', line 33

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



55
56
57
58
59
60
# File 'lib/scout_apm/store.rb', line 55

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



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

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



76
77
78
79
80
81
82
83
# File 'lib/scout_apm/store.rb', line 76

def write_reporting_period(layaway, time, rp)
  collect_samplers(rp)
  layaway.write_reporting_period(rp)
rescue => e
  ScoutApm::Agent.instance.logger.warn("Failed writing data to layaway file: #{e.message} / #{e.backtrace}")
ensure
  reporting_periods.delete(time)
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.



67
68
69
70
71
72
73
74
# File 'lib/scout_apm/store.rb', line 67

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

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