class Fluent::Plugin::PromMetricsAggregator

Public Class Methods

new() click to toggle source
# File lib/fluent/plugin/prometheus_metrics.rb, line 30
def initialize
  @metrics = {}
end

Public Instance Methods

add_metrics(metrics) click to toggle source
# File lib/fluent/plugin/prometheus_metrics.rb, line 43
def add_metrics(metrics)
  current_metric = ''
  new_metric = false
  lines = metrics.split("\n")
  for line in lines
    if line[0] == '#'
      # Metric comment (# TYPE, # HELP)
      parsed_metric = get_metric_name_from_comment(line)
      if parsed_metric != ''
        if parsed_metric != current_metric
          # Starting a new metric comment block
          new_metric = !@metrics.key?(parsed_metric)
          if new_metric
            @metrics[parsed_metric] = PrometheusMetrics.new()
          end
          current_metric = parsed_metric
        end

        if new_metric && parsed_metric == current_metric
          # New metric, inject comments (# TYPE, # HELP)
          @metrics[parsed_metric].add_comment(line)
        end
      end
    else
      # Metric value, simply append line
      @metrics[current_metric].add_metric_value(line)
    end
  end
end
get_metric_name_from_comment(line) click to toggle source
# File lib/fluent/plugin/prometheus_metrics.rb, line 34
def get_metric_name_from_comment(line)
  tokens = line.split(' ')
  if ['HELP', 'TYPE'].include?(tokens[1])
    tokens[2]
  else
    ''
  end
end
get_metrics() click to toggle source
# File lib/fluent/plugin/prometheus_metrics.rb, line 73
def get_metrics
  @metrics.map{|k,v| v.to_string()}.join("\n") + (@metrics.length ? "\n" : "")
end