Class: Judoscale::MetricsStore

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/judoscale/metrics_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetricsStore

Returns a new instance of MetricsStore.



13
14
15
16
# File 'lib/judoscale/metrics_store.rb', line 13

def initialize
  @metrics = []
  @flushed_at = Time.now
end

Instance Attribute Details

#flushed_atObject (readonly)

Returns the value of attribute flushed_at.



11
12
13
# File 'lib/judoscale/metrics_store.rb', line 11

def flushed_at
  @flushed_at
end

#metricsObject (readonly)

Returns the value of attribute metrics.



11
12
13
# File 'lib/judoscale/metrics_store.rb', line 11

def metrics
  @metrics
end

Instance Method Details

#clearObject



37
38
39
# File 'lib/judoscale/metrics_store.rb', line 37

def clear
  @metrics.clear
end

#flushObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/judoscale/metrics_store.rb', line 26

def flush
  @flushed_at = Time.now
  flushed_metrics = []

  while (metric = @metrics.shift)
    flushed_metrics << metric
  end

  flushed_metrics
end

#push(identifier, value, time = Time.now, queue_name = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/judoscale/metrics_store.rb', line 18

def push(identifier, value, time = Time.now, queue_name = nil)
  # If it's been two minutes since clearing out the store, stop collecting metrics.
  # There could be an issue with the reporter, and continuing to collect will consume linear memory.
  return if @flushed_at && @flushed_at < Time.now - 120

  @metrics << Metric.new(identifier, value, time, queue_name)
end