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 91996cf2a2)
This commit is contained in:
andsel 2021-01-14 10:54:50 +01:00 committed by J.A.R.V.I.S. - an Elastic git bot
parent 4e82485d39
commit 275abab171
4 changed files with 52 additions and 26 deletions

View file

@ -121,10 +121,10 @@ class LogStash::Agent
transition_to_running transition_to_running
converge_state_and_update
start_webserver_if_enabled start_webserver_if_enabled
converge_state_and_update
if auto_reload? if auto_reload?
# `sleep_then_run` instead of firing the interval right away # `sleep_then_run` instead of firing the interval right away
Stud.interval(@reload_interval, :sleep_then_run => true) do Stud.interval(@reload_interval, :sleep_then_run => true) do

View file

@ -12,6 +12,7 @@ module LogStash; module Inputs; class Metrics;
@snapshot = snapshot @snapshot = snapshot
@metric_store = @snapshot.metric_store @metric_store = @snapshot.metric_store
@cluster_uuid = cluster_uuid @cluster_uuid = cluster_uuid
@webserver_enabled = LogStash::SETTINGS.get_value("http.enabled")
end end
def make(agent, extended_performance_collection=true, collection_interval=10) def make(agent, extended_performance_collection=true, collection_interval=10)
@ -124,8 +125,13 @@ module LogStash; module Inputs; class Metrics;
end end
def fetch_node_stats(agent, stats) 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({ @global_stats.merge({
"http_address" => stats.get_shallow(:http_address).value, "http_address" => http_addr,
"ephemeral_id" => agent.ephemeral_id "ephemeral_id" => agent.ephemeral_id
}) })
end end

View file

@ -6,6 +6,37 @@ require "monitoring/inputs/metrics/stats_event_factory"
require "logstash/config/pipeline_config" require "logstash/config/pipeline_config"
require 'json' 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 describe LogStash::Inputs::Metrics::StatsEventFactory do
let(:schemas_path) { File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec", "monitoring", "schemas") } let(:schemas_path) { File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec", "monitoring", "schemas") }
let(:queue) { Concurrent::Array.new } let(:queue) { Concurrent::Array.new }
@ -52,31 +83,20 @@ describe LogStash::Inputs::Metrics::StatsEventFactory do
end end
context "new model" do context "new model" do
let(:schema_file) { File.join(schemas_path, "monitoring_document_new_schema.json") } it_behaves_like("new model monitoring event with webserver setting") do
let(:webserver_enabled) {false}
it "should be valid" do end
global_stats = {"uuid" => "00001" } it_behaves_like("new model monitoring event with webserver setting") do
sut = described_class.new(global_stats, collector.snapshot_metric, "funky_cluster_uuid") let(:webserver_enabled) {true}
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
end end
end end
context "old model" do context "old model" do
let(:schema_file) { File.join(schemas_path, "monitoring_document_schema.json") } it_behaves_like("old model monitoring event with webserver setting") do
let(:webserver_enabled) {false}
it "should be valid" do end
global_stats = {"uuid" => "00001" } it_behaves_like("old model monitoring event with webserver setting") do
sut = described_class.new(global_stats, collector.snapshot_metric, nil) let(:webserver_enabled) {true}
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 end
end end
end end

View file

@ -71,7 +71,7 @@
"type": "object", "type": "object",
"required": ["http_address", "uuid", "ephemeral_id"], "required": ["http_address", "uuid", "ephemeral_id"],
"properties": { "properties": {
"http_address": { "type": "string" }, "http_address": { "type": ["string", "null"] },
"uuid": { "type": "string" }, "uuid": { "type": "string" },
"ephemeral_id": { "type": "string" } "ephemeral_id": { "type": "string" }
} }