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

[otelcol] Fix grpclogger to capture correct caller location #10773

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion otelcol/internal/grpclog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func SetLogger(baseLogger *zap.Logger, loglevel zapcore.Level) *zapgrpc.Logger {
c = core
}
return c.With([]zapcore.Field{zap.Bool("grpc_log", true)})
})))
}), zap.AddCallerSkip(5)))
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved

grpclog.SetLoggerV2(logger)
return logger
Expand Down
27 changes: 22 additions & 5 deletions otelcol/internal/grpclog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc/grpclog"
)

func TestGRPCLogger(t *testing.T) {
Expand Down Expand Up @@ -50,29 +51,45 @@ func TestGRPCLogger(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
obsInfo, obsWarn := false, false
callerInfo := ""
hook := zap.Hooks(func(entry zapcore.Entry) error {
switch entry.Level {
case zapcore.InfoLevel:
obsInfo = true
case zapcore.WarnLevel:
obsWarn = true
}
callerInfo = entry.Caller.String()
return nil
})

// create new collector zap logger
logger, err := test.cfg.Build(hook)
assert.NoError(t, err)

// create colGRPCLogger
// create GRPCLogger
glogger := SetLogger(logger, test.cfg.Level.Level())
assert.NotNil(t, glogger)

glogger.Info(test.name)
glogger.Warning(test.name)

// grpc does not usually call the logger directly, but through various wrappers that add extra depth
component := &mockComponent{logger: grpclog.Component("channelz")}
component.Info(test.name)
component.Warning(test.name)
assert.Equal(t, obsInfo, test.infoLogged)
assert.Equal(t, obsWarn, test.warnLogged)
// match the file name and line number of Warning() call above
assert.Contains(t, callerInfo, "internal/grpclog/logger_test.go:76")
})
}
}

type mockComponent struct {
logger grpclog.DepthLoggerV2
}

func (c *mockComponent) Info(args ...any) {
c.logger.Info(args...)
}

func (c *mockComponent) Warning(args ...any) {
c.logger.Warning(args...)
}
Loading