Class: Fluent::Plugin::Prometheus::Counter

Inherits:
Metric
  • Object
show all
Defined in:
lib/fluent/plugin/prometheus.rb

Instance Attribute Summary

Attributes inherited from Metric

#desc, #key, #name, #type

Instance Method Summary collapse

Methods inherited from Metric

get, #labels

Constructor Details

#initialize(element, registry, labels) ⇒ Counter

Returns a new instance of Counter.



191
192
193
194
195
196
197
198
# File 'lib/fluent/plugin/prometheus.rb', line 191

def initialize(element, registry, labels)
  super
  begin
    @counter = registry.counter(element['name'].to_sym, element['desc'])
  rescue ::Prometheus::Client::Registry::AlreadyRegisteredError
    @counter = Fluent::Plugin::Prometheus::Metric.get(registry, element['name'].to_sym, :counter, element['desc'])
  end
end

Instance Method Details

#instrument(record, expander, placeholders) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/fluent/plugin/prometheus.rb', line 200

def instrument(record, expander, placeholders)
  # use record value of the key if key is specified, otherwise just increment
  if @key.nil?
    value = 1
  elsif @key.is_a?(String)
    value = record[@key]
  else
    value = @key.call(record)
  end

  # ignore if record value is nil
  return if value.nil?

  @counter.increment(labels(record, expander, placeholders), value)
end