3 回答
TA贡献1828条经验 获得超3个赞
查看此代码的启发示例,问题是否与没有“grpc_ctxtags”拦截器有关?见https://github.com/grpc-ecosystem/go-grpc-middleware/blob/06f64829ca1f521d41cd6235a7a204a6566fb0dc/logging/logrus/examples_test.go#L31
TA贡献1995条经验 获得超2个赞
因为这些变量为零,所以很恐慌
var (
logrusLogger *logrus.Logger
customFunc grpc_logrus.CodeToLevel
)
改为使用
var (
logrusLogger = logrus.New()
customFunc = func(code codes.Code) logrus.Level {
if code == codes.OK {
return logrus.InfoLevel
}
return logrus.ErrorLevel
}
)
注意:grpc_logrus.CodeToLevel具有基础类型func(code codes.Code) logrus.Level
TA贡献1891条经验 获得超3个赞
我面临着类似的问题:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x8e8ffb]
goroutine 38 [running]:
github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus.UnaryServerInterceptor.func1(0xc98ac0, 0xc00011e9c0, 0xafa620, 0xc00011e3f0, 0xc0001164c0, 0xc000116520, 0xc98ac0, 0xc00011e9c0, 0xc000120030, 0xc00011e990)
/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.2.2/logging/logrus/server_interceptors.go:37 +0x1bb
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1(0xc98ac0, 0xc00011e9c0, 0xafa620, 0xc00011e3f0, 0x17, 0xafa620, 0xc00011e3f0, 0x0)
/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.2.2/chain.go:25 +0x63
github.com/grpc-ecosystem/go-grpc-middleware/tags.UnaryServerInterceptor.func1(0xc98ac0, 0xc00011e3c0, 0xafa620, 0xc00011e3f0, 0xc0001164c0, 0xc000116540, 0x8dac2a, 0xb165e0, 0xc000116560, 0xc0001164c0)
/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.2.2/tags/interceptors.go:23 +0x86
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1(0xc98ac0, 0xc00011e3c0, 0xafa620, 0xc00011e3f0, 0xc000152100, 0x0, 0xc0001f3b30, 0x40f0a8)
/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.2.2/chain.go:25 +0x63
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1(0xc98ac0, 0xc00011e3c0, 0xafa620, 0xc00011e3f0, 0xc0001164c0, 0xc0001164e0, 0xc000505ba0, 0x493578, 0xb34560, 0xc00011e3c0)
/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.2.2/chain.go:34 +0xd5
/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1082 +0x50a
google.golang.org/grpc.(*Server).handleStream(0xc00031eea0, 0xc9ffa0, 0xc000102780, 0xc000152100, 0x0)
/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1405 +0xccd
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc000142020, 0xc00031eea0, 0xc9ffa0, 0xc000102780, 0xc000152100)
/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:746 +0xa1
created by google.golang.org/grpc.(*Server).serveStreams.func1
/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:744 +0xa1
导致问题的代码是(我导入logrus为log):
func InitgRPC(port int, appStruct *app.App) (*grpc.Server, net.Listener) {
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", fmt.Sprint(port)))
if err != nil {
log.Fatalf("Failed to listen on port: %s, error: %v", fmt.Sprint(port), err)
}
logrusEntry := log.NewEntry(logrusLogger)
opts := []grpc_logrus.Option{
grpc_logrus.WithLevels(customFunc),
}
grpc_logrus.ReplaceGrpcLogger(logrusEntry)
grpcServer := grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...),
grpc_auth.UnaryServerInterceptor(appStruct.AuthenticateUser),
grpc_recovery.UnaryServerInterceptor()),
grpc_middleware.WithStreamServerChain(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.StreamServerInterceptor(logrusEntry, opts...),
grpc_auth.StreamServerInterceptor(appStruct.AuthenticateUser),
grpc_recovery.StreamServerInterceptor(),
),
)
app.RegisterServer(grpcServer, appStruct)
return grpcServer, lis
}
grpc_ctxtags所以这个问题与尼克波科克所说的不包括拦截器无关。
- 3 回答
- 0 关注
- 234 浏览
添加回答
举报