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-9149] Use RDK Logger Across Interceptors #400

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

bashar-515
Copy link

@bashar-515 bashar-515 commented Jan 8, 2025

Stop converting the logger into a zap logger and instead use the native zap compatible logger. See pics below for visible change (the error log in between the highlighted lines is the same as what's referenced in the ticket).

Note
re: all the 'from here' comments..
To no longer use an ordinary zap logger, I replaced calls to grpc interceptor methods with calls to copies of those methods that I modify to use an RDK logger instead. An effect of this change was the additional copying over of all private types employed by these functions in the grpc library we use,, is there a better way about this? It seems like this PR used a similar tactic to get client interceptors to use the preferred logger.

Before:
Screenshot 2025-01-08 at 11 01 25 AM

After: notice here that the logger (from goutils/logger.go)is outputting logs that are not ERROR level or higher. This is not desired behavior. This has since been fixed; the original behavior has been restored.
Screenshot 2025-01-08 at 11 03 04 AM

After:
Screenshot 2025-01-09 at 1 22 26 PM

Testing
Modify the the module.go RDK file in the simple module example custom resource so that the DoCommand() implementation always returns an error. Then, follow the instructions provided in the custom resources README.md file to see the logs output by the robot server.

@@ -327,10 +324,6 @@ func NewServer(logger utils.ZapCompatibleLogger, opts ...ServerOption) (Server,
logger: logger,
}

grpcLogger := logger.Desugar()
if !(sOpts.debug || utils.Debug) {
grpcLogger = grpcLogger.WithOptions(zap.IncreaseLevel(zap.LevelEnablerFunc(zapcore.ErrorLevel.Enabled)))
Copy link
Author

Choose a reason for hiding this comment

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

[qu] is removing this safe? Not sure if we lose out on key functionality here by not setting these options. I'm assuming that these options are replaced by the cases outlined in LogFinalLine().

Copy link
Member

Choose a reason for hiding this comment

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

the idea here is that if debug mode is not on, we only want to see error level logs and up. We should port that functionality to the interceptor themselves (see if you're seeing info level logs with this set of changes)

@@ -119,3 +126,117 @@ func remoteSpanContextFromContext(ctx context.Context) (trace.SpanContext, error

return trace.SpanContext{TraceID: traceID, SpanID: spanID, TraceOptions: traceOptions, Tracestate: nil}, nil
}

func grpcUnaryServerInterceptor(logger utils.ZapCompatibleLogger, opts ...grpcZapOption) grpc.UnaryServerInterceptor {
Copy link
Author

Choose a reason for hiding this comment

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

from here

Copy link
Member

Choose a reason for hiding this comment

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

we don't pass in any grpcZapOption, so happy to just remove all of this private stuff

}
}

func grpcStreamServerInterceptor(logger utils.ZapCompatibleLogger, opts ...grpcZapOption) grpc.StreamServerInterceptor {
Copy link
Author

Choose a reason for hiding this comment

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

from here.

}
}

type grpcZapOption func(*GrpcZapOptions)
Copy link
Author

Choose a reason for hiding this comment

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

from here


type grpcZapOption func(*GrpcZapOptions)

type GrpcZapOptions struct {
Copy link
Author

Choose a reason for hiding this comment

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

from here

messageFunc grpc_zap.MessageProducer
}

func evaluateServerOpt(opts []grpcZapOption) *GrpcZapOptions {
Copy link
Author

Choose a reason for hiding this comment

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

from here

return optCopy
}

var (
Copy link
Author

Choose a reason for hiding this comment

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

from here

logger.go Outdated
Copy link
Author

Choose a reason for hiding this comment

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

The changes in this file are just part of a refactor. LogFinalLine() used to live in rpc/wrtc_client_channel.go.

Copy link
Author

Choose a reason for hiding this comment

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

Just refactoring. See this comment.

@bashar-515 bashar-515 marked this pull request as ready for review January 8, 2025 17:25
@bashar-515 bashar-515 requested a review from cheukt January 8, 2025 17:25
Copy link
Member

@cheukt cheukt left a comment

Choose a reason for hiding this comment

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

I like where this is going, but definitely not worried about taking out more of the code if we aren't currently exercising

@@ -119,3 +126,117 @@ func remoteSpanContextFromContext(ctx context.Context) (trace.SpanContext, error

return trace.SpanContext{TraceID: traceID, SpanID: spanID, TraceOptions: traceOptions, Tracestate: nil}, nil
}

func grpcUnaryServerInterceptor(logger utils.ZapCompatibleLogger, opts ...grpcZapOption) grpc.UnaryServerInterceptor {
Copy link
Member

Choose a reason for hiding this comment

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

we don't pass in any grpcZapOption, so happy to just remove all of this private stuff

@@ -327,10 +324,6 @@ func NewServer(logger utils.ZapCompatibleLogger, opts ...ServerOption) (Server,
logger: logger,
}

grpcLogger := logger.Desugar()
if !(sOpts.debug || utils.Debug) {
grpcLogger = grpcLogger.WithOptions(zap.IncreaseLevel(zap.LevelEnablerFunc(zapcore.ErrorLevel.Enabled)))
Copy link
Member

Choose a reason for hiding this comment

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

the idea here is that if debug mode is not on, we only want to see error level logs and up. We should port that functionality to the interceptor themselves (see if you're seeing info level logs with this set of changes)

@@ -38,7 +45,7 @@ func UnaryServerTracingInterceptor(logger *zap.Logger) grpc.UnaryServerIntercept
}

// StreamServerTracingInterceptor starts a new Span if Span metadata exists in the context.
func StreamServerTracingInterceptor(logger *zap.Logger) grpc.StreamServerInterceptor {
func StreamServerTracingInterceptor(logger utils.ZapCompatibleLogger) grpc.StreamServerInterceptor {
Copy link
Member

Choose a reason for hiding this comment

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

we don't even use the logger here, do we?

Copy link
Author

Choose a reason for hiding this comment

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

Nope lol you're right good catch

newCtx := newLoggerForCall(ctx, logger, info.FullMethod, startTime)

resp, err := handler(newCtx, req)
if !o.shouldLog(info.FullMethod, err) {
Copy link
Member

Choose a reason for hiding this comment

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

check the current path, I think we just always log (DefaultDecider)

Comment on lines 155 to 158
newCtx := newLoggerForCall(stream.Context(), logger, info.FullMethod, startTime)
wrapped := grpc_middleware.WrapServerStream(stream)

wrapped.WrappedContext = newCtx
Copy link
Member

Choose a reason for hiding this comment

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

should be fine to get rid of, we don't have a convention of passing loggers through the context. (can also check rdk for usages)

Copy link
Author

Choose a reason for hiding this comment

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

I think I understand what you mean here. I went ahead and took out lines 155 and 158 as visible in the code block above (the lines wrapped.WrappedContext = newCtx and newCtx := newLoggerForCall(stream.Context(), logger, info.FullMethod, startTime)) from grpcStreamServerInterceptor(). grpcUnaryServerInterceptor() also uses newLoggerForCall() to wrap a Logger with a Context, but the result is directly passed to the handler function, so I don't think I can remove anything in that case.

@cheukt cheukt added the safe to test Mark as safe to test label Jan 8, 2025
@viambot viambot removed the safe to test Mark as safe to test label Jan 9, 2025
Copy link

github-actions bot commented Jan 9, 2025

For security reasons, this PR must be labeled with safe to test in order for tests to run.

@bashar-515 bashar-515 requested a review from cheukt January 9, 2025 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants