Repository: etcd-io/etcd
File: server/etcdmain/grpc_proxy_logger.go (Go)

Code before:

const (
	responseCallType = "response"
	requestCallType  = "request"
)

func (r *proxylogger) PostCall(_ error, _ time.Duration) {
	// no op - no post-call payload logging
}

func (r *proxylogger) PostMsgReceive(payload any, err error, _ time.Duration) {
	callType := requestCallType
	f := logFieldsFromPayload(payload, err, callType)
	r.logger.Info(fmt.Sprintf("received payload logged as grpc.%s.content field", callType), slices.Concat(f, r.fields)...)
}

func (r *proxylogger) PostMsgSend(payload any, err error, _ time.Duration) {
	callType := responseCallType
	f := logFieldsFromPayload(payload, err, callType)
	r.logger.Info(fmt.Sprintf("returned response payload logged as grpc.%s.content field", callType), slices.Concat(f, r.fields)...)
}

func logFieldsFromPayload(payload any, err error, callType string) []zap.Field {
	fields := []zap.Field{}
	if err != nil {
		fields = append(fields, zap.NamedError(fmt.Sprintf("post.%s.msg.error", callType), err))
	}
	p, ok := payload.(proto.Message)
	if !ok {
		fields = append(fields, zap.NamedError("msg.type.error", fmt.Errorf("payload is not a github.com/golang/protobuf/proto message")))
	}
	msg, pErr := protoToJSON(p)
	if pErr != nil {
		fields = append(fields, zap.NamedError("msg.proto.error", fmt.Errorf("error when converting payload to logging json: %w", pErr)))
	}
	fields = append(fields, zap.String(fmt.Sprintf("grpc.%s.content", callType), msg))
	return fields
}

func protoToJSON(msg proto.Message) (string, error) {
	if reflect.ValueOf(msg).IsNil() {
		return "", nil
	}
	marshaler := jsonpb.Marshaler{}
	return marshaler.MarshalToString(msg)
}

func reportable(lg *zap.Logger) interceptors.CommonReportableFunc {
	return func(ctx context.Context, c interceptors.CallMeta) (interceptors.Reporter, context.Context) {
		fields := []zap.Field{
			zap.String("grpc.service", path.Dir(c.FullMethod())[1:]),

Review comment (suggestion):
```suggestion
		fields = append(fields, zap.NamedError(fmt.Sprintf("grpc.%s.error", callType), err))
```

Code after:

var _ interceptors.Reporter = (*grpcProxyLogger)(nil)

const (
	responseCallType = "response"
	requestCallType  = "request"
)

func (r *grpcProxyLogger) PostCall(_ error, _ time.Duration) {
	// no op - no post-call payload logging
}

func (r *grpcProxyLogger) PostMsgReceive(payload any, err error, _ time.Duration) {
	callType := requestCallType
	f := logFieldsFromPayload(payload, err, callType)
	r.logger.Info(fmt.Sprintf("received payload logged as grpc.%s.content field", callType), slices.Concat(f, r.fields)...)
}

func (r *grpcProxyLogger) PostMsgSend(payload any, err error, _ time.Duration) {
	callType := responseCallType
	f := logFieldsFromPayload(payload, err, callType)
	r.logger.Info(fmt.Sprintf("returned response payload logged as grpc.%s.content field", callType), slices.Concat(f, r.fields)...)
}

func logFieldsFromPayload(payload any, err error, callType string) []zap.Field {
	fields := []zap.Field{}
	if err != nil {
		fields = append(fields, zap.NamedError(fmt.Sprintf("grpc.%s.error", callType), err))
	}
	p, ok := payload.(proto.Message)
	if !ok {
		fields = append(fields, zap.NamedError("msg.type.error", fmt.Errorf("payload is not a github.com/golang/protobuf/proto message")))
	}
	msg, pErr := protoToJSON(p)
	if pErr != nil {
		fields = append(fields, zap.NamedError("msg.proto.error", fmt.Errorf("error when converting payload to logging json: %w", pErr)))
	}
	fields = append(fields, zap.String(fmt.Sprintf("grpc.%s.content", callType), msg))
	return fields
}

func protoToJSON(msg proto.Message) (string, error) {
	if reflect.ValueOf(msg).IsNil() {
		return "", nil
	}
	marshaler := jsonpb.Marshaler{}
	return marshaler.MarshalToString(msg)
}

func reportable(lg *zap.Logger) interceptors.CommonReportableFunc {
	return func(ctx context.Context, c interceptors.CallMeta) (interceptors.Reporter, context.Context) {