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

Set grpc logger to collector #4501

Merged
merged 11 commits into from
Dec 13, 2021
Prev Previous commit
Next Next commit
fixed comments
  • Loading branch information
pavankrish123 committed Dec 3, 2021
commit c44808eff4a567ef51076506030a92900b10b594
2 changes: 1 addition & 1 deletion service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
return fmt.Errorf("failed to get logger: %w", err)
}

telemetrylogs.NewColGrpcLogger(col.logger, col.cfgW.cfg.Service.Telemetry.Logs.Level).SetGrpcLogger()
telemetrylogs.NewColGRPCLogger(col.logger, col.cfgW.cfg.Service.Telemetry.Logs.Level).SetGRPCLogger()

col.logger.Info("Applying configuration...")

Expand Down
18 changes: 10 additions & 8 deletions service/internal/telemetrylogs/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ func NewLogger(cfg config.ServiceTelemetryLogs, options []zap.Option) (*zap.Logg
return logger, nil
}

// SettableGrpcLoggerV2 sets grpc framework's logger with internal logger.
type SettableGrpcLoggerV2 interface {
SetGrpcLogger()
// SettableGRPCLoggerV2 sets grpc framework's logger with internal logger.
type SettableGRPCLoggerV2 interface {
SetGRPCLogger()
}

type colGrpcLogger struct {
type colGRPCLogger struct {
setOnce sync.Once
loggerV2 grpclog.LoggerV2
}

// NewColGrpcLogger constructs a grpclog.LoggerV2 instance cloned from baseLogger with exact configuration.
// NewColGRPCLogger constructs a grpclog.LoggerV2 instance cloned from baseLogger with exact configuration.
// The minimum level of gRPC logs is set to WARN should the loglevel of the collector is set to INFO to avoid
// copious logging from grpc framework.
func NewColGrpcLogger(baseLogger *zap.Logger, loglevel zapcore.Level) SettableGrpcLoggerV2 {
func NewColGRPCLogger(baseLogger *zap.Logger, loglevel zapcore.Level) SettableGRPCLoggerV2 {
logger := baseLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
var c zapcore.Core
if loglevel == zap.InfoLevel {
Expand All @@ -76,12 +76,14 @@ func NewColGrpcLogger(baseLogger *zap.Logger, loglevel zapcore.Level) SettableGr
}
return c.With([]zapcore.Field{zap.Bool("grpc_log", true)})
}))
return &colGrpcLogger{
return &colGRPCLogger{
loggerV2: zapgrpc.NewLogger(logger),
}
}

func (gl *colGrpcLogger) SetGrpcLogger() {
// SetGRPCLogger needs to be run before any grpc calls and this implementation requires it to be run
// only once.
func (gl *colGRPCLogger) SetGRPCLogger() {
gl.setOnce.Do(func() {
grpclog.SetLoggerV2(gl.loggerV2)
})
Expand Down
4 changes: 2 additions & 2 deletions service/internal/telemetrylogs/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func TestGRPCLogger(t *testing.T) {
assert.NoError(t, err)

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

zapGRPCLogger, ok := glogger.(*colGrpcLogger)
zapGRPCLogger, ok := glogger.(*colGRPCLogger)
assert.True(t, ok)

zapGRPCLogger.loggerV2.Info(test.name)
Expand Down