-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
65 lines (48 loc) · 1.98 KB
/
config.ru
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
require 'rack'
require 'prometheus_exporter'
require 'prometheus_exporter/server'
server = PrometheusExporter::Server::WebServer.new bind: "0.0.0.0", port: 9394
server.start
$counter = PrometheusExporter::Metric::Counter.new("http_requests_total", "total number of web requests")
$http_request_duration_seconds = PrometheusExporter::Metric::Summary.new("http_request_duration_seconds", "time it took to complete a request", quantiles: [0.01, 0.1, 0.5, 0.9, 0.99])
server.collector.register_metric($counter)
server.collector.register_metric($http_request_duration_seconds)
$install_id = Time.now.to_i
$body_message = "Start ENV_MESSAGEs:\n"
(ENV.keys.select { |k| k.start_with?("ENV_MESSAGE") }).each do |k|
$body_message += "#{$install_id} - #{ENV[k]}\n"
end
$body_message += "End ENV_MESSAGEs.\n"
puts("GOVUK replatform test app - #{$install_id}\n#{$body_message}")
Dir['messages/*'].each do |filename|
file = File.open(filename)
filedata = file.read
file.close
$body_message += "#{filedata}\n"
puts("#{$install_id} - #{filedata}")
end
class RackApp
def call(env)
start = Time.now.to_f
req = Rack::Request.new(env)
path, query = req.fullpath.split('?')
body = ""
status = 200
if path == "/healthcheck/live" || path == "/healthcheck/ready" || path == "/readyz"
if !req.head?
body = "Version: #{$install_id}. Hello, the time is #{Time.now}, health check done"
end
else
qs = Rack::Utils.parse_nested_query query
status = qs["status"] || 400
if !req.head?
body = "Version: #{$install_id}. Hello, the time is #{Time.now}, you requested a #{qs["status"]} status response"
end
$counter.observe(1, route: path, status: status, install_id: $install_id)
duration = Time.now.to_f - start
$http_request_duration_seconds.observe(duration, action: 'test', status: status, install_id: $install_id)
end
[status, {"Content-Type" => "text/plain"}, [$body_message, body]]
end
end
run RackApp.new