Class: Fluent::Plugin::Prometheus::Histogram

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) ⇒ Histogram

Returns a new instance of Histogram.



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/fluent/plugin/prometheus.rb', line 277

def initialize(element, registry, labels)
  super
  if @key.nil?
    raise ConfigError, "histogram metric requires 'key' option"
  end

  begin
    if element['buckets']
      buckets = element['buckets'].split(/,/).map(&:strip).map do |e|
        e[/\A\d+.\d+\Z/] ? e.to_f : e.to_i
      end
      @histogram = registry.histogram(element['name'].to_sym, docstring: element['desc'], labels: @base_labels.keys, buckets: buckets)
    else
      @histogram = registry.histogram(element['name'].to_sym, docstring: element['desc'], labels: @base_labels.keys)
    end
  rescue ::Prometheus::Client::Registry::AlreadyRegisteredError
    @histogram = Fluent::Plugin::Prometheus::Metric.get(registry, element['name'].to_sym, :histogram, element['desc'])
  end
end

Instance Method Details

#instrument(record, expander) ⇒ Object



297
298
299
300
301
302
303
304
305
306
# File 'lib/fluent/plugin/prometheus.rb', line 297

def instrument(record, expander)
  if @key.is_a?(String)
    value = record[@key]
  else
    value = @key.call(record)
  end
  if value
    @histogram.observe(value, labels: labels(record, expander))
  end
end