Class: ScoutApm::Capacity

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCapacity

Returns a new instance of Capacity.



6
7
8
9
10
# File 'lib/scout_apm/capacity.rb', line 6

def initialize
  @processing_start_time = Time.now
  @lock ||= Mutex.new # the transaction_entry_time could be modified while processing a request or when #process is called.
  @accumulated_time = 0.0
end

Instance Attribute Details

#accumulated_timeObject (readonly)

Returns the value of attribute accumulated_time.



4
5
6
# File 'lib/scout_apm/capacity.rb', line 4

def accumulated_time
  @accumulated_time
end

#processing_start_timeObject (readonly)

Returns the value of attribute processing_start_time.



4
5
6
# File 'lib/scout_apm/capacity.rb', line 4

def processing_start_time
  @processing_start_time
end

#transaction_entry_timeObject (readonly)

Returns the value of attribute transaction_entry_time.



4
5
6
# File 'lib/scout_apm/capacity.rb', line 4

def transaction_entry_time
  @transaction_entry_time
end

Instance Method Details

#finish_transaction!Object

Called when a transaction completes to record its time used.



20
21
22
23
24
25
26
27
28
29
# File 'lib/scout_apm/capacity.rb', line 20

def finish_transaction!
  @lock.synchronize do
    if transaction_entry_time
      @accumulated_time += (Time.now - transaction_entry_time).to_f
    else
      ScoutApm::Agent.instance.logger.warn "No transaction entry time. Not recording capacity metrics for transaction."
    end
    @transaction_entry_time = nil
  end
end

#processObject

Ran when sending metrics to server. Reports capacity usage metrics.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/scout_apm/capacity.rb', line 32

def process
  process_time = Time.now
  ScoutApm::Agent.instance.logger.debug "Processing capacity usage for [#{@processing_start_time}] to [#{process_time}]. Time Spent: #{@accumulated_time}."
  @lock.synchronize do
    time_spent = @accumulated_time
    @accumulated_time = 0.0
    # If a transaction is still running, capture its running time up to now and
    # reset the +transaction_entry_time+ to now.
    if @transaction_entry_time
      time_spent += (process_time - @transaction_entry_time).to_f
      ScoutApm::Agent.instance.logger.debug "A transaction is running while calculating capacity. Start time: [#{transaction_entry_time}]. Will update the entry time to [#{process_time}]."
      @transaction_entry_time = process_time # prevent from over-counting capacity usage. update the transaction start time to now.
    end
    time_spent = 0.0 if time_spent < 0.0

    window = (process_time - processing_start_time).to_f # time period we are evaulating capacity usage.
    window = 1.0 if window <= 0.0 # prevent divide-by-zero if clock adjusted.
    capacity = time_spent / window
    ScoutApm::Agent.instance.logger.debug "Instance/Capacity: #{capacity}"
    ScoutApm::Agent.instance.store.track_one!("Instance", "Capacity", capacity)

    @processing_start_time = process_time
  end
end

#start_transaction!Object

Called when a transaction is traced.



13
14
15
16
17
# File 'lib/scout_apm/capacity.rb', line 13

def start_transaction!
  @lock.synchronize do
    @transaction_entry_time = Time.now
  end
end