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

Refactor: make sure plugin logger sets context #13201

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

kares
Copy link
Contributor

@kares kares commented Sep 2, 2021

Improves logging experience for the user (commits extracted from #13038).

  • loosened logger(msg) contract allows for logging any (to_s) message not just strings - just like Log4j2 loggers do
  • an internal PluginLogger gets introduced to always have a plugin.id logging context
    (with logger.debug ... etc.)

Other changes:

  • fixed logger.trace? (previously delegated to logger.isDebugEnabled())

Breaking changes:

  • ~ low impact
class LogStash::Input::SomePlugin < ...

  def initialize(params)
    logger.info "this would no longer work - previously did but only if not using @logger.info ..."
    super(params)
    # logging after super works fine
  end
...
  • potential plugin test failures ... mocking such as the following would need updating*:
  expect(LogStash::Codecs::Multiline).to receive(:logger).and_return(Mlc::MultilineLogTracer.new).at_least(:once)
  • or keeping def class.logger(plugin = self) functional in a backwards compatible way

Why is it important/What is the impact to the user?

Logs are Logstash's primary way of communicating with the user, always having a plugin id logged greatly improves the user (debugging) experience.

Release notes

We improved logging from plugins to always include contextual information (plugin's id) in logs.

What does this PR do?

A new PluginLogger is introduced, which is now unique per plugin instance.

Why is it important/What is the impact to the user?

Users will have better context when going through logs - as they should see the plugin id (which might be user set) more often.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files (and/or docker env variables)
  • I have added tests that prove my fix is effective or that my feature works

Author's Checklist

  • codec ids (which we care about) use a different format than other plugins e.g.
    [DEBUG][logstash.codecs.line ][line_cec86de4-b9b7-44ce-95c4-63423b9f92c3]
    to resolve this we we need to do further changes, either:

How to test this PR locally

e.g. bin/logstash --log.level debug -e "" will do

Related issues

Logs

[2021-09-02T13:11:35,570][DEBUG][logstash.plugins.registry] On demand adding plugin to the registry {:name=>"stdin", :type=>"input", :class=>LogStash::Inputs::Stdin}
[2021-09-02T13:11:35,797][DEBUG][logstash.plugins.registry] On demand adding plugin to the registry {:name=>"line", :type=>"codec", :class=>LogStash::Codecs::Line}
[2021-09-02T13:11:36,116][DEBUG][logstash.instrument.periodicpoller.jvm] collector name {:name=>"ParNew"}
[2021-09-02T13:11:36,118][DEBUG][logstash.codecs.line     ][line_cec86de4-b9b7-44ce-95c4-63423b9f92c3] config LogStash::Codecs::Line/@id = "line_cec86de4-b9b7-44ce-95c4-63423b9f92c3"
[2021-09-02T13:11:36,120][DEBUG][logstash.instrument.periodicpoller.jvm] collector name {:name=>"ConcurrentMarkSweep"}
[2021-09-02T13:11:36,120][DEBUG][logstash.codecs.line     ][line_cec86de4-b9b7-44ce-95c4-63423b9f92c3] config LogStash::Codecs::Line/@enable_metric = true
[2021-09-02T13:11:36,120][DEBUG][logstash.codecs.line     ][line_cec86de4-b9b7-44ce-95c4-63423b9f92c3] config LogStash::Codecs::Line/@charset = "UTF-8"
[2021-09-02T13:11:36,120][DEBUG][logstash.codecs.line     ][line_cec86de4-b9b7-44ce-95c4-63423b9f92c3] config LogStash::Codecs::Line/@delimiter = "\n"
[2021-09-02T13:11:36,219][DEBUG][logstash.inputs.stdin    ][78126271ac5bff73f6bf0184af4762dae07fc37550e7518c65e53f4a5304e914] config LogStash::Inputs::Stdin/@type = "stdin"
[2021-09-02T13:11:36,220][DEBUG][logstash.inputs.stdin    ][78126271ac5bff73f6bf0184af4762dae07fc37550e7518c65e53f4a5304e914] config LogStash::Inputs::Stdin/@id = "78126271ac5bff73f6bf0184af4762dae07fc37550e7518c65e53f4a5304e914"
[2021-09-02T13:11:36,220][DEBUG][logstash.inputs.stdin    ][78126271ac5bff73f6bf0184af4762dae07fc37550e7518c65e53f4a5304e914] config LogStash::Inputs::Stdin/@enable_metric = true
[2021-09-02T13:11:36,234][DEBUG][logstash.inputs.stdin    ][78126271ac5bff73f6bf0184af4762dae07fc37550e7518c65e53f4a5304e914] config LogStash::Inputs::Stdin/@codec = <LogStash::Codecs::Line id=>"line_cec86de4-b9b7-44ce-95c4-63423b9f92c3", enable_metric=>true, charset=>"UTF-8", delimiter=>"\n">
[2021-09-02T13:11:36,236][DEBUG][logstash.inputs.stdin    ][78126271ac5bff73f6bf0184af4762dae07fc37550e7518c65e53f4a5304e914] config LogStash::Inputs::Stdin/@add_field = {}
[2021-09-02T13:11:36,303][DEBUG][logstash.plugins.registry] On demand adding plugin to the registry {:name=>"stdout", :type=>"output", :class=>LogStash::Outputs::Stdout}
[2021-09-02T13:11:36,326][DEBUG][logstash.plugins.registry] On demand adding plugin to the registry {:name=>"rubydebug", :type=>"codec", :class=>LogStash::Codecs::RubyDebug}
[2021-09-02T13:11:36,347][DEBUG][logstash.codecs.rubydebug][rubydebug_ca944dc6-b28f-4c71-a31f-b3d7ac7d5009] config LogStash::Codecs::RubyDebug/@id = "rubydebug_ca944dc6-b28f-4c71-a31f-b3d7ac7d5009"
[2021-09-02T13:11:36,347][DEBUG][logstash.codecs.rubydebug][rubydebug_ca944dc6-b28f-4c71-a31f-b3d7ac7d5009] config LogStash::Codecs::RubyDebug/@enable_metric = true
[2021-09-02T13:11:36,348][DEBUG][logstash.codecs.rubydebug][rubydebug_ca944dc6-b28f-4c71-a31f-b3d7ac7d5009] config LogStash::Codecs::RubyDebug/@metadata = false
[2021-09-02T13:11:39,524][DEBUG][logstash.outputs.stdout  ][d1bd143ca9c0117499c20d8bac3ca9cfc73bb5eee5fbc5f5246d5862d956a109] config LogStash::Outputs::Stdout/@codec = <LogStash::Codecs::RubyDebug id=>"rubydebug_ca944dc6-b28f-4c71-a31f-b3d7ac7d5009", enable_metric=>true, metadata=>false>
[2021-09-02T13:11:39,525][DEBUG][logstash.outputs.stdout  ][d1bd143ca9c0117499c20d8bac3ca9cfc73bb5eee5fbc5f5246d5862d956a109] config LogStash::Outputs::Stdout/@id = "d1bd143ca9c0117499c20d8bac3ca9cfc73bb5eee5fbc5f5246d5862d956a109"
[2021-09-02T13:11:39,525][DEBUG][logstash.outputs.stdout  ][d1bd143ca9c0117499c20d8bac3ca9cfc73bb5eee5fbc5f5246d5862d956a109] config LogStash::Outputs::Stdout/@enable_metric = true
[2021-09-02T13:11:39,526][DEBUG][logstash.outputs.stdout  ][d1bd143ca9c0117499c20d8bac3ca9cfc73bb5eee5fbc5f5246d5862d956a109] config LogStash::Outputs::Stdout/@workers = 1
[2021-09-02T13:11:39,643][DEBUG][logstash.javapipeline    ] Starting pipeline {:pipeline_id=>"main"}

Comment on lines +207 to +214
# TODO do we want to keep this around due plugin specs mocking logger through the class.logger call?!
#
# @override LogStash::Util::Loggable.logger
# @private
# def self.logger(plugin = nil)
# plugin.nil? ? super() : LogStash::Logging::PluginLogger.new(plugin)
# end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, some existing plugin specs assume SomePlugin.logger class method to return the logger used for the plugin instance. the class level call still works (instantiates a new logger) but since we want the logger instance to keep context it needs the plugin instance passed in (for correctness).
my approach would be to deal with the broken specs afterwards and not introduce hacks such as these ...

Suggested change
# TODO do we want to keep this around due plugin specs mocking logger through the class.logger call?!
#
# @override LogStash::Util::Loggable.logger
# @private
# def self.logger(plugin = nil)
# plugin.nil? ? super() : LogStash::Logging::PluginLogger.new(plugin)
# end

@kares kares requested a review from yaauie September 2, 2021 10:53
@kares kares removed the v7.16.0 label Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants