prometheus_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/parser.rb +56 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 07be3476590d88af8f595b1a98c8373e7d92f6d50957e92afbdf2b1489a9fdbc
4
+ data.tar.gz: 7339abafe9a6a064ee215189b03307c38bcc350989ac4e0d0e906ccf883f09be
5
+ SHA512:
6
+ metadata.gz: fc6856638171409a5f8c9ef503727e6452e694fe5380452a60c86196473b3f14cc22f128a938edcf9b3286a2ea786dc08e42ebda7deeba979f42c2e12e289d4f
7
+ data.tar.gz: 8ddd7ef3b50ff5a7697890b809b56631c9f123e3413146b60828839935d3dc93228cdea65518e33a0ac7001d015a4e17fc55f5b51b73ce0164234689ab88f6af
data/lib/parser.rb ADDED
@@ -0,0 +1,56 @@
1
+ class PrometheusParser
2
+ class Invalid < StandardError
3
+ def initialize
4
+ super("invalid input")
5
+ end
6
+ end
7
+
8
+ KEY_RE = /[\w:]+/
9
+ VALUE_RE = /-?\d+\.?\d*E?-?\d*|NaN/
10
+ ATTR_KEY_RE = /[ \w-]+/
11
+ ATTR_VALUE_RE = %r{\s*"([\\"'\sa-zA-Z0-9\-_/.+~]*)"\s*} # /\s*"(\S*)"\s*/
12
+
13
+ def self.parse(raw)
14
+ s = StringScanner.new(raw)
15
+ res = []
16
+ until s.eos?
17
+ if s.peek(1) == "#" # Skip comment lines
18
+ s.scan(/.*\n/)
19
+ next
20
+ end
21
+ key = s.scan KEY_RE
22
+ raise Invalid unless key
23
+ attrs = parse_attrs(s)
24
+ value = s.scan VALUE_RE
25
+ raise Invalid unless value
26
+ value = value.to_f
27
+ s.scan(/\n/)
28
+ res.push({ key:, attrs:, value: })
29
+ end
30
+ res
31
+ end
32
+
33
+ def self.parse_attrs(s)
34
+ attrs = {}
35
+ if s.scan(/\s|{/) == "{"
36
+ loop do
37
+ if s.peek(1) == "}"
38
+ s.scan(/}/)
39
+ break
40
+ end
41
+ key = s.scan ATTR_KEY_RE
42
+ raise Invalid unless key
43
+ key = key.strip.to_sym
44
+ s.scan(/=/)
45
+ s.scan ATTR_VALUE_RE
46
+
47
+ value = s[1] # grab first match of ATTR_VALUE_RE
48
+ raise Invalid unless value
49
+ attrs[key] = value
50
+ break if s.scan(/,|}/) == "}"
51
+ end
52
+ s.scan(/\s/)
53
+ end
54
+ attrs
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prometheus_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Magnus Landerblom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: mange@84codes.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/parser.rb
20
+ homepage: https://rubygems.org/gems/prometheus_parser
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.4.12
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Parse prometheus metrics
43
+ test_files: []