Class: Fluent::Plugin::PrometheusOutputMetric

Inherits:
Output
  • Object
show all
Includes:
PrometheusLabelParser
Defined in:
lib/fluent/plugin/out_prometheus_metric.rb

Instance Method Summary collapse

Methods included from PrometheusLabelParser

#configure, #parse_labels_elements

Constructor Details

#initializePrometheusOutputMetric

Returns a new instance of PrometheusOutputMetric.



9
10
11
12
# File 'lib/fluent/plugin/out_prometheus_metric.rb', line 9

def initialize
  super
  @registry = ::Prometheus::Client.registry
end

Instance Method Details

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/fluent/plugin/out_prometheus_metric.rb', line 14

def multi_workers_ready?
  true
end

#process(tag, es) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fluent/plugin/out_prometheus_metric.rb', line 20

def process(tag, es)
  # Write out values in event stream to Registry
  es.each do |time, record|
    # Create metric if not exists
    begin
      desc = "fluentd message stream with tag: ${tag}"
      gauge = @registry.gauge(tag.to_sym, desc.sub!("${tag}", tag))
    rescue ::Prometheus::Client::Registry::AlreadyRegisteredError
      gauge = @registry.get(tag.to_sym)
    end
    value = record[@key]
    if value
      record.tap do |hs| 
        hs.delete(@key)
        hs.delete('timestamp')
      end
      # set labels as all elements in message besides the key
      gauge.set(record.transform_keys(&:to_sym), value)
    end
  end
end