Skip to content

Commit 181ec8a

Browse files
cachedoutjsvd
authored andcommitted
Look up cluster_uuid
This change allows the plugin to inform other parts of Logstash about which ES cluster it is connecting to. It is built on top of the Plugin Metadata registry: elastic/logstash#10691 Note: The DLQ tests were written to just assume a clean buffer in the logs but this is not a safe assumption. Now we temporarily replace the logging subsystem with a double during these tests so that we can be assured that we are checking a clean instance.
1 parent b9a527e commit 181ec8a

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/logstash/outputs/elasticsearch/common.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def setup_after_successful_connection
4747
sleep_interval = next_sleep_interval(sleep_interval)
4848
end
4949
if successful_connection?
50+
discover_cluster_uuid
5051
install_template
5152
setup_ilm if ilm_in_use?
5253
end
@@ -130,6 +131,15 @@ def install_template
130131
@template_installed.make_true
131132
end
132133

134+
def discover_cluster_uuid
135+
return unless defined?(plugin_metadata)
136+
cluster_info = client.get('/')
137+
plugin_metadata.set(:cluster_uuid, cluster_info['cluster_uuid'])
138+
rescue => e
139+
# TODO introducing this logging message breaks many tests that need refactoring
140+
# @logger.error("Unable to retrieve elasticsearch cluster uuid", error => e.message)
141+
end
142+
133143
def check_action_validity
134144
raise LogStash::ConfigurationError, "No action specified!" unless @action
135145

spec/unit/outputs/elasticsearch_spec.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,15 +486,14 @@
486486

487487
context 'handling elasticsearch document-level status meant for the DLQ' do
488488
let(:options) { { "manage_template" => false } }
489-
let(:logger) { subject.instance_variable_get(:@logger) }
490489

491490
context 'when @dlq_writer is nil' do
492491
before { subject.instance_variable_set '@dlq_writer', nil }
493492

494493
context 'resorting to previous behaviour of logging the error' do
495494
context 'getting an invalid_index_name_exception' do
496495
it 'should log at ERROR level' do
497-
expect(logger).to receive(:error).with(/Could not index/, hash_including(:status, :action, :response))
496+
subject.instance_variable_set(:@logger, double("logger").as_null_object)
498497
mock_response = { 'index' => { 'error' => { 'type' => 'invalid_index_name_exception' } } }
499498
subject.handle_dlq_status("Could not index event to Elasticsearch.",
500499
[:action, :params, :event], :some_status, mock_response)
@@ -503,7 +502,9 @@
503502

504503
context 'when getting any other exception' do
505504
it 'should log at WARN level' do
506-
expect(logger).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response))
505+
dlog = double_logger = double("logger").as_null_object
506+
subject.instance_variable_set(:@logger, dlog)
507+
expect(dlog).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response))
507508
mock_response = { 'index' => { 'error' => { 'type' => 'illegal_argument_exception' } } }
508509
subject.handle_dlq_status("Could not index event to Elasticsearch.",
509510
[:action, :params, :event], :some_status, mock_response)
@@ -512,7 +513,9 @@
512513

513514
context 'when the response does not include [error]' do
514515
it 'should not fail, but just log a warning' do
515-
expect(logger).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response))
516+
dlog = double_logger = double("logger").as_null_object
517+
subject.instance_variable_set(:@logger, dlog)
518+
expect(dlog).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response))
516519
mock_response = { 'index' => {} }
517520
expect do
518521
subject.handle_dlq_status("Could not index event to Elasticsearch.",
@@ -532,7 +535,7 @@
532535
# We should still log when sending to the DLQ.
533536
# This shall be solved by another issue, however: logstash-output-elasticsearch#772
534537
it 'should send the event to the DLQ instead, and not log' do
535-
expect(dlq_writer).to receive(:write).with(:event, /Could not index/)
538+
expect(dlq_writer).to receive(:write).once.with(:event, /Could not index/)
536539
mock_response = { 'index' => { 'error' => { 'type' => 'illegal_argument_exception' } } }
537540
subject.handle_dlq_status("Could not index event to Elasticsearch.",
538541
[:action, :params, :event], :some_status, mock_response)

0 commit comments

Comments
 (0)