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

Make config values more strictly and enable to set nil or default #2685

Merged
merged 22 commits into from
Dec 12, 2019

Conversation

ashie
Copy link
Member

@ashie ashie commented Nov 6, 2019

Which issue(s) this PR fixes:
Fixes #2616

What this PR does / why we need it:
Add a new command line option --strict-config-value to parse integer, float and bool values in a config file more strictly. A system config item strict_config_value is also added.
Since non-numerical (or unrecognized values for bool) values for them in config values are implicitly parsed as 0 , sometimes it makes troubleshooting hard. This option will raise an error for such values to clarify such issues in a config file.

In addition, this PR also adds helper methods to set nil or default value to parameters in config files, since many users expect nil for "#{ENV['SOME_ENV_VAR']}" with empty SOME_ENV_VAR environment variable (but it's converted to an empty string in actual).

some_param "#{ENV['SOME_ENV_VAR'] || use_nil}"
some_param "#{ENV['SOME_ENV_VAR'] || use_default}"

Docs Changes:

  • Add --strict-config-value to Command Line Options in Deployment - System Configuration.
  • Add strict_config_value to system config in Configuration - Config File Syntax and Deployment - System Configuration.
  • Add note for use_nil and use_default to Embedding Ruby Expressions in Config File Syntax.

Release Note:
Same with the title.

@ashie
Copy link
Member Author

ashie commented Nov 6, 2019

This PR adds a new option :strict to config_param to confirm the
concept. Now I've confirmed that it can address referred issues such as
#2607, #2616 and #2444 when I merge this option to all plugins by
default (it's not commited yet). But I think it's not a thing you want.
A global option is the required one, isn't it?

My first question is "where should I add the global option"?

  1. environment variable
  2. command line option
  3. system config

I think the command line option is the desired place.
What do you think about it?

The second question is: "The :strict option for config_param is usable?"
If it's not, I don't expose it to plugins and just switch the behavior
internally. If it's usable, I'll continue to implement this feature
(more tests are needed).

I have some more questions. I'll post it later.

@cosmo0920
Copy link
Contributor

cosmo0920 commented Nov 6, 2019

My first question is "where should I add the global option"?

  1. environment variable
  2. command line option
  3. system config

IMO, 3 would be ideal. 2 would be better.

The second question is: "The :strict option for config_param is usable?"

Looks workable in my environment:

diff --git a/lib/fluent/plugin/in_udp.rb b/lib/fluent/plugin/in_udp.rb
index c2d43611..ac3d0bc3 100644
--- a/lib/fluent/plugin/in_udp.rb
+++ b/lib/fluent/plugin/in_udp.rb
@@ -25,7 +25,7 @@ module Fluent::Plugin
     desc 'Tag of output events.'
     config_param :tag, :string
     desc 'The port to listen to.'
-    config_param :port, :integer, default: 5160
+    config_param :port, :integer, default: 5160, strict: true
     desc 'The bind address to listen to.'
     config_param :bind, :string, default: '0.0.0.0'
<source>
  @type udp
  format none
  bind 0.0.0.0
  port invalid
  body_size_limit 4KB
  source_host_key "host"
  tag test
</source>

<match test>
  @type stdout
</match>
2019-11-06 17:40:11 +0900 [info]: parsing config file is succeeded path="example/in_udp.conf"
2019-11-06 17:40:11 +0900 [info]: gem 'fluentd' version '1.8.0.rc2'
2019-11-06 17:40:11 +0900 [info]: gem 'fluent-plugin-elasticsearch' version '3.5.5'
2019-11-06 17:40:11 +0900 [info]: gem 'fluent-plugin-kinesis' version '3.1.0'
2019-11-06 17:40:11 +0900 [info]: gem 'fluent-plugin-prometheus' version '1.6.0'
bundler: failed to load command: fluentd (/Users/cosmo/GitHub/fluentd/vendor/bundle/ruby/2.6.0/bin/fluentd)
ArgumentError: invalid value for Integer(): "invalid"

And it would be preferable just adding strict checking.

lib/fluent/config/types.rb Outdated Show resolved Hide resolved
lib/fluent/config/types.rb Outdated Show resolved Hide resolved
test/config/test_types.rb Show resolved Hide resolved
test/config/test_types.rb Show resolved Hide resolved
@cosmo0920
Copy link
Contributor

I've commented the my first impression.
Please take a look, @ashie.

@repeatedly
Copy link
Member

I think :strict option in config_param is not good because it is unclear behaviour for users.
So command line option and system configuration are better.

@ashie ashie force-pushed the strict-config-value branch 8 times, most recently from 684f9d3 to dfff109 Compare November 14, 2019 04:04
@ashie ashie changed the title Make config values more strictive Make config values more strictly Nov 25, 2019
@ashie ashie changed the title Make config values more strictly Make config values more strictly and enable to set nil or default Nov 25, 2019
@ashie ashie marked this pull request as ready for review November 25, 2019 08:59
lib/fluent/config/error.rb Outdated Show resolved Hide resolved
lib/fluent/config/error.rb Outdated Show resolved Hide resolved
lib/fluent/config/literal_parser.rb Outdated Show resolved Hide resolved
lib/fluent/config/literal_parser.rb Outdated Show resolved Hide resolved
lib/fluent/config/section.rb Outdated Show resolved Hide resolved
test/config/test_types.rb Outdated Show resolved Hide resolved
test/config/test_types.rb Outdated Show resolved Hide resolved
lib/fluent/config/types.rb Outdated Show resolved Hide resolved
lib/fluent/config/literal_parser.rb Show resolved Hide resolved
lib/fluent/config/types.rb Outdated Show resolved Hide resolved
@ashie ashie force-pushed the strict-config-value branch 2 times, most recently from e07c550 to 23761f0 Compare December 3, 2019 02:37
@ashie
Copy link
Member Author

ashie commented Dec 3, 2019

Oops, sorry I missed some comments.
I'll address them.

lib/fluent/config/element.rb Outdated Show resolved Hide resolved
lib/fluent/config/section.rb Outdated Show resolved Hide resolved
lib/fluent/config/element.rb Outdated Show resolved Hide resolved
We should ensure to put only 1 assertion in 1 test because an failed
assertion stops to continue the test, remaining assertions in it
aren't evaluated. It's not convenient on modifying existing features.

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Add a new config option :strict to Config::INTERGER_VALUE and
Config::FLOAT_VALUE. Config.size_value and Config.time_value also
support it.

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Example config:

  <system>
    strict_config_value true
  </system>

When this option is set to true, ambiguous values for config parameters
aren't accepted. For example, an empty string or not a number like "abc"
aren't accepted as an integer or a float, an exception will be raised
instead.

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Should raise ConfigError on unrecognized strings or any other value
types to notify mistakes in user's config as early as possible.

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Example setting:

  username: "#{ENV['USERNAME'] || raise(SetNil)}"

Although many users expect nil for "#{ENV{'FOOBAR']}" with empty FOOBAR
environemnt variable, it's converted to an empty string "" since it's
Ruby's specification. Instead we are going to add a helper method for it.
The helper method will be added in a later commit.

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Example setting:

  username: "#{ENV['USERNAME'] || raise(SetDefault)}"

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Example settings:

  username: "#{ENV['USERNAME'] || use_nil}"

  or

  username: "#{ENV['USERNAME'] || use_default}"

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
The new argument `strict_config_value` should be added to the last of
arguments of the method.

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Default config values should be taken from ConfigProxy::defaults.
Otherwise a default value overwritten by config_set_default isn't
taken correctly.

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Signed-off-by: Takuro Ashie <ashie@clear-code.com>
Since the first argument inherits Hash class, it's confusing.

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
@repeatedly
Copy link
Member

Looks good for me. Does anyone have other concerns?

@ashie
Copy link
Member Author

ashie commented Dec 11, 2019

Thanks for your review!
It's completed, I have no additional patch for this PR.
I'll add description for this feature to fluent-docs if there is no objection to its specificaion.

@cosmo0920
Copy link
Contributor

cosmo0920 commented Dec 11, 2019

Looks good for me, too. I have no concern for these patches.

@repeatedly repeatedly merged commit 340d1aa into fluent:master Dec 12, 2019
@repeatedly
Copy link
Member

Merged. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to make config value more stricitive
4 participants