class Fluent::Plugin::Prometheus::Metric

Attributes

desc[R]
key[R]
name[R]
type[R]

Public Class Methods

get(registry, name, type, docstring) click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 263
def self.get(registry, name, type, docstring)
  metric = registry.get(name)

  # should have same type, docstring
  if metric.type != type
    raise AlreadyRegisteredError, "#{name} has already been registered as #{type} type"
  end
  if metric.docstring != docstring
    raise AlreadyRegisteredError, "#{name} has already been registered with different docstring"
  end

  metric
end
init_label_set(metric, base_initlabels, base_labels) click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 238
def self.init_label_set(metric, base_initlabels, base_labels)
  base_initlabels.each { |initlabels|
    # Should never happen, but handy test should code evolution break current implementation
    if initlabels.keys.sort != base_labels.keys.sort
      raise ConfigError, "initlabels for metric #{metric.name} must have the same signature than labels " \
                        "(initlabels given: #{initlabels.keys} vs." \
                        " expected from labels: #{base_labels.keys})"
    end

    metric.init_label_set(initlabels)
  }
end
new(element, registry, labels) click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 218
def initialize(element, registry, labels)
  ['name', 'desc'].each do |key|
    if element[key].nil?
      raise ConfigError, "metric requires '#{key}' option"
    end
  end
  @type = element['type']
  @name = element['name']
  @key = element['key']
  @desc = element['desc']
  element['initialized'].nil? ? @initialized = false : @initialized = element['initialized'] == 'true'
  
  @base_labels = Fluent::Plugin::Prometheus.parse_labels_elements(element)
  @base_labels = labels.merge(@base_labels)

  if @initialized
    @base_initlabels = Fluent::Plugin::Prometheus.parse_initlabels_elements(element, @base_labels)
  end
end

Public Instance Methods

labels(record, expander) click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 251
def labels(record, expander)
  label = {}
  @base_labels.each do |k, v|
    if v.is_a?(String)
      label[k] = expander.expand(v)
    else
      label[k] = v.call(record)
    end
  end
  label
end