Skip to content

Commit

Permalink
Merge pull request #1202 from alphagov/add-govuk_request_id-to-logs
Browse files Browse the repository at this point in the history
Add govuk_request_id to logs when available
  • Loading branch information
tunylund authored Jul 19, 2023
2 parents ea01cd9 + 427398d commit 608e8c0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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
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

0 comments on commit 608e8c0

Please sign in to comment.