Class: Fluent::Plugin::PrometheusOutputMetric

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

Instance Method Summary collapse

Methods included from Prometheus

#instrument, #instrument_single, parse_labels_elements, parse_metrics_elements, placeholder_expander

Methods included from PrometheusLabelParser

#parse_labels_elements

Constructor Details

#initializePrometheusOutputMetric



11
12
13
14
# File 'lib/fluent/plugin/out_prometheus_metric.rb', line 11

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

Instance Method Details

#configure(conf) ⇒ Object



34
35
36
37
38
# File 'lib/fluent/plugin/out_prometheus_metric.rb', line 34

def configure(conf)
  super
  @labels = parse_labels_elements(conf)
  @placeholder_expander_builder = Fluent::Plugin::Prometheus.placeholder_expander(log)
end

#labels(labels, record, expander) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fluent/plugin/out_prometheus_metric.rb', line 22

def labels(labels, record, expander)
  label = {}
  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

#multi_workers_ready?Boolean



16
17
18
# File 'lib/fluent/plugin/out_prometheus_metric.rb', line 16

def multi_workers_ready?
  true
end

#process(tag, es) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fluent/plugin/out_prometheus_metric.rb', line 40

def process(tag, es)
  placeholder_values = {
    'tag' => tag,
    'worker_id' => fluentd_worker_id,
  }

  # 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
    expander = @placeholder_expander_builder.build(placeholder_values)
    if @key.is_a?(String)
      value = record[@key]
    else
      value = @key.call(record)
    end
    if value
      gauge.set(labels(@labels, record, expander), value)
    end
  end
end