Skip to content

Commit e2fdab5

Browse files
committed
Add the vertex response time to the response header
This is a bit of a workaround so that we can see vertex response time for individual queries in the kibana logs. However it looks like there is still an issue with the logstasher initialisation - running the app in docker I can see errors that were supposed to have been fixed by https://github.com/alphagov/govuk_app_config/pull/561/changes https://github.com/alphagov/govuk_app_config/pull/564/changes
1 parent c6acd45 commit e2fdab5

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

app/controllers/searches_controller.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
class SearchesController < ApplicationController
22
before_action :validate_query_params, only: :show
3+
after_action :add_response_time_header
34

45
def show
5-
ActiveSupport::Notifications.instrument("vertex_search_request_duration") do
6-
render json: DiscoveryEngine::Query::Search.new(query_params, user_agent: request.user_agent).result_set
6+
with_duration_logging do
7+
@search_results = DiscoveryEngine::Query::Search.new(query_params, user_agent: request.user_agent).result_set
78
end
9+
render json: @search_results
810
end
911

1012
private
@@ -16,4 +18,14 @@ def query_params
1618
def validate_query_params
1719
raise ActionController::BadRequest, "Invalid query parameter" unless params.fetch(:q, "").is_a?(String)
1820
end
21+
22+
def with_duration_logging(&block)
23+
@duration = Benchmark.realtime(&block)
24+
end
25+
26+
def add_response_time_header
27+
return unless @duration
28+
29+
response.headers["Vertex-Response-Time"] = @duration.round(4).to_s
30+
end
1931
end

config/initializers/logstasher.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
if ENV["GOVUK_RAILS_JSON_LOGGING"].present?
2-
ActiveSupport::Notifications.subscribe("vertex_search_request_duration") do |event|
3-
duration_in_seconds = event.duration / 1000.0
4-
5-
GovukJsonLogging.configure do
6-
add_custom_fields do |fields|
7-
fields[:vertex_search_duration] = duration_in_seconds
8-
end
2+
GovukJsonLogging.configure do
3+
add_custom_fields do |fields|
4+
fields[:vertex_search_duration] = response.headers["Vertex-Response-Time"]
95
end
106
end
117
end

spec/requests/search_request_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
})
2424
end
2525

26+
it "adds a vertex response time header to the response" do
27+
get "/search.json"
28+
expect(response.headers["vertex-response-time"]).not_to be_nil
29+
end
30+
2631
it "passes any query parameters and user agent to the search service in the expected format" do
2732
get "/search.json?q=garden+centres&start=11&count=22&filter_public_timestamp=from:2019-01-01",
2833
headers: { "User-Agent" => "gds-api-adapters/99.2.0 (finder-frontend)" }

0 commit comments

Comments
 (0)