forked from Cardano-Fans/crfa-prometheus-web-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrfa-prometheus-http-gateway.rb
72 lines (56 loc) · 1.78 KB
/
crfa-prometheus-http-gateway.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
require 'webrick'
require 'faraday'
require 'json'
server = WEBrick::HTTPServer.new(
:Port => 8082,
:SSLEnable => false,
:ServerSoftware => '¯\_(ツ)_/¯'
)
puts "Configured urls:"
ARGV.each { |url| puts url }
def filterMetrics(body)
obj = { }
body.each_line { | line |
if line.start_with?('node_directory_size_bytes')
split = line.gsub(/\s+/m, ' ').strip.split(" ")
obj[:cardanoDbSize] = split[1].strip
end
if line.start_with?('cardano_node_metrics_density_real')
split = line.gsub(/\s+/m, ' ').strip.split(" ")
obj[:chainDensity] = split[1].strip
end
if line.start_with?('cardano_node_metrics_utxoSize_int')
split = line.gsub(/\s+/m, ' ').strip.split(" ")
obj[:utxoSize] = split[1].strip
end
if line.start_with?('cardano_node_metrics_delegMapSize_int')
split = line.gsub(/\s+/m, ' ').strip.split(" ")
obj[:delegMapSize] = split[1].strip
end
if line.start_with?('cardano_node_metrics_forks_int')
split = line.gsub(/\s+/m, ' ').strip.split(" ")
obj[:chainForks] = split[1].strip
end
}
return obj.to_json
end
server.mount_proc '/' do |req, res|
res['Content-Type'] = 'application/json; charset=utf-8'
if req.path == '/cardano-metrics'
combined = ""
ARGV.each { |base_url|
puts base_url
url = "#{base_url}/metrics"
response = Faraday.get(url)
body = response.body
combined += body + "\n"
}
res.status = 200
res.body = filterMetrics(combined)
else
res.status = 404
res.body = "Not Found!"
end
end
trap 'INT' do server.shutdown end
server.start