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

RSDK-9150 - Client log interceptor should not use zap.Logger #375

Merged
merged 5 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/edaniels/golog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// Logger is used various parts of the package for informational/debugging purposes.
Expand All @@ -24,6 +25,7 @@ type ILogger interface {
// ZapCompatibleLogger is a basic logging interface for golog.Logger (alias for *zap.SugaredLogger) and RDK loggers.
type ZapCompatibleLogger interface {
Desugar() *zap.Logger
Level() zapcore.Level

Debug(args ...interface{})
Debugf(template string, args ...interface{})
Expand Down
9 changes: 9 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

"github.com/edaniels/golog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.viam.com/test"
)

Expand Down Expand Up @@ -64,6 +65,10 @@
func (m *InvalidLogger) Fatalw(msg string, keysAndValues ...interface{}) {
}

func (m *InvalidLogger) Level() zapcore.Level {
return zapcore.Level(zap.DebugLevel)

Check failure on line 69 in logger_test.go

View workflow job for this annotation

GitHub Actions / Build and Test

unnecessary conversion (unconvert)
}

// MockLogger fulfills the ZapCompatibleLogger interface by extending InvalidLogger with a Sublogger() method. This type
// is used to simulate calling utils.Sublogger() on an RDK logger.
type MockLogger struct {
Expand All @@ -79,6 +84,10 @@
m.Name = "WithFields called"
}

func (m *MockLogger) Level() zapcore.Level {
return zapcore.Level(zap.DebugLevel)

Check failure on line 88 in logger_test.go

View workflow job for this annotation

GitHub Actions / Build and Test

unnecessary conversion (unconvert)
}

func TestSubloggerWithZapLogger(t *testing.T) {
logger := golog.NewTestLogger(t)
sublogger := Sublogger(logger, "sub")
Expand Down
58 changes: 35 additions & 23 deletions rpc/wrtc_client_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/logging"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/pion/webrtc/v4"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -101,11 +100,10 @@
reply interface{},
opts ...grpc.CallOption,
) error {
fields := newClientLoggerFields(method)
startTime := time.Now()
err := ch.invokeWithInterceptor(ctx, method, args, reply, opts...)
newCtx := ctxzap.ToContext(ctx, ch.webrtcBaseChannel.logger.Desugar().With(fields...))
logFinalClientLine(newCtx, startTime, err, "finished client unary call")
loggerWithFields := utils.AddFieldsToLogger(ch.webrtcBaseChannel.logger, newClientLoggerFields(method)...)
logFinalClientLine(loggerWithFields, startTime, err, "finished client unary call")
return err
}

Expand Down Expand Up @@ -189,11 +187,10 @@
method string,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
fields := newClientLoggerFields(method)
startTime := time.Now()
clientStream, err := ch.streamWithInterceptor(ctx, method)
newCtx := ctxzap.ToContext(ctx, ch.webrtcBaseChannel.logger.Desugar().With(fields...))
logFinalClientLine(newCtx, startTime, err, "finished client streaming call")
loggerWithFields := utils.AddFieldsToLogger(ch.webrtcBaseChannel.logger, newClientLoggerFields(method)...)
logFinalClientLine(loggerWithFields, startTime, err, "finished client streaming call")
return clientStream, err
}

Expand Down Expand Up @@ -336,29 +333,44 @@

// taken from
// https://github.com/grpc-ecosystem/go-grpc-middleware/blob/560829fc74fcf9a69b7ab01d484f8b8961dc734b/logging/zap/client_interceptors.go

func logFinalClientLine(ctx context.Context, startTime time.Time, err error, msg string) {
func logFinalClientLine(logger utils.ZapCompatibleLogger, startTime time.Time, err error, msg string) {
code := grpc_logging.DefaultErrorToCode(err)
level := grpc_zap.DefaultCodeToLevel(code)

// this calculation is done because duration.Milliseconds() will return an integer, which is not precise enough.
duration := float32(time.Since(startTime).Nanoseconds()/1000) / 1000
fields := []any{}
if err == nil {
level = zap.DebugLevel
} else {
fields = append(fields, "error", err)
}
fields = append(fields, "grpc.code", code.String(), "grpc.time_ms", duration)

if logger.Level().Enabled(level) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? I would expect if the level were not enabled, then the calls to logger.Debuw/logger.Infow/etc... would early return. Are we not observing that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya this is probably unnecessary, let me check. I was thinking too much about replicating the functionality of grpc_zap.DefaultMessageProducer, which also does a check before logging

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, definitely speak up if removing this is problematic.

If removing is not problematic, then we can cascade and delete the interface change that added Level and the dummy implementations for the testing cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to remove

// cases for all outputs of grpc_zap.DefaultCodeToLevel
switch level {

Check failure on line 352 in rpc/wrtc_client_channel.go

View workflow job for this annotation

GitHub Actions / Build and Test

missing cases in switch of type zapcore.Level: zapcore.DPanicLevel, zapcore.PanicLevel, zapcore.FatalLevel, zapcore.InvalidLevel (exhaustive)
case zap.DebugLevel:
logger.Debugw(msg, fields...)
case zap.InfoLevel:
logger.Infow(msg, fields...)
case zap.ErrorLevel:
logger.Errorw(msg, fields...)
case zap.WarnLevel:
fallthrough
default:
logger.Warnw(msg, fields...)
}
Copy link
Member Author

@cheukt cheukt Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is pretty ugly, but the Write() methods differ between zap loggers and rdk loggers so I could either write another conversion method like AddFieldsToLogger or do this and this is easier

}
duration := grpc_zap.DefaultDurationToField(time.Since(startTime))
grpc_zap.DefaultMessageProducer(ctx, msg, level, code, err, duration)
}

var (
clientField = zap.String("span.kind", "client")
systemField = zap.String("system", "grpc")
)

func newClientLoggerFields(fullMethodString string) []zapcore.Field {
func newClientLoggerFields(fullMethodString string) []any {
service := path.Dir(fullMethodString)[1:]
method := path.Base(fullMethodString)
return []zapcore.Field{
systemField,
clientField,
zap.String("grpc.service", service),
zap.String("grpc.method", method),
return []any{
"span.kind", "client",
Copy link
Member Author

@cheukt cheukt Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: rdk loggers' WithFields() do not play nicely with zapcore.Field at all. which probably means that zap loggers will not play nicely with AddFieldsToLogger

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for filing that ticket; sounds like zap loggers will almost certainly not play nicely with AddFieldsToLogger. But, from the sample "after changes" output you put in the PR description, the added fields do seem to be getting added. Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, but the way they're getting passed in now is different - no longer a list of zap.Fields

"system", "grpc",
"grpc.service", service,
"grpc.method", method,
}
}
Loading