Skip to content
Draft
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
16 changes: 15 additions & 1 deletion app/controllers/searches_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class SearchesController < ApplicationController
before_action :validate_query_params, only: :show
after_action :add_response_time_header

def show
render json: DiscoveryEngine::Query::Search.new(query_params, user_agent: request.user_agent).result_set
with_duration_logging do
@search_results = DiscoveryEngine::Query::Search.new(query_params, user_agent: request.user_agent).result_set
end
render json: @search_results
end

private
Expand All @@ -14,4 +18,14 @@ def query_params
def validate_query_params
raise ActionController::BadRequest, "Invalid query parameter" unless params.fetch(:q, "").is_a?(String)
end

def with_duration_logging(&block)
@duration = Benchmark.realtime(&block)
end

def add_response_time_header
return unless @duration

response.headers["Vertex-Response-Time"] = @duration.round(4).to_s
end
end
7 changes: 7 additions & 0 deletions config/initializers/logstasher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if ENV["GOVUK_RAILS_JSON_LOGGING"].present?
GovukJsonLogging.configure do
add_custom_fields do |fields|
fields[:vertex_search_duration] = response.headers["Vertex-Response-Time"]
end
end
end
5 changes: 5 additions & 0 deletions spec/requests/search_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
})
end

it "adds a vertex response time header to the response" do
get "/search.json"
expect(response.headers["vertex-response-time"]).not_to be_nil
end

it "passes any query parameters and user agent to the search service in the expected format" do
get "/search.json?q=garden+centres&start=11&count=22&filter_public_timestamp=from:2019-01-01",
headers: { "User-Agent" => "gds-api-adapters/99.2.0 (finder-frontend)" }
Expand Down
Loading