Class: Fluent::Plugin::PromMetricsAggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/prometheus_metrics.rb

Instance Method Summary collapse

Constructor Details

#initializePromMetricsAggregator

Returns a new instance of PromMetricsAggregator.



30
31
32
# File 'lib/fluent/plugin/prometheus_metrics.rb', line 30

def initialize
  @metrics = {}
end

Instance Method Details

#add_metrics(metrics) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



34
35
36
37
38
39
40
41
# 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_metricsObject



73
74
75
# 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