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.



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

def initialize
  @mutex = Mutex.new
  @reporting_periods = Hash.new { |h,k| h[k] = StoreReportingPeriod.new(k) }
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

Instance Method Details

#current_periodObject



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

def current_period
  reporting_periods[current_timestamp]
end

#current_timestampObject



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

def current_timestamp
  StoreReportingPeriodTimestamp.new
end

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

Save newly collected metrics



23
24
25
26
27
# File 'lib/scout_apm/store.rb', line 23

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

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



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

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_transaction!(slow_transaction) ⇒ Object

Save a new slow transaction



37
38
39
40
41
42
# File 'lib/scout_apm/store.rb', line 37

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.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/scout_apm/store.rb', line 49

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|
                               layaway.add_reporting_period(time, rp)
                               reporting_periods.delete(time)
                             }
  }
  ScoutApm::Agent.instance.logger.debug("Finished writing to layaway")
end