diff --git a/otelcol/internal/grpclog/logger.go b/otelcol/internal/grpclog/logger.go index 39b56f74890..8f48fe7207d 100644 --- a/otelcol/internal/grpclog/logger.go +++ b/otelcol/internal/grpclog/logger.go @@ -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))) grpclog.SetLoggerV2(logger) return logger diff --git a/otelcol/internal/grpclog/logger_test.go b/otelcol/internal/grpclog/logger_test.go index 58ef95331e5..75d39d95368 100644 --- a/otelcol/internal/grpclog/logger_test.go +++ b/otelcol/internal/grpclog/logger_test.go @@ -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) { @@ -50,6 +51,7 @@ 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: @@ -57,6 +59,7 @@ func TestGRPCLogger(t *testing.T) { case zapcore.WarnLevel: obsWarn = true } + callerInfo = entry.Caller.String() return nil }) @@ -64,15 +67,29 @@ func TestGRPCLogger(t *testing.T) { 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...) +}