Logstash: plugins class equivalence problem, surfaces in fix_streaming_codec

Created on 13 Sep 2019  路  7Comments  路  Source: elastic/logstash

There is a problem testing the plugins class equivalence. It surfaces in the fix_streaming_codec where the codec instance class does not match the case statement which checks for the LogStash::Codecs::Plain and LogStash::Codecs::JSON classes per https://github.com/elastic/logstash/blob/3d2daca5d839ed99ab0727f7c29df2e3ce6f9155/logstash-core/lib/logstash/inputs/base.rb#L135-L140

For example

bin/logstash -e 'input{tcp{port => 12345 codec => json}} output{stdout{codec => rubydebug}}'

Does not switch to the json_lines codec as it should do per fix_streaming_codec

https://github.com/logstash-plugins/logstash-input-tcp/blob/f372baecd5b778a949e16d407196630236ebb92b/lib/logstash/inputs/tcp.rb#L138

bug

All 7 comments

This problem started in 7.2.0.

I found the root cause; starting in 7.2.0 codecs are wrapped in a delegator https://github.com/elastic/logstash/blob/v7.2.0/logstash-core/lib/logstash/config/mixin.rb#L414 https://github.com/elastic/logstash/blob/v7.2.0/logstash-core/lib/logstash/codecs/delegator.rb

and this delegator does not redefine the === operator which is used by the case/when statement on classes.

Fixing the delegator class to be transparent WRT === to work with the above case/when when testing on codec classes is a bit tricky, I suggest to first push a simple fix on the fix_streaming_codec method to not test on classes, and look into the joys of Ruby metaprogramming and try to fix the delegator class in a second phase.

interesting, never realized Delegator does not delegate === (while it does == and eql? etc)
... guess a 3rd (although quite ugly) option would be to do manual unwrapping (__getobj__) ?
since there must be a good enough reason for missing === :hushed:

@kares yeah ... I tried a bit to hack my way into supporting === into our delegator but failed :P - while exploring Delegator/SimpleDelegator classes in Ruby I saw that there is also DelegateClass which might be better in that respect. That or we just write our own wrapper.

One observations is that this problem really threw me off in trying to figure why two seemingly identical classes where not equivalent, it took me while to figure the delegator... :O metaprogramming is powerful but side effects are hard to predict and debugging can be fun.

11401 was merged and will be included in 7.5.1 and resolved the specific fix_streaming_codec issue here so will be closing this issue. I will create a followup issue for the Delegator class.

followup issue #11434

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bobbyhubbard picture bobbyhubbard  路  3Comments

JPvRiel picture JPvRiel  路  3Comments

dedemorton picture dedemorton  路  3Comments

dorj1234 picture dorj1234  路  3Comments

ashangit picture ashangit  路  4Comments