Class: PrometheusExporter::Ext::Metric::GaugeWithExpire

Inherits:
Metric::Gauge
  • Object
show all
Defined in:
lib/prometheus_exporter/ext/metric/gauge_with_expire.rb

Constant Summary collapse

NULLIFY_STRATEGY_UPDATE =
->(labels) { expiration_times[labels] = now_time + ttl }.freeze
NULLIFY_STRATEGY_EXPIRE =
->(labels) { data.delete(labels) }.freeze
ZEROING_STRATEGY_UPDATE =
->(labels) do
  if data[labels].zero?
    expiration_times.delete(labels)
  else
    expiration_times[labels] = now_time + ttl
  end
end.freeze
ZEROING_STRATEGY_EXPIRE =
->(labels) { data[labels] = 0 }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, help, opts = {}) ⇒ GaugeWithExpire

Returns a new instance of GaugeWithExpire.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 39

def initialize(name, help, opts = {})
  super(name, help)
  @ttl = opts[:ttl] || self.class.default_ttl
  raise ArgumentError, ':ttl must be numeric' unless ttl.is_a?(Numeric)
  raise ArgumentError, ":ttl must be greater than zero: #{ttl.inspect}" unless ttl.positive?

  @strategy = self.class.strategies.fetch(opts[:strategy] || :removing) do
    raise ArgumentError, "Unknown strategy: #{opts[:strategy].inspect}"
  end
end

Instance Attribute Details

#expiration_timesObject (readonly)

Returns the value of attribute expiration_times.



37
38
39
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 37

def expiration_times
  @expiration_times
end

#ttlObject (readonly)

Returns the value of attribute ttl.



37
38
39
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 37

def ttl
  @ttl
end

Class Method Details

.default_ttlObject



32
33
34
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 32

def default_ttl
  60
end

.strategiesHash

Returns key - strategy name value [Hash] with keys: :on_update, :on_expire

:on_update [Proc] yieldparam labels [Hash] - updates expiration_times after data was updated (instance exec)
:on_expire [Proc] yieldparam labels [Hash] - updates data after expiration_times was expired (instance exec).

Returns:

  • (Hash)

    key - strategy name value [Hash] with keys: :on_update, :on_expire

    :on_update [Proc] yieldparam labels [Hash] - updates expiration_times after data was updated (instance exec)
    :on_expire [Proc] yieldparam labels [Hash] - updates data after expiration_times was expired (instance exec)
    


25
26
27
28
29
30
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 25

def strategies
  {
    removing: { on_update: NULLIFY_STRATEGY_UPDATE, on_expire: NULLIFY_STRATEGY_EXPIRE },
    zeroing: { on_update: ZEROING_STRATEGY_UPDATE, on_expire: ZEROING_STRATEGY_EXPIRE }
  }
end

Instance Method Details

#decrement(labels = {}, value = 1) ⇒ Object



78
79
80
81
82
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 78

def decrement(labels = {}, value = 1)
  result = super
  update_expired_at(labels)
  result
end

#expireObject



84
85
86
87
88
89
90
91
92
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 84

def expire
  now = now_time
  expiration_times.each do |labels, expired_at|
    if expired_at < now
      remove_data_when_expired(labels)
      remove_expired_at(labels)
    end
  end
end

#increment(labels = {}, value = 1) ⇒ Object



72
73
74
75
76
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 72

def increment(labels = {}, value = 1)
  result = super
  update_expired_at(labels)
  result
end

#metric_textObject



55
56
57
58
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 55

def metric_text
  expire
  super
end

#observe(value, labels = {}) ⇒ Object



66
67
68
69
70
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 66

def observe(value, labels = {})
  result = super
  value.nil? ? remove_expired_at(labels) : update_expired_at(labels)
  result
end

#remove(labels) ⇒ Object



60
61
62
63
64
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 60

def remove(labels)
  result = super
  remove_expired_at(labels)
  result
end

#reset!Object



50
51
52
53
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 50

def reset!
  @expiration_times = {}
  super
end

#to_hObject



94
95
96
97
# File 'lib/prometheus_exporter/ext/metric/gauge_with_expire.rb', line 94

def to_h
  expire
  super
end