为了账号安全,请及时绑定邮箱和手机立即绑定

将日志记录中间件添加到 grpc

将日志记录中间件添加到 grpc

Go
幕布斯6054654 2022-06-01 11:20:20
我正在尝试将一些日志中间件添加到 grpc 服务器,我正在按照他们的 github 上的简单示例进行操作。https://github.com/grpc-ecosystem/go-grpc-middleware/blob/master/logging/logrus/examples_test.go我这样设置我的服务器选项:var (  logrusLogger *logrus.Logger  customFunc   grpc_logrus.CodeToLevel)func main() {    logrusEntry := logrus.NewEntry(logrusLogger)    lorgusOpts := []grpc_logrus.Option{        grpc_logrus.WithLevels(customFunc),    }    grpc_logrus.ReplaceGrpcLogger(logrusEntry)    opt := []grpc.ServerOption{        grpc.Creds(credentials.NewTLS(tlsConfig)),        grpc_middleware.WithUnaryServerChain(            grpc_auth.UnaryServerInterceptor(auther.Auth),            grpc_logrus.UnaryServerInterceptor(logrusEntry, lorgusOpts...),        ),    }    s := grpc.NewServer(opt...)    if err := s.Serve(lis); err != nil {        log.Fatalf("err %+v", err)    }}但是,每当我查看日志时,我都会得到一个 nil 指针。panic: runtime error: invalid memory address or nil pointer dereference[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x89edde]goroutine 23 [running]:github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus.UnaryServerInterceptor.func1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0xc00018caa0, 0xc00018cac0, 0xb2e640, 0xc00026c390, 0x0, 0x0)    /go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.2.0/logging/logrus/server_interceptors.go:37 +0x1begithub.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0x0, 0x0, 0x0, 0xc0000adfc0)    /go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.2.0/chain.go:25 +0x63github.com/grpc-ecosystem/go-grpc-middleware/auth.UnaryServerInterceptor.func1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0xc00018caa0, 0xc00018cae0, 0x89ceda, 0x9f24e0, 0xc00018cb00, 0xc00018caa0)
查看完整描述

3 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

查看此代码的启发示例,问题是否与没有“grpc_ctxtags”拦截器有关?见https://github.com/grpc-ecosystem/go-grpc-middleware/blob/06f64829ca1f521d41cd6235a7a204a6566fb0dc/logging/logrus/examples_test.go#L31

查看完整回答
反对 回复 2022-06-01
?
拉风的咖菲猫

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


查看完整回答
反对 回复 2022-06-01
?
万千封印

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所以这个问题与尼克波科克所说的不包括拦截器无关。


查看完整回答
反对 回复 2022-06-01
  • 3 回答
  • 0 关注
  • 234 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信