Skip to content

Commit 6da4aec

Browse files
authored
Improve log messages for data stream support checks (#1109)
This commit changed the data-stream log messages providing more context on configuration issues and incompatible settings. The check_data_stream_config! method was refactored to reduce its cyclomatic complexity. It also changed the data_stream_default log levels from debug to info as those messages are helpful in understanding the auto-configuration mechanism and troubleshooting possibly incompatible values. Prior to this commit, a few data stream logs were being printed twice due to a race condition on the data_stream_config? method.
1 parent 89a0fe9 commit 6da4aec

File tree

5 files changed

+76
-52
lines changed

5 files changed

+76
-52
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 11.12.3
2+
- Changed the log messages for data stream checks [#1109](https://github.com/logstash-plugins/logstash-output-elasticsearch/pull/1109)
3+
- Added more details about incompatible data streams supplied configurations
4+
- Changed the data stream auto-configuration log levels from `debug` to `info`
5+
16
## 11.12.2
27
- [Doc] Fixes the broken apache http client link [#1101](https://github.com/logstash-plugins/logstash-output-elasticsearch/pull/1101)
38

lib/logstash/outputs/elasticsearch.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ def register
300300
# to build_client down to the Pool class.
301301
@client = build_client(LicenseChecker.new(@logger))
302302

303+
# Avoids race conditions in the @data_stream_config initialization (invoking check_data_stream_config! twice).
304+
# It's being concurrently invoked by this register method and by the finish_register on the @after_successful_connection_thread
305+
data_stream_enabled = data_stream_config?
306+
303307
@after_successful_connection_thread = after_successful_connection do
304308
begin
305309
finish_register
@@ -324,7 +328,7 @@ def register
324328
raise LogStash::ConfigurationError, "DLQ feature (dlq_custom_codes) is configured while DLQ is not enabled" unless dlq_custom_codes.empty?
325329
end
326330

327-
if data_stream_config?
331+
if data_stream_enabled
328332
@event_mapper = -> (e) { data_stream_event_action_tuple(e) }
329333
@event_target = -> (e) { data_stream_name(e) }
330334
@index = "#{data_stream_type}-#{data_stream_dataset}-#{data_stream_namespace}".freeze # default name

lib/logstash/outputs/elasticsearch/data_stream_support.rb

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def self.included(base)
2828
base.extend(Validator)
2929
end
3030

31-
# @note assumes to be running AFTER {after_successful_connection} completed, due ES version checks
3231
def data_stream_config?
3332
@data_stream_config.nil? ? @data_stream_config = check_data_stream_config! : @data_stream_config
3433
end
@@ -45,53 +44,64 @@ def data_stream_name(event)
4544
"#{type}-#{dataset}-#{namespace}"
4645
end
4746

48-
DATA_STREAMS_REQUIRES_ECS_LS_VERSION = '8.0.0'
47+
DATA_STREAMS_AND_ECS_ENABLED_BY_DEFAULT_LS_VERSION = '8.0.0'
4948

5049
# @param params the user configuration for the ES output
5150
# @note LS initialized configuration (with filled defaults) won't detect as data-stream
5251
# compatible, only explicit (`original_params`) config should be tested.
53-
# @return [TrueClass|FalseClass] whether given configuration is data-stream compatible
52+
# @return [Boolean] whether given configuration is data-stream compatible
5453
def check_data_stream_config!(params = original_params)
55-
data_stream_params = params.select { |name, _| name.start_with?('data_stream_') } # exclude data_stream =>
56-
invalid_data_stream_params = invalid_data_stream_params(params)
57-
5854
case data_stream_explicit_value
5955
when false
60-
if data_stream_params.any?
61-
@logger.error "Ambiguous configuration; data stream settings must not be present when data streams is disabled (caused by: `data_stream => false`)", data_stream_params
62-
raise LogStash::ConfigurationError, "Ambiguous configuration, please remove data stream specific settings: #{data_stream_params.keys}"
63-
end
56+
check_disabled_data_stream_config!(params)
6457
return false
6558
when true
66-
if invalid_data_stream_params.any?
67-
@logger.error "Invalid data stream configuration, following parameters are not supported:", invalid_data_stream_params
68-
raise LogStash::ConfigurationError, "Invalid data stream configuration: #{invalid_data_stream_params.keys}"
69-
end
70-
if ecs_compatibility == :disabled
71-
if ::Gem::Version.create(LOGSTASH_VERSION) < ::Gem::Version.create(DATA_STREAMS_REQUIRES_ECS_LS_VERSION)
72-
@deprecation_logger.deprecated "In a future release of Logstash, the Elasticsearch output plugin's `data_stream => true` will require the plugin to be run in ECS compatibility mode. " + ENABLING_ECS_GUIDANCE
73-
else
74-
@logger.error "Invalid data stream configuration; `ecs_compatibility` must not be `disabled`. " + ENABLING_ECS_GUIDANCE
75-
raise LogStash::ConfigurationError, "Invalid data stream configuration: `ecs_compatibility => disabled`"
76-
end
77-
end
59+
check_enabled_data_stream_config!(params)
7860
return true
79-
else
80-
use_data_stream = data_stream_default(data_stream_params, invalid_data_stream_params)
81-
if use_data_stream
82-
@logger.info("Config is compliant with data streams. `data_stream => auto` resolved to `true`")
83-
elsif data_stream_params.any?
84-
# DS (auto) disabled but there's still some data-stream parameters (and no `data_stream => false`)
85-
@logger.error "Ambiguous configuration; data stream settings are present, but data streams are not enabled", data_stream_params
86-
raise LogStash::ConfigurationError, "Ambiguous configuration, please set data_stream => true " +
87-
"or remove data stream specific settings: #{data_stream_params.keys}"
88-
else
89-
@logger.info("Config is not compliant with data streams. `data_stream => auto` resolved to `false`")
61+
else # data_stream => auto or not set
62+
use_data_stream = data_stream_default(params)
63+
64+
check_disabled_data_stream_config!(params) unless use_data_stream
65+
66+
@logger.info("Data streams auto configuration (`data_stream => auto` or unset) resolved to `#{use_data_stream}`")
67+
return use_data_stream
68+
end
69+
end
70+
71+
def check_enabled_data_stream_config!(params)
72+
invalid_data_stream_params = invalid_data_stream_params(params)
73+
74+
if invalid_data_stream_params.any?
75+
@logger.error "Invalid data stream configuration, the following parameters are not supported:", invalid_data_stream_params
76+
raise LogStash::ConfigurationError, "Invalid data stream configuration: #{invalid_data_stream_params.keys}"
77+
end
78+
79+
if ecs_compatibility == :disabled
80+
if ecs_compatibility_required?
81+
@logger.error "Invalid data stream configuration; `ecs_compatibility` must not be `disabled`. " + ENABLING_ECS_GUIDANCE
82+
raise LogStash::ConfigurationError, "Invalid data stream configuration: `ecs_compatibility => disabled`"
9083
end
91-
use_data_stream
84+
85+
@deprecation_logger.deprecated "In a future release of Logstash, the Elasticsearch output plugin's `data_stream => true` will require the plugin to be run in ECS compatibility mode. " + ENABLING_ECS_GUIDANCE
9286
end
9387
end
9488

89+
def check_disabled_data_stream_config!(params)
90+
data_stream_params = data_stream_params(params)
91+
92+
if data_stream_params.any?
93+
@logger.error "Ambiguous configuration; data stream settings must not be present when data streams are disabled (caused by `data_stream => false`, `data_stream => auto` or unset resolved to false). " \
94+
"You can either manually set `data_stream => true` or remove the following specific data stream settings: ", data_stream_params
95+
96+
raise LogStash::ConfigurationError,
97+
"Ambiguous configuration; data stream settings must not be present when data streams are disabled: #{data_stream_params.keys}"
98+
end
99+
end
100+
101+
def data_stream_params(params)
102+
params.select { |name, _| name.start_with?('data_stream_') }
103+
end
104+
95105
def data_stream_explicit_value
96106
case @data_stream
97107
when 'true'
@@ -131,6 +141,7 @@ def inherited_internal_config_param?(name)
131141

132142
DATA_STREAMS_ORIGIN_ES_VERSION = '7.9.0'
133143

144+
# @note assumes to be running AFTER {after_successful_connection} completed, due ES version checks
134145
# @return [Gem::Version] if ES supports DS nil (or raise) otherwise
135146
def assert_es_version_supports_data_streams
136147
fail 'no last_es_version' unless last_es_version # assert - should not happen
@@ -144,38 +155,41 @@ def assert_es_version_supports_data_streams
144155
es_version # return truthy
145156
end
146157

147-
DATA_STREAMS_ENABLED_BY_DEFAULT_LS_VERSION = '8.0.0'
148-
149158
# when data_stream => is either 'auto' or not set
150-
# @param data_stream_params [#any?]
151-
# @param invalid_data_stream_config [#any?#inspect]
152-
def data_stream_default(data_stream_params, invalid_data_stream_config)
159+
def data_stream_default(params)
153160
if ecs_compatibility == :disabled
154-
@logger.debug("Not eligible for data streams because ecs_compatibility is not enabled. " + ENABLING_ECS_GUIDANCE)
161+
@logger.info("Not eligible for data streams because ecs_compatibility is not enabled. " + ENABLING_ECS_GUIDANCE)
155162
return false
156163
end
157164

158-
ds_default = ::Gem::Version.create(LOGSTASH_VERSION) >= ::Gem::Version.create(DATA_STREAMS_ENABLED_BY_DEFAULT_LS_VERSION)
165+
invalid_data_stream_params = invalid_data_stream_params(params)
159166

160-
if ds_default # LS 8.0
161-
if invalid_data_stream_config.any?
162-
@logger.debug("Not eligible for data streams because config contains one or more settings that are not compatible with data streams: #{invalid_data_stream_config.inspect}")
167+
if data_stream_and_ecs_enabled_by_default?
168+
if invalid_data_stream_params.any?
169+
@logger.info("Not eligible for data streams because config contains one or more settings that are not compatible with data streams: #{invalid_data_stream_params.inspect}")
163170
return false
164171
end
165172

166-
@logger.debug 'Configuration is data stream compliant'
167173
return true
168174
end
169175

170176
# LS 7.x
171-
if !invalid_data_stream_config.any? && !data_stream_params.any?
177+
if !invalid_data_stream_params.any? && !data_stream_params(params).any?
172178
@logger.warn "Configuration is data stream compliant but due backwards compatibility Logstash 7.x will not assume " +
173179
"writing to a data-stream, default behavior will change on Logstash 8.0 " +
174180
"(set `data_stream => true/false` to disable this warning)"
175181
end
176182
false
177183
end
178184

185+
def ecs_compatibility_required?
186+
data_stream_and_ecs_enabled_by_default?
187+
end
188+
189+
def data_stream_and_ecs_enabled_by_default?
190+
::Gem::Version.create(LOGSTASH_VERSION) >= ::Gem::Version.create(DATA_STREAMS_AND_ECS_ENABLED_BY_DEFAULT_LS_VERSION)
191+
end
192+
179193
# an {event_action_tuple} replacement when a data-stream configuration is detected
180194
def data_stream_event_action_tuple(event)
181195
event_data = event.to_hash

logstash-output-elasticsearch.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-output-elasticsearch'
3-
s.version = '11.12.2'
3+
s.version = '11.12.3'
44
s.licenses = ['apache-2.0']
55
s.summary = "Stores logs in Elasticsearch"
66
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"

spec/unit/outputs/elasticsearch/data_stream_support_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
context "on LS #{ls_version_desc}" do
8585
around(:each) { |example| change_constant(:LOGSTASH_VERSION, ls_version, &example) }
8686
it "does not use data-streams" do
87-
expect( subject.logger ).to receive(:debug).with(a_string_including "ecs_compatibility is not enabled")
87+
expect( subject.logger ).to receive(:info).with(a_string_including "ecs_compatibility is not enabled")
88+
expect( subject.logger ).to receive(:info).with(a_string_including "Data streams auto configuration (`data_stream => auto` or unset) resolved to `false`")
8889
expect( subject.data_stream_config? ).to be false
8990
end
9091
end
@@ -161,7 +162,7 @@
161162

162163
it "does not default to data-streams" do
163164
expect( subject.logger ).to receive(:error) do |msg|
164-
expect(msg).to include "Ambiguous configuration; data stream settings are present, but data streams are not enabled"
165+
expect(msg).to include "Ambiguous configuration; data stream settings must not be present when data streams are disabled"
165166
end
166167
change_constant :LOGSTASH_VERSION, '7.10.2' do
167168
expect { subject.data_stream_config? }.to raise_error(LogStash::ConfigurationError, /Ambiguous configuration/i)
@@ -173,8 +174,8 @@
173174
let(:options) { super().merge('data_stream' => 'false') }
174175

175176
it "raises a configuration error (due ds specific settings)" do
176-
expect( subject.logger ).to receive(:error).with(/Ambiguous configuration; data stream settings must not be present when data streams is disabled/,
177-
{"data_stream_auto_routing"=>"false", "data_stream_dataset"=>"test"})
177+
expect( subject.logger ).to receive(:error).with(/Ambiguous configuration; data stream settings must not be present when data streams are disabled/,
178+
{"data_stream_auto_routing"=>"false", "data_stream_dataset"=>"test"})
178179
change_constant :LOGSTASH_VERSION, '7.10.2' do
179180
expect { subject.data_stream_config? }.to raise_error(LogStash::ConfigurationError, /Ambiguous configuration/i)
180181
end

0 commit comments

Comments
 (0)