Skip to content

Commit 1648f90

Browse files
committed
Support logging of tenant id
1 parent 5995f00 commit 1648f90

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

internal/env/env.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package env
2+
3+
import "os"
4+
5+
func TenantID() string {
6+
return os.Getenv("CQ_TENANT_ID")
7+
}

managedplugin/logging.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ package managedplugin
22

33
import "github.com/rs/zerolog"
44

5-
func (c *Client) jsonToLog(l zerolog.Logger, msg map[string]any) {
5+
func (c *Client) jsonToLog(l zerolog.Logger, msg map[string]any, protectedFields []string) {
66
level := msg["level"]
77
// The log level is part of the log message received from the plugin, so we need to remove it before logging
88
delete(msg, "level")
9+
10+
// Remove protected fields from log message to avoid duplication
11+
for _, field := range protectedFields {
12+
if _, found := msg[field]; found {
13+
delete(msg, field)
14+
}
15+
}
916
switch level {
1017
case "trace":
1118
l.Trace().Fields(msg).Msg("")

managedplugin/plugin.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"time"
1818

1919
"github.com/avast/retry-go/v4"
20+
"github.com/cloudquery/plugin-pb-go/internal/env"
2021
pbBase "github.com/cloudquery/plugin-pb-go/pb/base/v0"
2122
pbDiscovery "github.com/cloudquery/plugin-pb-go/pb/discovery/v0"
2223
pbDiscoveryV1 "github.com/cloudquery/plugin-pb-go/pb/discovery/v1"
@@ -554,6 +555,10 @@ func (c *Client) readLogLines(reader io.ReadCloser) {
554555
lr := NewLogReader(reader)
555556
// reset the context to avoid duplicate fields in the logs when streaming logs from plugins
556557
pluginsLogger := c.logger.With().Reset().Timestamp().Logger()
558+
tenantID := env.TenantID()
559+
if tenantID != "" {
560+
pluginsLogger = pluginsLogger.With().Str("tenant_id", tenantID).Logger()
561+
}
557562
for {
558563
line, err := lr.NextLine()
559564
if errors.Is(err, io.EOF) {
@@ -571,7 +576,7 @@ func (c *Client) readLogLines(reader io.ReadCloser) {
571576
if err := json.Unmarshal(line, &structuredLogLine); err != nil {
572577
c.logger.Info().Str("level", "unknown").Msg(string(line))
573578
} else {
574-
c.jsonToLog(pluginsLogger, structuredLogLine)
579+
c.jsonToLog(pluginsLogger, structuredLogLine, []string{"tenant_id"})
575580
}
576581
}
577582
}

0 commit comments

Comments
 (0)