-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Config File Syntax: Extend Embedded Ruby Code support for Hashes and Arrays #4580
Config File Syntax: Extend Embedded Ruby Code support for Hashes and Arrays #4580
Conversation
@ashie please check this whenever you are free |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR!
As I commented on the original issue, we need to consider the specifications in LiteralParser
carefully.
I'd like to think about it some more, but at the moment it doesn't seem like a good design decision to give types.rb
the same functionality as LiteralParser
.
I would like to hear from other maintainers.
We need to consider this as an issue of Embedded Ruby Code. Using environment variables is just one way of it.
Embedded Ruby Code
feature is implemented inLiteralParser
:fluentd/lib/fluent/config/literal_parser.rb
Lines 183 to 199 in e763c07
def eval_embedded_code(code) if @eval_context.nil? parse_error! "embedded code is not allowed in this file" end # Add hostname and worker_id to code for preventing unused warnings code = <<EOM + code hostname = Socket.gethostname worker_id = ENV['SERVERENGINE_WORKER_ID'] || '' EOM begin @eval_context.instance_eval(code) rescue SetNil nil rescue SetDefault :default end end fluentd/lib/fluent/config/literal_parser.rb
Lines 108 to 110 in e763c07
elsif skip(/\#\{/) string << eval_embedded_code(scan_embedded_code) skip(/\}/) Since it is for
double_quoted_string
, we need to use double quotes for the entire value to use this feature.It may be possible to address this as a
types
problem, as in #4580, but the specifications inLiteralParser
need to be carefully considered.
I don't yet see the detail but I feel same smell. |
@daipom do you suggest that we should handle this scenario in |
Yes. We should start by considering whether there are points in |
Oh ok |
@Athishpranav2003 Thanks so much! |
8e43b54
to
4ed590c
Compare
@daipom ignore the prev comments |
@daipom not sure why 2 workflows alone failed, could you check that part alone? |
No problem! They have nothing to do with this PR! |
Could you add unit test for this? |
@ashie added UTs for the same |
Thanks! The current specification is:
As far as seeing changes in the code, embedded codes will always be supported, right?
We need to consider whether the new specifications are acceptable while taking into account these concerns. |
Strings stay the same - only double quoted strings support string interpolation For Hashes and Arrays - We do string interpolation for anything that has Tho we have one benefit that the docs present for now we didn't restrict the interpolation to strings so it wont go against the specifications from the past |
This is the difficult part. |
OK. I think this direction is possible for us!
but I think these points are less important for this feature. We need to hear from other maintainers on this point. |
Fine |
The focus is on what extent we care about the possibility that existing users are using settings like: <match test>
@type http
endpoint foo
headers {"foo": "foo#{bar"}
</match> When |
I'm not sure but it seems that such workaround isn't available for Array & Hash types. diff --git a/test/config/test_config_parser.rb b/test/config/test_config_parser.rb
index fe29028e..b0c18fc9 100644
--- a/test/config/test_config_parser.rb
+++ b/test/config/test_config_parser.rb
@@ -483,8 +483,8 @@ module Fluent::Config
param_size 4k
param_bool true
param_time 10m
- param_hash { "key1": "value1", "key2": 2 }
- param_array ["value1", "value2", 100]
+ param_hash '{ "key1": "value1", "key2": 2 }'
+ param_array '["value1", "value2", 100]'
param_regexp /pattern/
])
target = AllTypes.new.configure(conf) |
Sorry, please ignore my comment. |
In general, we intend to keep compatibility so that users can continue to use same major version without changing settings. When we have to change behaviour in any reason, it would be better to show announcements or warnings before making the change, and wait a few minor versions to make the change in actual. On the other hand, using |
It would be better adding a test for this. |
BTW my current impression is very positive for including this change in the next minor release (with addressing some corner cases as @daipom mentioned). |
Signed-off-by: Athish Pranav D <[email protected]>
@ashie i tried your tests |
Signed-off-by: Athish Pranav D <[email protected]>
ea69709
to
af2b11d
Compare
Not sure |
Thanks! I'm checking the actual behavior. |
Array behaviorApplied to only explicit arrays, except for single quoted arrays. Config: <source>
@type sample
tag test1
sample {"k1":"v1","k2":"v2","k3":"v3"}
</source>
<source>
@type sample
tag test2
sample {"k1":"v1","k2":"v2","k3":"v3"}
</source>
<source>
@type sample
tag test3
sample {"k1":"v1","k2":"v2","k3":"v3"}
</source>
# Not applied to shorthand array
<filter test1>
@type record_transformer
remove_keys k1, "#{ENV['FOO']}"
</filter>
# Applied to array
<filter test2>
@type record_transformer
remove_keys ["k1", "#{ENV['FOO']}"]
</filter>
# Not applied to single quoted array
<filter test3>
@type record_transformer
remove_keys '["k1", "#{ENV[\'FOO\']}"]'
</filter>
<match test*>
@type stdout
</match> Parsed result ( <ROOT>
<source>
@type sample
tag "test1"
sample {"k1":"v1","k2":"v2","k3":"v3"}
</source>
<source>
@type sample
tag "test2"
sample {"k1":"v1","k2":"v2","k3":"v3"}
</source>
<source>
@type sample
tag "test3"
sample {"k1":"v1","k2":"v2","k3":"v3"}
</source>
<filter test1>
@type record_transformer
remove_keys k1, "#{ENV['FOO']}"
</filter>
<filter test2>
@type record_transformer
remove_keys ["k1","k2"]
</filter>
<filter test3>
@type record_transformer
remove_keys ["k1", "#{ENV['FOO']}"]
</filter>
<match test*>
@type stdout
</match>
</ROOT> Output:
|
Hash behaviorApplied to only explicit hashes, except for single quoted hashes. It should be noted that it can also be applied to non-string values of JSON. Config: # Not Applied to shorthand hash
<match test>
@type http
endpoint foo
headers key:#{ENV['FOO']}
</match>
# Applied to String value of hash
<match test>
@type http
endpoint foo
headers {"key":"#{ENV['FOO']}"}
</match>
# Applied to non-String value of hash
<match test>
@type http
endpoint foo
headers {"key":#{ENV['FOO']}}
</match>
# Not Applied to single quoted hash
<match test>
@type http
endpoint foo
headers '{"key":"#{ENV[\'FOO\']}"}'
</match> Parsed result ( <ROOT>
<match test>
@type http
endpoint "foo"
headers key:#{ENV['FOO']}
</match>
<match test>
@type http
endpoint "foo"
headers {"key":"0"}
</match>
<match test>
@type http
endpoint "foo"
headers {"key":0}
</match>
<match test>
@type http
endpoint "foo"
headers {"key":"#{ENV['FOO']}"}
</match>
</ROOT> Note
<match test>
@type http
endpoint foo
headers {"key":#{worker_id}}
</match>
|
Considering this point, it is inaccurate to describe this PR's specification as
We should describe this specification as
|
@daipom i will change the subject of PR as you mentioned. |
Thanks!!
I think this PR does not need to care about the |
@Athishpranav2003 Thanks! This looks good to me! |
Peace :). |
Signed-off-by: Athish Pranav D <[email protected]>
be5708b
to
ee84bef
Compare
@ashie i have added the changes you suggested. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Waiting to open merge window for v1.18.0.
Now, it is opened. |
I have updated the description. |
TODO document. |
Yay, it's a really good enhancement! 👍 👍 |
Which issue(s) this PR fixes:
Fixes #4385
What this PR does / why we need it:
Support Embedded Ruby Code for Hashes and Arrays.
This is not backward compatible.
We can disable this feature by surrounding the entire value with single quotes.
Note: this feature is for JSON literals
This feature is for literals that using
[]
or{}
.literal
andvalue
of the Fluentd Config Syntax.literal
by parsing the config file.literal
is String or JSON.value
by parsing theliteral
.value
has various types.value
.For example, we can specify Array/Hash
value
by using Stringliteral
We don't necessarily need to use JSON
literal
to specify Array/Hashvalue
.The following formats works from previous versions, and there is no specification change at all.
Note: support only String of JSON
For example, this does not support Number of JSON.
It is because JSON
literal
supports multiple lines and comments.#
not in String is considered the start of a comment line.Docs Changes:
TODO
Release Note:
key {"foo":"#{1 + 1}"} => key {"foo":"2"}
key '{"foo":"#{1 + 1}"}' => key {"foo":"#{1 + 1}"}