From 275abab17174bdc8f724e3b0c1ed9f728fb2f85a Mon Sep 17 00:00:00 2001 From: andsel Date: Thu, 14 Jan 2021 10:54:50 +0100 Subject: [PATCH] Fix Logstash pipelines management in case of slow loading pipelines or disabled webserver (#12571) This commit avoid an error in gathering monitoring information when webserver is disabled or is not yet started; which could happen with slow loading pipelines or no pipelines defined from the central management UI. (cherry picked from commit 91996cf2a2b2b7cd9b11c8f003830874deab67f9) --- logstash-core/lib/logstash/agent.rb | 4 +- .../inputs/metrics/stats_event_factory.rb | 8 ++- .../metrics/stats_event_factory_spec.rb | 64 ++++++++++++------- .../schemas/monitoring_document_schema.json | 2 +- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/logstash-core/lib/logstash/agent.rb b/logstash-core/lib/logstash/agent.rb index 7e05fba8d..c90a4d6d6 100644 --- a/logstash-core/lib/logstash/agent.rb +++ b/logstash-core/lib/logstash/agent.rb @@ -121,10 +121,10 @@ class LogStash::Agent transition_to_running - converge_state_and_update - start_webserver_if_enabled + converge_state_and_update + if auto_reload? # `sleep_then_run` instead of firing the interval right away Stud.interval(@reload_interval, :sleep_then_run => true) do diff --git a/x-pack/lib/monitoring/inputs/metrics/stats_event_factory.rb b/x-pack/lib/monitoring/inputs/metrics/stats_event_factory.rb index 17a7a3658..8c5177b80 100644 --- a/x-pack/lib/monitoring/inputs/metrics/stats_event_factory.rb +++ b/x-pack/lib/monitoring/inputs/metrics/stats_event_factory.rb @@ -12,6 +12,7 @@ module LogStash; module Inputs; class Metrics; @snapshot = snapshot @metric_store = @snapshot.metric_store @cluster_uuid = cluster_uuid + @webserver_enabled = LogStash::SETTINGS.get_value("http.enabled") end def make(agent, extended_performance_collection=true, collection_interval=10) @@ -124,8 +125,13 @@ module LogStash; module Inputs; class Metrics; end def fetch_node_stats(agent, stats) + if @webserver_enabled + http_addr = stats.get_shallow(:http_address).value + else + http_addr = nil + end @global_stats.merge({ - "http_address" => stats.get_shallow(:http_address).value, + "http_address" => http_addr, "ephemeral_id" => agent.ephemeral_id }) end diff --git a/x-pack/spec/monitoring/inputs/metrics/stats_event_factory_spec.rb b/x-pack/spec/monitoring/inputs/metrics/stats_event_factory_spec.rb index 905356fe8..79f69ac16 100644 --- a/x-pack/spec/monitoring/inputs/metrics/stats_event_factory_spec.rb +++ b/x-pack/spec/monitoring/inputs/metrics/stats_event_factory_spec.rb @@ -6,6 +6,37 @@ require "monitoring/inputs/metrics/stats_event_factory" require "logstash/config/pipeline_config" require 'json' +shared_examples_for("old model monitoring event with webserver setting") do + let(:schema_file) { File.join(schemas_path, "monitoring_document_schema.json") } + + it "should be valid" do + global_stats = {"uuid" => "00001" } + sut = described_class.new(global_stats, collector.snapshot_metric, nil) + LogStash::SETTINGS.set_value("monitoring.enabled", false) + LogStash::SETTINGS.set_value("http.enabled", webserver_enabled) + + monitoring_evt = sut.make(agent, true) + json = JSON.parse(monitoring_evt.to_json) + expect(JSON::Validator.fully_validate(schema_file, monitoring_evt.to_json)).to be_empty + end +end + +shared_examples_for("new model monitoring event with webserver setting") do + let(:schema_file) { File.join(schemas_path, "monitoring_document_new_schema.json") } + + it "should be valid" do + global_stats = {"uuid" => "00001" } + sut = described_class.new(global_stats, collector.snapshot_metric, "funky_cluster_uuid") + LogStash::SETTINGS.set_value("monitoring.enabled", true) + LogStash::SETTINGS.set_value("http.enabled", webserver_enabled) + + monitoring_evt = sut.make(agent, true) + json = JSON.parse(monitoring_evt.to_json) + expect(json['type']).to eq('logstash_stats') + expect(JSON::Validator.fully_validate(schema_file, monitoring_evt.to_json)).to be_empty + end +end + describe LogStash::Inputs::Metrics::StatsEventFactory do let(:schemas_path) { File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec", "monitoring", "schemas") } let(:queue) { Concurrent::Array.new } @@ -52,31 +83,20 @@ describe LogStash::Inputs::Metrics::StatsEventFactory do end context "new model" do - let(:schema_file) { File.join(schemas_path, "monitoring_document_new_schema.json") } - - it "should be valid" do - global_stats = {"uuid" => "00001" } - sut = described_class.new(global_stats, collector.snapshot_metric, "funky_cluster_uuid") - LogStash::SETTINGS.set_value("monitoring.enabled", true) - - monitoring_evt = sut.make(agent, true) - json = JSON.parse(monitoring_evt.to_json) - expect(json['type']).to eq('logstash_stats') - expect(JSON::Validator.fully_validate(schema_file, monitoring_evt.to_json)).to be_empty + it_behaves_like("new model monitoring event with webserver setting") do + let(:webserver_enabled) {false} + end + it_behaves_like("new model monitoring event with webserver setting") do + let(:webserver_enabled) {true} end end context "old model" do - let(:schema_file) { File.join(schemas_path, "monitoring_document_schema.json") } - - it "should be valid" do - global_stats = {"uuid" => "00001" } - sut = described_class.new(global_stats, collector.snapshot_metric, nil) - LogStash::SETTINGS.set_value("monitoring.enabled", false) - - monitoring_evt = sut.make(agent, true) - json = JSON.parse(monitoring_evt.to_json) - expect(JSON::Validator.fully_validate(schema_file, monitoring_evt.to_json)).to be_empty - end + it_behaves_like("old model monitoring event with webserver setting") do + let(:webserver_enabled) {false} + end + it_behaves_like("old model monitoring event with webserver setting") do + let(:webserver_enabled) {true} + end end end \ No newline at end of file diff --git a/x-pack/spec/monitoring/schemas/monitoring_document_schema.json b/x-pack/spec/monitoring/schemas/monitoring_document_schema.json index df7238b06..d0c31fd7b 100644 --- a/x-pack/spec/monitoring/schemas/monitoring_document_schema.json +++ b/x-pack/spec/monitoring/schemas/monitoring_document_schema.json @@ -71,7 +71,7 @@ "type": "object", "required": ["http_address", "uuid", "ephemeral_id"], "properties": { - "http_address": { "type": "string" }, + "http_address": { "type": ["string", "null"] }, "uuid": { "type": "string" }, "ephemeral_id": { "type": "string" } }