Class: Fluent::Plugin::Prometheus::Gauge

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

Returns a new instance of Gauge.



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fluent/plugin/prometheus.rb', line 165

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

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

Instance Method Details

#instrument(record, expander, placeholders) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/fluent/plugin/prometheus.rb', line 178

def instrument(record, expander, placeholders)
  if @key.is_a?(String)
    value = record[@key]
  else
    value = @key.call(record)
  end
  if value
    @gauge.set(labels(record, expander, placeholders), value)
  end
end