Class: PrometheusExporter::Server::MetricsContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus_exporter/server/metrics_container.rb

Constant Summary collapse

METRIC_MAX_AGE =
60
METRIC_EXPIRE_ATTR =
"_expire_at"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl: METRIC_MAX_AGE, expire_attr: METRIC_EXPIRE_ATTR, filter: nil) ⇒ MetricsContainer

Returns a new instance of MetricsContainer.



11
12
13
14
15
16
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 11

def initialize(ttl: METRIC_MAX_AGE, expire_attr: METRIC_EXPIRE_ATTR, filter: nil)
  @data          = []
  @ttl           = ttl
  @expire_attr   = expire_attr
  @filter        = filter
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 8

def data
  @data
end

#filterObject

Returns the value of attribute filter.



9
10
11
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 9

def filter
  @filter
end

#ttlObject (readonly)

Returns the value of attribute ttl.



8
9
10
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 8

def ttl
  @ttl
end

Instance Method Details

#<<(obj) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 18

def <<(obj)
  now = get_time
  obj[@expire_attr] = now + @ttl

  expire(time: now, new_metric: obj)

  @data << obj
  @data
end

#[](key) ⇒ Object



28
29
30
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 28

def [](key)
  @data.tap { expire }[key]
end

#each(&blk) ⇒ Object



41
42
43
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 41

def each(&blk)
  wrap_expire(:each, &blk)
end

#expire(time: nil, new_metric: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 45

def expire(time: nil, new_metric: nil)
  time ||= get_time

  @data.delete_if do |metric|
    expired = metric[@expire_attr] < time
    expired ||= filter.call(new_metric, metric) if @filter && new_metric
    expired
  end
end

#map(&blk) ⇒ Object



37
38
39
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 37

def map(&blk)
  wrap_expire(:map, &blk)
end

#size(&blk) ⇒ Object Also known as: length



32
33
34
# File 'lib/prometheus_exporter/server/metrics_container.rb', line 32

def size(&blk)
  wrap_expire(:size, &blk)
end