Module: Fluent::Prometheus
- Included in:
- PrometheusFilter, PrometheusOutput
- Defined in:
- lib/fluent/plugin/prometheus.rb
Defined Under Namespace
Classes: AlreadyRegisteredError, Counter, Gauge, Metric, Summary
Class Method Summary collapse
- .parse_labels_elements(conf) ⇒ Object
- .parse_metrics_elements(conf, registry, labels = {}) ⇒ Object
- .placeholder_expander(log) ⇒ Object
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 |
# 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) else raise ConfigError, "type option must be 'counter', 'gauge' or 'summary'" end } metrics end |
.placeholder_expander(log) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fluent/plugin/prometheus.rb', line 44 def self.(log) # Use internal class in order to expand placeholder if defined?(Fluent::Filter) # for v0.12, built-in PlaceholderExpander begin require 'fluent/plugin/filter_record_transformer' return Fluent::RecordTransformerFilter::PlaceholderExpander.new(log: log) rescue LoadError => e raise ConfigError, "cannot find filter_record_transformer plugin: #{e.}" end else # for v0.10, use PlaceholderExapander in fluent-plugin-record-reformer plugin begin require 'fluent/plugin/out_record_reformer.rb' return Fluent::RecordReformerOutput::PlaceholderExpander.new(log: log) rescue LoadError => e raise ConfigError, "cannot find fluent-plugin-record-reformer: #{e.}" end end end |
Instance Method Details
#configure(conf) ⇒ Object
63 64 65 66 67 |
# File 'lib/fluent/plugin/prometheus.rb', line 63 def configure(conf) super @placeholder_expander = Fluent::Prometheus.(log) @hostname = Socket.gethostname end |
#instrument(tag, es, metrics) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fluent/plugin/prometheus.rb', line 69 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 |