Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add govuk_request_id to logs when available #1202

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/gds_api/json_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def with_ssl_options(method_params)
end

def do_request(method, url, params = nil, additional_headers = {})
loggable = { request_uri: url, start_time: Time.now.to_f }
loggable = { request_uri: url, start_time: Time.now.to_f, govuk_request_id: GdsApi::GovukHeaders.headers[:govuk_request_id] }.compact
tunylund marked this conversation as resolved.
Show resolved Hide resolved
start_logging = loggable.merge(action: "start")
logger.debug start_logging.to_json

Expand Down
16 changes: 16 additions & 0 deletions test/json_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,22 @@ def test_should_use_custom_logger_specified_in_options
assert_same client.logger, custom_logger
end

def test_should_log_govuk_request_id_when_available
GdsApi::GovukHeaders.set_header(:govuk_request_id, "some-request-id")
custom_logger = mock
client = GdsApi::JsonClient.new(logger: custom_logger)
url = "http://www.example.com/timeout.json"
stub_request(:get, url).to_timeout

expected_string = "\"govuk_request_id\":\"some-request-id\""
custom_logger.expects(:debug).with(includes(expected_string))
custom_logger.expects(:error).with(includes(expected_string))

assert_raises do
client.get_json(url)
end
end

def test_should_avoid_content_type_header_on_get_without_body
url = "http://some.endpoint/some.json"
stub_request(:any, url)
Expand Down