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.



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/fluent/plugin/prometheus.rb', line 198

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, docstring: element['desc'], labels: @base_labels.keys)
  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) ⇒ Object



211
212
213
214
215
216
217
218
219
220
# File 'lib/fluent/plugin/prometheus.rb', line 211

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