Class: Neetodeploy::Rails::MetricsStore

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/neetodeploy/autoscale/rails/metrics_store.rb

Constant Summary collapse

WINDOW_SIZE_SECONDS =

Use sliding window: keep last 2 minutes of data (120 seconds) At 2s reporting interval, this gives us ~60 samples per window This ensures percentiles work correctly even with low traffic

120
MAX_SAMPLES =

Safety limit to prevent memory bloat

1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetricsStore

Returns a new instance of MetricsStore.



19
20
21
22
# File 'lib/neetodeploy/autoscale/rails/metrics_store.rb', line 19

def initialize
  @metrics = []  # Array of {time: timestamp, value: queue_time}
  @mutex = Mutex.new
end

Instance Attribute Details

#metricsObject (readonly)

Returns the value of attribute metrics.



17
18
19
# File 'lib/neetodeploy/autoscale/rails/metrics_store.rb', line 17

def metrics
  @metrics
end

Instance Method Details

#flushObject

Flush: return all values in window, but DON’T clear (sliding window) This allows high values to persist across multiple reporting cycles



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/neetodeploy/autoscale/rails/metrics_store.rb', line 55

def flush
  @mutex.synchronize do
    # Clean up old data first
    cutoff = Time.now.to_f - WINDOW_SIZE_SECONDS
    @metrics.reject! { |m| m[:time] < cutoff }
    
    result = @metrics.map { |m| m[:value] }
    size = result.size
    max_val = result.max if result.any?
    NeetoDeploy::Logger.logger.debug("MetricsStore: Reporting from sliding window: #{size} values, max=#{max_val}ms") if result.any?
    
    # Return values but keep them in the window for next cycle
    result
  end
end

#get_recent(seconds = WINDOW_SIZE_SECONDS) ⇒ Object

Get recent samples from the sliding window (last N seconds)



46
47
48
49
50
51
# File 'lib/neetodeploy/autoscale/rails/metrics_store.rb', line 46

def get_recent(seconds = WINDOW_SIZE_SECONDS)
  @mutex.synchronize do
    cutoff = Time.now.to_f - seconds
    @metrics.select { |m| m[:time] >= cutoff }.map { |m| m[:value] }
  end
end

#push(queue_time) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/neetodeploy/autoscale/rails/metrics_store.rb', line 24

def push(queue_time)
  @mutex.synchronize do
    now = Time.now.to_f
    @metrics << { time: now, value: queue_time }
    
    # Remove old data outside window
    cutoff = now - WINDOW_SIZE_SECONDS
    @metrics.reject! { |m| m[:time] < cutoff }
    
    # Safety limit: keep only most recent samples
    if @metrics.size > MAX_SAMPLES
      @metrics = @metrics.last(MAX_SAMPLES)
    end
    
    # Log high queue times to help debug
    if queue_time > 500
      NeetoDeploy::Logger.logger.debug("MetricsStore: Pushed high queue_time=#{queue_time}ms (total in window: #{@metrics.size})")
    end
  end
end