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



83
84
85
# File 'lib/scout_apm/store.rb', line 83

def add_sampler(sampler)
  @samplers << sampler
end

#collect_samplers(rp) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/scout_apm/store.rb', line 87

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_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
75
76
77
78
79
# 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|
                               collect_samplers(rp)
                               layaway.add_reporting_period(time, rp)
                               reporting_periods.delete(time)
                             }
  }
  ScoutApm::Agent.instance.logger.debug("Finished writing to layaway")
end