N P
N P

Reputation: 2619

Add logging middle to grpc

I am attempting to add some logging middleware to a grpc server, I am following the simple example on their github here.

https://github.com/grpc-ecosystem/go-grpc-middleware/blob/master/logging/logrus/examples_test.go

I set up my server options as so:

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)
    }
}

However, whenever I look at the logs I am getting a nil pointer.

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/[email protected]/logging/logrus/server_interceptors.go:37 +0x1be
github.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/[email protected]/chain.go:25 +0x63
github.com/grpc-ecosystem/go-grpc-middleware/auth.UnaryServerInterceptor.func1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0xc00018caa0, 0xc00018cae0, 0x89ceda, 0x9f24e0, 0xc00018cb00, 0xc00018caa0)
    /go/pkg/mod/github.com/grpc-ecosystem/[email protected]/auth/auth.go:47 +0x108
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0xc0001a8100, 0x0, 0xc0001c5ac0, 0x40c5b8)
    /go/pkg/mod/github.com/grpc-ecosystem/[email protected]/chain.go:25 +0x63
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0xc00018caa0, 0xc00018cac0, 0xc00008eb30, 0x56cd28, 0xa04380, 0xc00026c390)
    /go/pkg/mod/github.com/grpc-ecosystem/[email protected]/chain.go:34 +0xd5
qcap/proto/company._CompanyService_SelectCompany_Handler(0x9c43e0, 0xc00000e068, 0xb2e640, 0xc00026c390, 0xc0000ab140, 0xc000228120, 0xb2e640, 0xc00026c390, 0xc0000a20f0, 0x26)
    /go/src/qcap/proto/company/service.pb.go:975 +0x14b
google.golang.org/grpc.(*Server).processUnaryRPC(0xc000001500, 0xb35300, 0xc0001da480, 0xc0001a8100, 0xc000228570, 0xf082f8, 0x0, 0x0, 0x0)
    /go/pkg/mod/google.golang.org/[email protected]/server.go:1024 +0x4f4
google.golang.org/grpc.(*Server).handleStream(0xc000001500, 0xb35300, 0xc0001da480, 0xc0001a8100, 0x0)
    /go/pkg/mod/google.golang.org/[email protected]/server.go:1313 +0xd97
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc0001dce50, 0xc000001500, 0xb35300, 0xc0001da480, 0xc0001a8100)
    /go/pkg/mod/google.golang.org/[email protected]/server.go:722 +0xbb
created by google.golang.org/grpc.(*Server).serveStreams.func1
    /go/pkg/mod/google.golang.org/[email protected]/server.go:720 +0xa1

Has anyone come across this before?

Upvotes: 2

Views: 7190

Answers (2)

Ivan Rusli Mcdohl
Ivan Rusli Mcdohl

Reputation: 31

It is panic because of these variables are nil

var (
  logrusLogger *logrus.Logger
  customFunc   grpc_logrus.CodeToLevel
)

instead use

var (
  logrusLogger = logrus.New()
  customFunc   = func(code codes.Code) logrus.Level {
    if code == codes.OK {
        return logrus.InfoLevel
    }
    return logrus.ErrorLevel
  }
)

NOTE: grpc_logrus.CodeToLevel has underlying type of func(code codes.Code) logrus.Level

Upvotes: 3

apolcyn
apolcyn

Reputation: 124

Looking at the examples that this code is inspired from, could the problem be related to not having the "grpc_ctxtags" interceptor? See https://github.com/grpc-ecosystem/go-grpc-middleware/blob/06f64829ca1f521d41cd6235a7a204a6566fb0dc/logging/logrus/examples_test.go#L31

Upvotes: 2

Related Questions