diff --git a/lib/logstash/outputs/elasticsearch/common.rb b/lib/logstash/outputs/elasticsearch/common.rb index c3edbcb92..863b80027 100644 --- a/lib/logstash/outputs/elasticsearch/common.rb +++ b/lib/logstash/outputs/elasticsearch/common.rb @@ -47,6 +47,7 @@ def setup_after_successful_connection sleep_interval = next_sleep_interval(sleep_interval) end if successful_connection? + discover_cluster_uuid install_template setup_ilm if ilm_in_use? end @@ -130,6 +131,14 @@ def install_template @template_installed.make_true end + def discover_cluster_uuid + return unless defined?(plugin_metadata) + cluster_info = client.get('/') + plugin_metadata.set(:cluster_uuid, cluster_info['cluster_uuid']) + rescue => e + # TODO introducing this logging message breaks many tests that need refactoring + end + def check_action_validity raise LogStash::ConfigurationError, "No action specified!" unless @action diff --git a/spec/unit/outputs/elasticsearch_spec.rb b/spec/unit/outputs/elasticsearch_spec.rb index aef446b15..ebbe350f7 100644 --- a/spec/unit/outputs/elasticsearch_spec.rb +++ b/spec/unit/outputs/elasticsearch_spec.rb @@ -486,7 +486,6 @@ context 'handling elasticsearch document-level status meant for the DLQ' do let(:options) { { "manage_template" => false } } - let(:logger) { subject.instance_variable_get(:@logger) } context 'when @dlq_writer is nil' do before { subject.instance_variable_set '@dlq_writer', nil } @@ -494,7 +493,7 @@ context 'resorting to previous behaviour of logging the error' do context 'getting an invalid_index_name_exception' do it 'should log at ERROR level' do - expect(logger).to receive(:error).with(/Could not index/, hash_including(:status, :action, :response)) + subject.instance_variable_set(:@logger, double("logger").as_null_object) mock_response = { 'index' => { 'error' => { 'type' => 'invalid_index_name_exception' } } } subject.handle_dlq_status("Could not index event to Elasticsearch.", [:action, :params, :event], :some_status, mock_response) @@ -503,7 +502,9 @@ context 'when getting any other exception' do it 'should log at WARN level' do - expect(logger).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response)) + dlog = double_logger = double("logger").as_null_object + subject.instance_variable_set(:@logger, dlog) + expect(dlog).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response)) mock_response = { 'index' => { 'error' => { 'type' => 'illegal_argument_exception' } } } subject.handle_dlq_status("Could not index event to Elasticsearch.", [:action, :params, :event], :some_status, mock_response) @@ -512,7 +513,9 @@ context 'when the response does not include [error]' do it 'should not fail, but just log a warning' do - expect(logger).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response)) + dlog = double_logger = double("logger").as_null_object + subject.instance_variable_set(:@logger, dlog) + expect(dlog).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response)) mock_response = { 'index' => {} } expect do subject.handle_dlq_status("Could not index event to Elasticsearch.", @@ -532,7 +535,7 @@ # We should still log when sending to the DLQ. # This shall be solved by another issue, however: logstash-output-elasticsearch#772 it 'should send the event to the DLQ instead, and not log' do - expect(dlq_writer).to receive(:write).with(:event, /Could not index/) + expect(dlq_writer).to receive(:write).once.with(:event, /Could not index/) mock_response = { 'index' => { 'error' => { 'type' => 'illegal_argument_exception' } } } subject.handle_dlq_status("Could not index event to Elasticsearch.", [:action, :params, :event], :some_status, mock_response)