Class: Fluent::Plugin::PrometheusTextParser

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

Overview

parser prometheus exposition format - text-based prometheus.io/docs/instrumenting/exposition_formats/

Constant Summary collapse

DEFAULT_TIME_FORMAT =
'%Q'
DEFAULT_DELIMITER =
"\n"
DEFAULT_LABEL_PREFIX =
''
EMPTY_RE =
/^[[:space:]]*$/.freeze
COMMENT_RE =
/^#/.freeze
HELP_RE =
/^#\sHELP\s(?<metric_name>\w+)\s(?<metric_docstring>.*)$/.freeze
TYPE_RE =
/^#\sTYPE\s(?<metric_name>\w+)\s(?<metric_type>\w+)/.freeze
METRIC_RE =
/^(?<metric_name>\w+)
(:?\{(?<labels>.*?)\})?\s*
(?<metric_value>[-\dEe.]+|NaN|[+-]Inf)
(?:\s(?<timestamp>-?\d+))?$/x.freeze
LABEL_RE =
/(?:\b(\w+)="(.*?)(?<!\\)"(?:,?|\b))/.freeze

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object

Raises:

  • (Fluent::ConfigError)


34
35
36
37
38
39
40
# File 'lib/fluent/plugin/parser_prometheus_text.rb', line 34

def configure(conf)
  super

  raise Fluent::ConfigError, 'delimiter must not be empty.' if !delimiter || delimiter.empty?

  @time_parser = Fluent::TimeParser.new(DEFAULT_TIME_FORMAT)
end

#parse(text) ⇒ Object



42
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
72
73
74
# File 'lib/fluent/plugin/parser_prometheus_text.rb', line 42

def parse(text)
  types_store = {}

  text.each_line(delimiter, chomp: true) do |entry|
    case entry
    when empty_re
      # skip blank entry
    when help_re
      # skip docstring
    when type_re
      types_store[Regexp.last_match('metric_name')] = Regexp.last_match('metric_type')
    when comment_re
      # skip comment
    when metric_re
      time = generate_metric_timestamp(Regexp.last_match('timestamp'))
      record = generate_metric_record(
        Regexp.last_match('metric_name'),
        Regexp.last_match('metric_value'),
        Regexp.last_match('labels')
      )
      if add_type
        if types_store.key?(record['metric_name'])
          record['metric_type'] = types_store[record['metric_name']]
        else
          warn("missing metric type for #{record['metric_name']}")
        end
      end
      yield time, record
    else
      error("skip unsupported prometheus entry: #{entry}")
    end
  end
end