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, #retention, #retention_check_interval, #type

Instance Method Summary collapse

Methods inherited from Metric

get, #has_retention?, #instrument, #labels, #remove_expired_metrics, #set_value?

Constructor Details

#initialize(element, registry, labels) ⇒ Histogram

Returns a new instance of Histogram.



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/fluent/plugin/prometheus.rb', line 384

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

#set_value(value, labels) ⇒ Object



412
413
414
# File 'lib/fluent/plugin/prometheus.rb', line 412

def set_value(value, labels)
  @histogram.observe(value, labels: labels)
end

#value(record) ⇒ Object



404
405
406
407
408
409
410
# File 'lib/fluent/plugin/prometheus.rb', line 404

def value(record)
  if @key.is_a?(String)
    record[@key]
  else
    @key.call(record)
  end
end