-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.rb
82 lines (73 loc) · 2.11 KB
/
helpers.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
73
74
75
76
77
78
79
80
81
82
def flash_danger(message)
session[:flashes] << { type: 'alert-danger', message: message }
end
def flash_success(message)
session[:flashes] << { type: 'alert-success', message: message }
end
def log_event(type, name, message, params = '{}')
case type
when 'error'
logger.error('service=ui | ' \
"event=#{name} | " \
"request_id=#{request.env['REQUEST_ID']} | " \
"message=\'#{message}\' | " \
"params: #{params.to_json}")
when 'info'
logger.info('service=ui | ' \
"event=#{name} | " \
"request_id=#{request.env['REQUEST_ID']} | " \
"message=\'#{message}\' | " \
"params: #{params.to_json}")
when 'warning'
logger.warn('service=ui | ' \
"event=#{name} | " \
"request_id=#{request.env['REQUEST_ID']} | " \
"message=\'#{message}\' | " \
"params: #{params.to_json}")
end
end
def http_request(method, url, params = {})
unless defined?(request).nil?
settings.http_client.headers[:request_id] = request.env['REQUEST_ID'].to_s
end
case method
when 'get'
response = settings.http_client.get url
JSON.parse(response.body)
when 'post'
settings.http_client.post url, params
end
end
def http_healthcheck_handler(post_url, comment_url, version)
post_status = check_service_health(post_url)
comment_status = check_service_health(comment_url)
status = if comment_status == 1 && post_status == 1
1
else
0
end
healthcheck = { status: status,
dependent_services: {
comment: comment_status,
post: post_status
},
version: version }
healthcheck.to_json
end
def check_service_health(url)
name = http_request('get', "#{url}/healthcheck")
rescue StandardError
0
else
name['status']
end
def set_health_gauge(metric, value)
metric.set(
{
version: VERSION,
commit_hash: BUILD_INFO[0].strip,
branch: BUILD_INFO[1].strip
},
value
)
end