Sha256: f441dd23249e5ffd58fe170b08330d665fa879b24e84a990d6ae15daa7b3b956

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

class PrometheusParser
  class Invalid < StandardError
    def initialize
      super("invalid input")
    end
  end

  KEY_RE = /[\w:]+/
  VALUE_RE = /-?\d+\.?\d*E?-?\d*|NaN/
  ATTR_KEY_RE = /[ \w-]+/
  ATTR_VALUE_RE = %r{\s*"([\\"'\sa-zA-Z0-9\-_/.+~@;=]*)"\s*} # /\s*"(\S*)"\s*/

  def self.parse(raw)
    s = StringScanner.new(raw)
    res = []
    until s.eos?
      if s.peek(1) == "#" || s.peek(1) == "\n" # Skip comment and empty lines
        s.scan(/.*\n/)
        next
      end
      key = s.scan KEY_RE
      raise Invalid unless key
      attrs = parse_attrs(s)
      value = s.scan VALUE_RE
      raise Invalid unless value
      value = value.to_f
      s.scan(/\n/)
      res.push({ key:, attrs:, value: })
    end
    res
  end

  def self.parse_attrs(s)
    attrs = {}
    if s.scan(/\s|{/) == "{"
      loop do
        if s.peek(1) == "}"
          s.scan(/}/)
          break
        end
        key = s.scan ATTR_KEY_RE
        raise Invalid unless key
        key = key.strip.to_sym
        s.scan(/=/)
        s.scan ATTR_VALUE_RE

        value = s[1] # grab first match of ATTR_VALUE_RE
        raise Invalid unless value
        attrs[key] = value
        break if s.scan(/,|}/) == "}"
      end
      s.scan(/\s/)
    end
    attrs
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prometheus_parser-0.2.1 lib/prometheus_parser.rb