Class: ScoutApm::Store

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

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Store

Returns a new instance of Store.



6
7
8
9
10
11
12
13
# 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

Instance Method Details

#add_sampler(sampler_klass) ⇒ Object

Sampler support



120
121
122
# File 'lib/scout_apm/store.rb', line 120

def add_sampler(sampler_klass)
  @samplers << sampler_klass.new(@context)
end

#current_timestampObject



15
16
17
# File 'lib/scout_apm/store.rb', line 15

def current_timestamp
  StoreReportingPeriodTimestamp.new(Time.now)
end

#tick!Object

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



102
103
104
105
# File 'lib/scout_apm/store.rb', line 102

def tick!
  rp = current_period
  collect_samplers(rp)
end

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

Save newly collected metrics



34
35
36
37
38
39
# 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 = {}) ⇒ Object



48
49
50
51
52
53
# 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_histograms!(histograms, options = {}) ⇒ Object



41
42
43
44
45
46
# 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) ⇒ Object



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

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

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



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

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



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

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



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

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.



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

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