Module: Fluent::Prometheus

Included in:
PrometheusFilter, PrometheusOutput
Defined in:
lib/fluent/plugin/prometheus.rb

Defined Under Namespace

Classes: AlreadyRegisteredError, Counter, Gauge, Histogram, Metric, Summary

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_labels_elements(conf) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fluent/plugin/prometheus.rb', line 8

def self.parse_labels_elements(conf)
  labels = conf.elements.select { |e| e.name == 'labels' }
  if labels.size > 1
    raise ConfigError, "labels section must have at most 1"
  end

  base_labels = {}
  unless labels.empty?
    labels.first.each do |key, value|
      labels.first.has_key?(key)
      base_labels[key.to_sym] = value
    end
  end

  base_labels
end

.parse_metrics_elements(conf, registry, labels = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fluent/plugin/prometheus.rb', line 25

def self.parse_metrics_elements(conf, registry, labels = {})
  metrics = []
  conf.elements.select { |element|
    element.name == 'metric'
  }.each { |element|
    case element['type']
    when 'summary'
      metrics << Fluent::Prometheus::Summary.new(element, registry, labels)
    when 'gauge'
      metrics << Fluent::Prometheus::Gauge.new(element, registry, labels)
    when 'counter'
      metrics << Fluent::Prometheus::Counter.new(element, registry, labels)
    when 'histogram'
      metrics << Fluent::Prometheus::Histogram.new(element, registry, labels)
    else
      raise ConfigError, "type option must be 'counter', 'gauge', 'summary' or 'histogram'"
    end
  }
  metrics
end

.placeholder_expander(log) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fluent/plugin/prometheus.rb', line 46

def self.placeholder_expander(log)
  # Use internal class in order to expand placeholder
  require 'fluent/plugin/filter_record_transformer'
  if defined?(Fluent::Plugin::RecordTransformerFilter::PlaceholderExpander)
    # for v0.14
    return Fluent::Plugin::RecordTransformerFilter::PlaceholderExpander.new(log: log)
  else
    # for v0.12
    return Fluent::RecordTransformerFilter::PlaceholderExpander.new(log: log)
  end
rescue LoadError => e
  raise ConfigError, "cannot find filter_record_transformer plugin: #{e.message}"
end

Instance Method Details

#configure(conf) ⇒ Object



60
61
62
63
64
# File 'lib/fluent/plugin/prometheus.rb', line 60

def configure(conf)
  super
  @placeholder_expander = Fluent::Prometheus.placeholder_expander(log)
  @hostname = Socket.gethostname
end

#instrument(tag, es, metrics) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fluent/plugin/prometheus.rb', line 66

def instrument(tag, es, metrics)
  placeholder_values = {
    'tag' => tag,
    'hostname' => @hostname,
  }

  es.each do |time, record|
    placeholders = record.merge(placeholder_values)
    placeholders = @placeholder_expander.prepare_placeholders(placeholders)
    metrics.each do |metric|
      begin
        metric.instrument(record, @placeholder_expander, placeholders)
      rescue => e
        log.warn "prometheus: failed to instrument a metric.", error_class: e.class, error: e, tag: tag, name: metric.name
        router.emit_error_event(tag, time, record, e)
      end
    end
  end
end