module Fluent::Plugin::Prometheus
Public Class Methods
parse_labels_elements(conf)
click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 35 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) # use RecordAccessor only for $. and $[ syntax # otherwise use the value as is or expand the value by RecordTransformer for ${} syntax if value.start_with?('$.') || value.start_with?('$[') base_labels[key.to_sym] = PluginHelper::RecordAccessor::Accessor.new(value) else base_labels[key.to_sym] = value end end end base_labels end
parse_metrics_elements(conf, registry, labels = {})
click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 59 def self.parse_metrics_elements(conf, registry, labels = {}) metrics = [] conf.elements.select { |element| element.name == 'metric' }.each { |element| if element.has_key?('key') && (element['key'].start_with?('$.') || element['key'].start_with?('$[')) value = element['key'] element['key'] = PluginHelper::RecordAccessor::Accessor.new(value) end case element['type'] when 'summary' metrics << Fluent::Plugin::Prometheus::Summary.new(element, registry, labels) when 'gauge' metrics << Fluent::Plugin::Prometheus::Gauge.new(element, registry, labels) when 'counter' metrics << Fluent::Plugin::Prometheus::Counter.new(element, registry, labels) when 'histogram' metrics << Fluent::Plugin::Prometheus::Histogram.new(element, registry, labels) else raise ConfigError, "type option must be 'counter', 'gauge', 'summary' or 'histogram'" end } metrics end
placeholder_expander(log)
click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 84 def self.placeholder_expander(log) Fluent::Plugin::Prometheus::ExpandBuilder.new(log: log) end
Public Instance Methods
configure(conf)
click to toggle source
Calls superclass method
# File lib/fluent/plugin/prometheus.rb, line 100 def configure(conf) super @placeholder_values = {} @placeholder_expander_builder = Fluent::Plugin::Prometheus.placeholder_expander(log) @hostname = Socket.gethostname end
instrument(tag, es, metrics)
click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 127 def instrument(tag, es, metrics) placeholder_values = { 'tag' => tag, 'hostname' => @hostname, 'worker_id' => fluentd_worker_id, } es.each do |time, record| record = stringify_keys(record) placeholders = record.merge(placeholder_values) expander = @placeholder_expander_builder.build(placeholders) metrics.each do |metric| begin metric.instrument(record, expander) 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
instrument_single(tag, time, record, metrics)
click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 107 def instrument_single(tag, time, record, metrics) @placeholder_values[tag] ||= { 'tag' => tag, 'hostname' => @hostname, 'worker_id' => fluentd_worker_id, } record = stringify_keys(record) placeholders = record.merge(@placeholder_values[tag]) expander = @placeholder_expander_builder.build(placeholders) metrics.each do |metric| begin metric.instrument(record, expander) 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
stringify_keys(hash_to_stringify)
click to toggle source
# File lib/fluent/plugin/prometheus.rb, line 88 def stringify_keys(hash_to_stringify) # Adapted from: https://www.jvt.me/posts/2019/09/07/ruby-hash-keys-string-symbol/ hash_to_stringify.map do |k,v| value_or_hash = if v.instance_of? Hash stringify_keys(v) else v end [k.to_s, value_or_hash] end.to_h end