Skip to content

Commit 4634b49

Browse files
authored
feat: Support logging of tenant ID (#580)
Similar to cloudquery/cloudquery#21675, this adds support for logging tenant id at the plugin level.
1 parent 1f97843 commit 4634b49

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ 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+
delete(msg, field)
13+
}
914
switch level {
1015
case "trace":
1116
l.Trace().Fields(msg).Msg("")

managedplugin/plugin.go

Lines changed: 8 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"
@@ -557,6 +558,12 @@ func (c *Client) readLogLines(reader io.ReadCloser) {
557558
lr := NewLogReader(reader)
558559
// reset the context to avoid duplicate fields in the logs when streaming logs from plugins
559560
pluginsLogger := c.logger.With().Reset().Timestamp().Logger()
561+
protectedFields := make([]string, 0)
562+
tenantID := env.TenantID()
563+
if tenantID != "" {
564+
pluginsLogger = pluginsLogger.With().Str("tenant_id", tenantID).Logger()
565+
protectedFields = append(protectedFields, "tenant_id")
566+
}
560567
for {
561568
line, err := lr.NextLine()
562569
if errors.Is(err, io.EOF) {
@@ -574,7 +581,7 @@ func (c *Client) readLogLines(reader io.ReadCloser) {
574581
if err := json.Unmarshal(line, &structuredLogLine); err != nil {
575582
c.logger.Info().Str("level", "unknown").Msg(string(line))
576583
} else {
577-
c.jsonToLog(pluginsLogger, structuredLogLine)
584+
c.jsonToLog(pluginsLogger, structuredLogLine, protectedFields)
578585
}
579586
}
580587
}

0 commit comments

Comments
 (0)