Skip to content

Commit

Permalink
remove options
Browse files Browse the repository at this point in the history
  • Loading branch information
bashar-515 committed Jan 8, 2025
1 parent 4c2e07b commit d08c8c8
Showing 1 changed file with 4 additions and 38 deletions.
42 changes: 4 additions & 38 deletions rpc/server_interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,13 @@ 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 {
o := evaluateServerOpt(opts)

func grpcUnaryServerInterceptor(logger utils.ZapCompatibleLogger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
startTime := time.Now()
newCtx := newLoggerForCall(ctx, logger, info.FullMethod, startTime)

resp, err := handler(newCtx, req)
if !o.shouldLog(info.FullMethod, err) {
if !grpc_logging.DefaultDeciderMethod(info.FullMethod, err) {
return resp, err
}

Expand All @@ -147,9 +145,7 @@ func grpcUnaryServerInterceptor(logger utils.ZapCompatibleLogger, opts ...grpcZa
}
}

func grpcStreamServerInterceptor(logger utils.ZapCompatibleLogger, opts ...grpcZapOption) grpc.StreamServerInterceptor {
o := evaluateServerOpt(opts)

func grpcStreamServerInterceptor(logger utils.ZapCompatibleLogger) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
startTime := time.Now()
newCtx := newLoggerForCall(stream.Context(), logger, info.FullMethod, startTime)
Expand All @@ -158,7 +154,7 @@ func grpcStreamServerInterceptor(logger utils.ZapCompatibleLogger, opts ...grpcZ
wrapped.WrappedContext = newCtx

err := handler(srv, wrapped)
if !o.shouldLog(info.FullMethod, err) {
if !grpc_logging.DefaultDeciderMethod(info.FullMethod, err) {
return err
}

Expand All @@ -170,36 +166,6 @@ func grpcStreamServerInterceptor(logger utils.ZapCompatibleLogger, opts ...grpcZ
}
}

type grpcZapOption func(*GrpcZapOptions)

type GrpcZapOptions struct {
levelFunc grpc_zap.CodeToLevel
shouldLog grpc_logging.Decider
codeFunc grpc_logging.ErrorToCode
durationFunc grpc_zap.DurationToField
messageFunc grpc_zap.MessageProducer
}

func evaluateServerOpt(opts []grpcZapOption) *GrpcZapOptions {
optCopy := &GrpcZapOptions{}
*optCopy = *defaultOptions
optCopy.levelFunc = grpc_zap.DefaultCodeToLevel
for _, o := range opts {
o(optCopy)
}
return optCopy
}

var (
defaultOptions = &GrpcZapOptions{
levelFunc: grpc_zap.DefaultCodeToLevel,
shouldLog: grpc_logging.DefaultDeciderMethod,
codeFunc: grpc_logging.DefaultErrorToCode,
durationFunc: grpc_zap.DefaultDurationToField,
messageFunc: grpc_zap.DefaultMessageProducer,
}
)

func newLoggerForCall(ctx context.Context, logger utils.ZapCompatibleLogger, fullMethodString string, start time.Time) context.Context {
var f []any
f = append(f, "grpc.start_time", start.Format(time.RFC3339))
Expand Down

0 comments on commit d08c8c8

Please sign in to comment.