forked from 18F/identity-idp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaraday.rb
62 lines (59 loc) · 2.1 KB
/
faraday.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
# frozen_string_literal: true
# Instruments Faraday requests using ActiveSupport::Notifications and Faraday's
# instrumentation middleware. This file subscribes to both 'request_metric.faraday' and
# 'request_log.faraday' events. 'request_metric.faraday' is for requests which we
# want to turn into metrics, and potentially create alarms for. These requests should be
# critical to the functionality of the IDP. Less critical requests can still be instrumented and
# logged with 'request_log.faraday'.
#
# Requests publishing to 'request_metric.faraday' must include a context that describes the request
# under the :service_name key. The value must made of non-space ASCII characters due to
# CloudWatch (https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_Dimension.html).
#
# Example instrumentation of Faraday request:
#
# Faraday.new(ssl: ssl_config) do |f|
# f.request :instrumentation, name: 'request_metric.faraday'
# end.post(
# verify_token_uri,
# URI.encode_www_form({ token: token }),
# Authentication: authenticate(token),
# ) do |req|
# req.options.context = { service_name: 'piv_cac_token' }
# end
ActiveSupport::Notifications.subscribe('request_metric.faraday') do |name, starts, ends, _, env|
url = env[:url]
http_method = env[:method].to_s.upcase
duration_seconds = ends - starts
service = env.request.context.fetch(:service_name)
metadata = {
http_method: http_method,
host: url.host,
path: url.path,
duration_seconds: duration_seconds,
status: env.status,
service: service,
name: 'request_metric.faraday',
}
Rails.logger.info(
metadata.to_json,
)
end
ActiveSupport::Notifications.subscribe('request_log.faraday') do |name, starts, ends, _, env|
url = env[:url]
http_method = env[:method].to_s.upcase
duration_seconds = ends - starts
service = env.request.context&.dig(:service_name)
metadata = {
http_method: http_method,
host: url.host,
path: url.path,
duration_seconds: duration_seconds,
status: env.status,
service: service,
name: 'request_log.faraday',
}
Rails.logger.info(
metadata.to_json,
)
end