From a9922f267d3f23955d1eaadced0c36b740291617 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Thu, 27 Aug 2020 15:24:58 +0000
Subject: [PATCH] Bug 26310: Allow setting trace_to parameter in Elaasticsearch
 config

For debugging purposes we may wish to see the requests and responses made to
Elasticsearch

To test:
1 - prove -v t/Koha/SearchEngine/Elasticsearch.t
2 - Set <trace_to>Stderr</trace_to> in koha-conf
3 - Restart all
4 - perl misc/search_tools/rebuild_elasticsearch.pl
5 - Note requests are shown
6 - Set
    <trace_to>File</trace_to>
    <trace_to>/var/log/koha/kohadev/plack-error.log</trace_to>
    in koha-conf
7 - Restart all
8 - perl misc/search_tools/rebuild_elasticsearch.pl
9 - Check the plack log and see the ES requests
---
 Koha/SearchEngine/Elasticsearch.pm  | 2 ++
 etc/koha-conf.xml                   | 2 ++
 t/Koha/SearchEngine/Elasticsearch.t | 7 ++++++-
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm
index b518405cf5..67a0b641c7 100644
--- a/Koha/SearchEngine/Elasticsearch.pm
+++ b/Koha/SearchEngine/Elasticsearch.pm
@@ -1253,6 +1253,8 @@ sub _read_configuration {
 
     $configuration->{cxn_pool} = $conf->{cxn_pool} // 'Static';
 
+    $configuration->{trace_to} = $conf->{trace_to} if defined $conf->{trace_to};
+
     return $configuration;
 }
 
diff --git a/etc/koha-conf.xml b/etc/koha-conf.xml
index 7c0bc68e42..049d03ab5f 100644
--- a/etc/koha-conf.xml
+++ b/etc/koha-conf.xml
@@ -178,6 +178,8 @@ __PAZPAR2_TOGGLE_XML_POST__
 
      <!-- See https://metacpan.org/pod/Search::Elasticsearch#cxn_pool -->
      <cxn_pool>Static</cxn_pool>
+     <!-- See https://metacpan.org/pod/Search::Elasticsearch#trace_to -->
+     <!-- <trace_to>Stderr</trace_to> -->
  </elasticsearch>
  <!-- Uncomment the following line if you want to override the Elasticsearch default index settings -->
  <!-- <elasticsearch_index_config>__KOHA_CONF_DIR__/searchengine/elasticsearch/index_config.yaml</elasticsearch_index_config> -->
diff --git a/t/Koha/SearchEngine/Elasticsearch.t b/t/Koha/SearchEngine/Elasticsearch.t
index c4bcc1bc6a..524eefdff9 100644
--- a/t/Koha/SearchEngine/Elasticsearch.t
+++ b/t/Koha/SearchEngine/Elasticsearch.t
@@ -33,7 +33,7 @@ use Koha::SearchEngine::Elasticsearch::Search;
 
 subtest '_read_configuration() tests' => sub {
 
-    plan tests => 13;
+    plan tests => 15;
 
     my $configuration;
     t::lib::Mocks::mock_config( 'elasticsearch', undef );
@@ -96,10 +96,15 @@ subtest '_read_configuration() tests' => sub {
 
     $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
     is( $configuration->{cxn_pool}, 'Fluid', 'cxn_pool configuration parsed correctly' );
+    isnt( defined $configuration->{trace_to}, 'trace_to is not defined if not set' );
 
     my $params = Koha::SearchEngine::Elasticsearch::get_elasticsearch_params;
     is_deeply( $configuration->{nodes}, \@servers , 'get_elasticsearch_params is just a wrapper for _read_configuration' );
 
+    t::lib::Mocks::mock_config( 'elasticsearch', { server => \@servers, index_name => 'index', cxn_pool => 'Fluid', trace_to => 'Stderr' } );
+
+    $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
+    is( $configuration->{trace_to}, 'Stderr', 'trace_to configuration parsed correctly' );
 };
 
 subtest 'get_elasticsearch_settings() tests' => sub {
-- 
2.11.0