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

chore(log): Restructure and cleanup logging code #15234

Merged
merged 12 commits into from May 10, 2024

Conversation

srebhan
Copy link
Contributor

@srebhan srebhan commented Apr 25, 2024

Summary

This PR moves all logging code into the logger package and simplifies the code. It also exposes all required functions in the telegraf.Logger interface to provide an abstraction for changing the underlying logging implementation. This is in preparation to provide more fine-grained logging as requested in #6584 or adding structured logging as requested in #8815.

Checklist

  • No AI generated code was used in this PR

Related issues

resolves #10212

@telegraf-tiger
Copy link
Contributor

@srebhan srebhan self-assigned this Apr 26, 2024
@powersj
Copy link
Contributor

powersj commented Apr 26, 2024

Took an initial pass at this today:

  • Verified the --debug, --quiet, --once options print the same text as today
  • Verified startup negative cases, e.g. config file does not exist
  • inputs.kafka logging enabled/disabled
  • outputs.kafka logging enabled/disabled
  • Tested deprecation notice using inputs.net and ignore_protocol_stats config option
  • inputs.exec with debug enabled and disabled
  • Introduce a panic call in inputs.exec ensured the report the panic message still printed

TODO

  • goshim test
  • windows service

@powersj
Copy link
Contributor

powersj commented Apr 26, 2024

FYI I accidently hit submit on the previous comment too soon, but I have updated it.

For the goshim test, I grabed https://github.com/ssoroka/rand and added some logging:

diff --git plugins/inputs/rand/rand.go plugins/inputs/rand/rand.go
index b5f6847..b3cf571 100644
--- plugins/inputs/rand/rand.go
+++ plugins/inputs/rand/rand.go
@@ -54,6 +54,10 @@ func (r *RandomNumberGenerator) Description() string {
 }
 
 func (r *RandomNumberGenerator) Gather(a telegraf.Accumulator) error {
+       r.Log.Error("error message")
+       r.Log.Warn("warn message")
+       r.Log.Info("info message")
+       r.Log.Debug("debug message")
        r.sendMetric(a)
        return nil

Build the plugin and used this config:

[agent]
  debug = true
  omit_hostname = true

[[inputs.execd]]
  command = ["/home/powersj/rand/rand", "--config", "/home/powersj/rand/plugin.conf"]
  signal = "none"

[[outputs.file]]

Which produced:

2024-04-26T16:30:17Z I! [inputs.execd] Starting process: /home/powersj/rand/rand [--config /home/powersj/rand/plugin.conf]
2024-04-26T16:30:18Z E! [inputs.execd] stderr: "2024/04/26 10:30:18 E! error message"
2024-04-26T16:30:18Z E! [inputs.execd] stderr: "2024/04/26 10:30:18 W! warn message"
2024-04-26T16:30:18Z E! [inputs.execd] stderr: "2024/04/26 10:30:18 I! info message"
2024-04-26T16:30:18Z E! [inputs.execd] stderr: "2024/04/26 10:30:18 D! debug message"
...
random value=7i 1714149026350162901
2024-04-26T16:30:27Z D! [outputs.file]  Wrote batch of 9 metrics in 64.99µs
2024-04-26T16:30:27Z D! [outputs.file]  Buffer fullness: 0 / 10000 metrics

This is the same as what I see from master currently.

@powersj
Copy link
Contributor

powersj commented May 7, 2024

Built and installed it as a service and still looks good.

Thanks!

@powersj powersj added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label May 7, 2024
@powersj powersj assigned DStrand1 and unassigned srebhan May 7, 2024
@srebhan
Copy link
Contributor Author

srebhan commented May 8, 2024

@powersj can you confirm that the last log line is now present in the eventlog?

@powersj
Copy link
Contributor

powersj commented May 8, 2024

@powersj can you confirm that the last log line is now present in the eventlog?

I only saw the first message about loading the config file.

@srebhan srebhan removed the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label May 8, 2024
@srebhan srebhan self-assigned this May 8, 2024
@srebhan
Copy link
Contributor Author

srebhan commented May 8, 2024

@DStrand1 I remove the RFFR label for now until @powersj had a chance to test the "last line is in eventlog" thing... Will let you know once this is good to finally go.

@powersj
Copy link
Contributor

powersj commented May 8, 2024

Here is my updated config:

[agent]
    debug = true
    logtarget = "eventlog"
    
[[inputs.cpu]]
[[outputs.file]]

Then in an admin powershell:

> .\telegraf.exe --service install --config .\config.toml
> net stat telegraf
The Telegraf Data Collector Service service is starting.
The Telegraf Data Collector Service service was started successfully.

This is what I see in the Event Viewer:

image

I get the same with your branch or master.

Executable By Hand

If I run the executable by hand with that same config, now all the messages go to the event log:

image

And this is the final message:

image

Invalid Config

If I do the same with an invalid config, nothing new shows up in the event viewer and I get this message on the CLI:

PS C:\Users\Josh Powers\telegraf-srebhan> .\telegraf.exe --config .\config.toml
2024-05-08T20:47:04Z I! Loading config: .\config.toml
2024-05-08T20:47:04Z E! error loading config file .\config.toml: error parsing outpts, undefined but requested input: outpts

We never even get to the event viewer.

Error in output

If I update the config like that in #14144 with:

[agent]
    debug = true
    logtarget = "eventlog"
    
[[inputs.cpu]]
[[outputs.file]]
[[inputs.nvidia_smi]]
  bin_path = "C:\\Windows\\System32\\nvidia-smi.exe"

The last message is:

image

As opposed to the expected:

2024-05-08T20:50:08Z E! [telegraf] Error running agent: could not initialize input inputs.nvidia_smi: nvidia-smi not found in "C:\\Windows\\System32\\nvidia-smi.exe" and not in PATH; please make sure nvidia-smi is installed and/or is in PATH

fwiw I don't see the following debug message in the event viewer either:

2024-05-08T20:50:08Z D! [agent] Initializing plugins

Thoughts?

@srebhan
Copy link
Contributor Author

srebhan commented May 10, 2024

@powersj I will remove the reference to #14144 then and need to investigate the issue further. Are you still good with the PR otherwise?

@powersj
Copy link
Contributor

powersj commented May 10, 2024

Yes!

@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label May 10, 2024
@srebhan srebhan removed their assignment May 10, 2024
Copy link
Contributor

@DStrand1 DStrand1 left a comment

Choose a reason for hiding this comment

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

Looks great! Excited to see how this turns out

@DStrand1 DStrand1 merged commit 71b58dd into influxdata:master May 10, 2024
31 checks passed
@github-actions github-actions bot added this to the v1.30.3 milestone May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/agent chore ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adjust telegraf.Logger to be easy to use in whole codebase
3 participants