From 7fd4c9ebe43458cb8801c23e78f9538d4d1a3312 Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Fri, 3 Feb 2023 14:44:31 +1100 Subject: [PATCH] Introduce ::Zoekt::Logger and log search errors Log failures from Zoekt searches in a new `zoekt.log` file. Changelog: added EE: true --- doc/administration/logs/index.md | 13 +++++++++++++ ee/lib/gitlab/zoekt/search_results.rb | 8 ++++++++ ee/lib/zoekt/logger.rb | 9 +++++++++ ee/spec/lib/gitlab/zoekt/search_results_spec.rb | 15 +++++++++++++++ ee/spec/lib/zoekt/logger_spec.rb | 11 +++++++++++ 5 files changed, 56 insertions(+) create mode 100644 ee/lib/zoekt/logger.rb create mode 100644 ee/spec/lib/zoekt/logger_spec.rb diff --git a/doc/administration/logs/index.md b/doc/administration/logs/index.md index 9893c6935a3527..eab4c9b7d83331 100644 --- a/doc/administration/logs/index.md +++ b/doc/administration/logs/index.md @@ -866,6 +866,19 @@ Depending on your installation method, this file is located at: - Omnibus GitLab: `/var/log/gitlab/gitlab-rails/database_load_balancing.log` - Installations from source: `/home/git/gitlab/log/database_load_balancing.log` +## `zoekt.log` **(PREMIUM SELF)** + +> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110980) in GitLab 15.9. + +This file logs information related to the +[Exact code search](../../user/search/exact_code_search.md) feature which is +powered by Zoekt. + +Depending on your installation method, this file is located at: + +- Omnibus GitLab: `/var/log/gitlab/gitlab-rails/zoekt.log` +- Installations from source: `/home/git/gitlab/log/zoekt.log` + ## `elasticsearch.log` **(PREMIUM SELF)** > Introduced in GitLab 12.6. diff --git a/ee/lib/gitlab/zoekt/search_results.rb b/ee/lib/gitlab/zoekt/search_results.rb index 208b28675fd8a0..a4a9dc2f743518 100644 --- a/ee/lib/gitlab/zoekt/search_results.rb +++ b/ee/lib/gitlab/zoekt/search_results.rb @@ -138,6 +138,10 @@ def zoekt_search(query, num:, options:) allow_local_requests: true ) + unless response.success? + logger.error(message: "Zoekt search failed", status: response.code, response: response.body) + end + ::Gitlab::Json.parse(response.body, symbolize_names: true) end @@ -198,6 +202,10 @@ def yield_each_zoekt_search_result(response, preload_method, total_count) [items, total_count] end + + def logger + @logger ||= ::Zoekt::Logger.build + end end end end diff --git a/ee/lib/zoekt/logger.rb b/ee/lib/zoekt/logger.rb new file mode 100644 index 00000000000000..561819038bb65d --- /dev/null +++ b/ee/lib/zoekt/logger.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Zoekt + class Logger < ::Gitlab::JsonLogger + def self.file_name_noext + 'zoekt' + end + end +end diff --git a/ee/spec/lib/gitlab/zoekt/search_results_spec.rb b/ee/spec/lib/gitlab/zoekt/search_results_spec.rb index 4b4e68f6773e64..14e2a8f4e91d46 100644 --- a/ee/spec/lib/gitlab/zoekt/search_results_spec.rb +++ b/ee/spec/lib/gitlab/zoekt/search_results_spec.rb @@ -61,6 +61,21 @@ expect(results.blobs_count).to eq 0 end + context 'with an invalid search' do + it 'logs an error and returns an empty array for results', :aggregate_failures do + search_results = described_class.new(user, '(invalid search(', limit_project_ids) + + logger = instance_double(::Zoekt::Logger) + expect(::Zoekt::Logger).to receive(:build).and_return(logger) + expect(logger).to receive(:error).with(hash_including(status: 400)) + + blobs = search_results.objects('blobs') + expect(blobs).to be_empty + expect(search_results).to be_failed + expect(search_results.error).to include('error parsing regexp') + end + end + context 'when searching with special characters', :aggregate_failures do let(:examples) do { diff --git a/ee/spec/lib/zoekt/logger_spec.rb b/ee/spec/lib/zoekt/logger_spec.rb new file mode 100644 index 00000000000000..31260b311cacf8 --- /dev/null +++ b/ee/spec/lib/zoekt/logger_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Zoekt::Logger, feature_category: :global_search do + describe '.build' do + it 'builds an instance' do + expect(described_class.build).to be_an_instance_of(described_class) + end + end +end -- GitLab