Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally add monitoring structure to top-level of output in API #11106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions logstash-core/lib/logstash/api/commands/default_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,31 @@ module Api
module Commands
class DefaultMetadata < Commands::Base
def all
{:host => host,
:version => version,
:http_address => http_address,
:id => service.agent.id,
:name => service.agent.name,
:ephemeral_id => service.agent.ephemeral_id,
:status => "green", # This is hard-coded to mirror x-pack behavior
:snapshot => ::BUILD_INFO["build_snapshot"],
:pipeline => {
:workers => LogStash::SETTINGS.get("pipeline.workers"),
:batch_size => LogStash::SETTINGS.get("pipeline.batch.size"),
:batch_delay => LogStash::SETTINGS.get("pipeline.batch.delay"),
}
}
res = {:host => host,
:version => version,
:http_address => http_address,
:id => service.agent.id,
:name => service.agent.name,
:ephemeral_id => service.agent.ephemeral_id,
:status => "green", # This is hard-coded to mirror x-pack behavior
:snapshot => ::BUILD_INFO["build_snapshot"],
:pipeline => {
:workers => LogStash::SETTINGS.get("pipeline.workers"),
:batch_size => LogStash::SETTINGS.get("pipeline.batch.size"),
:batch_delay => LogStash::SETTINGS.get("pipeline.batch.delay"),
},
}
monitoring = {}
if enabled_xpack_monitoring?
monitoring = monitoring.merge({
:hosts => LogStash::SETTINGS.get("xpack.monitoring.elasticsearch.hosts"),
:username => LogStash::SETTINGS.get("xpack.monitoring.elasticsearch.username")
})
end
if LogStash::SETTINGS.set?("monitoring.cluster_uuid")
monitoring = monitoring.merge({:cluster_uuid => LogStash::SETTINGS.get("monitoring.cluster_uuid")})
end
res.merge(monitoring.empty? ? {} : {:monitoring => monitoring})
end

def host
Expand All @@ -36,6 +47,12 @@ def http_address
rescue ::LogStash::Instrument::MetricStore::MetricNotFound, NoMethodError => e
nil
end

private
def enabled_xpack_monitoring?
LogStash::SETTINGS.registered?("xpack.monitoring.enabled") &&
LogStash::SETTINGS.get("xpack.monitoring.enabled")
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion logstash-core/lib/logstash/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ module Environment
Setting::TimeValue.new("slowlog.threshold.debug", "-1"),
Setting::TimeValue.new("slowlog.threshold.trace", "-1"),
Setting::String.new("keystore.classname", "org.logstash.secret.store.backend.JavaKeyStore"),
Setting::String.new("keystore.file", ::File.join(::File.join(LogStash::Environment::LOGSTASH_HOME, "config"), "logstash.keystore"), false) # will be populated on
Setting::String.new("keystore.file", ::File.join(::File.join(LogStash::Environment::LOGSTASH_HOME, "config"), "logstash.keystore"), false), # will be populated on
Setting::NullableString.new("monitoring.cluster_uuid")
# post_process
].each {|setting| SETTINGS.register(setting) }

Expand Down
38 changes: 38 additions & 0 deletions logstash-core/spec/logstash/api/commands/default_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
describe LogStash::Api::Commands::DefaultMetadata do
include_context "api setup"

def registerIfNot(setting)
LogStash::SETTINGS.register(setting) unless LogStash::SETTINGS.registered?(setting.name)
end

let(:report_method) { :all }
subject(:report) do
factory = ::LogStash::Api::CommandFactory.new(LogStash::Api::Service.new(@agent))
Expand All @@ -12,9 +16,43 @@

let(:report_class) { described_class }

before :all do
registerIfNot(LogStash::Setting::Boolean.new("xpack.monitoring.enabled", false))
registerIfNot(LogStash::Setting::ArrayCoercible.new("xpack.monitoring.elasticsearch.hosts", String, [ "http://localhost:9200" ] ))
registerIfNot(LogStash::Setting::NullableString.new("xpack.monitoring.elasticsearch.username", "logstash_TEST system"))
registerIfNot(LogStash::Setting::NullableString.new("xpack.monitoring.elasticsearch.username", "logstash_TEST system"))
end

after :each do
LogStash::SETTINGS.set_value("xpack.monitoring.enabled", false)
end

describe "#plugins_stats_report" do
let(:report_method) { :all }

# Enforce just the structure
it "check monitoring exist when cluster_uuid has been defined" do
LogStash::SETTINGS.set_value("monitoring.cluster_uuid", "cracking_cluster")
expect(report.keys).to include(
:monitoring
)
end

it "check monitoring exist when monitoring is enabled" do
LogStash::SETTINGS.set_value("xpack.monitoring.enabled", true)
expect(report.keys).to include(
:monitoring
)
end

it "check monitoring does not appear when not enabled and nor cluster_uuid is defined" do
LogStash::SETTINGS.set_value("xpack.monitoring.enabled", false)
LogStash::SETTINGS.get_setting("monitoring.cluster_uuid").reset
expect(report.keys).not_to include(
:monitoring
)
end

it "check keys" do
expect(report.keys).to include(
:host,
Expand Down