Commit eb68a596 authored by Dylan Griffith's avatar Dylan Griffith

Log extra metadata about searches

Logging these extra fields as top level keys will ensure that we can
query/filter them in Kibana which will help us to answer questions about
common search terms or understand usage of search within individual
groups. Currently these query params are already logged but they are
nested keys they are not indexed and cannot be filtered in any way in
Kibana.

This change adds to the [existing logic in ApplicationController](
t https://gitlab.com/gitlab-org/gitlab/-/blob/452043cb89801feec2609b83a4ab6a25bfc99c08/app/controllers/application_controller.rb#L148
)
which in term relies on the [`append_info_to_payload`](
https://github.com/rails/rails/blob/fbe2433be6e052a1acac63c7faf287c52ed3c5ba/actionpack/lib/action_controller/metal/instrumentation.rb#L89
) being invoked after a request.

https://gitlab.com/gitlab-org/gitlab/-/issues/227884
parent 452043cb
......@@ -114,4 +114,15 @@ class SearchController < ApplicationController
Gitlab::UsageDataCounters::SearchCounter.count(:navbar_searches)
end
def append_info_to_payload(payload)
super
# Merging to :metadata will ensure these are logged as top level keys
payload[:metadata] || {}
payload[:metadata]['meta.search.group_id'] = params[:group_id]
payload[:metadata]['meta.search.project_id'] = params[:project_id]
payload[:metadata]['meta.search.search'] = params[:search]
payload[:metadata]['meta.search.scope'] = params[:scope]
end
end
......@@ -216,4 +216,23 @@ RSpec.describe SearchController do
it_behaves_like 'when the user cannot read cross project', :autocomplete, { term: 'hello' }
it_behaves_like 'with external authorization service enabled', :autocomplete, { term: 'hello' }
end
describe '#append_info_to_payload' do
it 'appends search metadata for logging' do
last_payload = nil
original_append_info_to_payload = controller.method(:append_info_to_payload)
expect(controller).to receive(:append_info_to_payload) do |payload|
original_append_info_to_payload.call(payload)
last_payload = payload
end
get :show, params: { scope: 'issues', search: 'hello world', group_id: '123', project_id: '456' }
expect(last_payload[:metadata]['meta.search.group_id']).to eq('123')
expect(last_payload[:metadata]['meta.search.project_id']).to eq('456')
expect(last_payload[:metadata]['meta.search.search']).to eq('hello world')
expect(last_payload[:metadata]['meta.search.scope']).to eq('issues')
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment