Skip to content

Commit

Permalink
fix: Reset logger context when streaming logs from plugins (#421)
Browse files Browse the repository at this point in the history
#### Summary

Fixes cloudquery/cloudquery-issues#2624 (internal issue)

We were using the logger created here https://github.com/cloudquery/cloudquery/blob/69143a73499660db2ab714781a5d207884fb9303/cli/cmd/logging.go#L51 to output logs messages from plugins. Plugins already set their log context in https://github.com/cloudquery/plugin-sdk/blob/8b9e067af19604d90d2e77d6e1aff1d93adad752/plugin/plugin.go#L213 and https://github.com/cloudquery/plugin-sdk/blob/8b9e067af19604d90d2e77d6e1aff1d93adad752/plugin/plugin.go#L241, so when we stream them to the `cloudquery.log` file we should use a new logger with an empty context.

---
  • Loading branch information
erezrokah authored Oct 22, 2024
1 parent 413fe0f commit 608e08a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 1 addition & 2 deletions managedplugin/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import "github.com/rs/zerolog"

func (c *Client) jsonToLog(l zerolog.Logger, msg map[string]any) {
level := msg["level"]
// Delete fields already added by the CLI so that they don't appear twice in the logs when we stream it from plugins
// The log level is part of the log message received from the plugin, so we need to remove it before logging
delete(msg, "level")
delete(msg, "invocation_id")
switch level {
case "trace":
l.Trace().Fields(msg).Msg("")
Expand Down
4 changes: 3 additions & 1 deletion managedplugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ func (c *Client) getPluginArgs() []string {
func (c *Client) readLogLines(reader io.ReadCloser) {
defer c.wg.Done()
lr := NewLogReader(reader)
// reset the context to avoid duplicate fields in the logs when streaming logs from plugins
pluginsLogger := c.logger.With().Reset().Timestamp().Logger()
for {
line, err := lr.NextLine()
if errors.Is(err, io.EOF) {
Expand All @@ -565,7 +567,7 @@ func (c *Client) readLogLines(reader io.ReadCloser) {
if err := json.Unmarshal(line, &structuredLogLine); err != nil {
c.logger.Info().Str("level", "unknown").Msg(string(line))
} else {
c.jsonToLog(c.logger, structuredLogLine)
c.jsonToLog(pluginsLogger, structuredLogLine)
}
}
}
Expand Down

0 comments on commit 608e08a

Please sign in to comment.