mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* Adding a way to inject a request ID It is very useful to associate a request ID to each incoming request, this change allows to provide a function to do that via Server Option. The change comes with a default function which will generate a new request ID. The request ID is put in the request context along with a common logger which always logs the request-id We add gRPC interceptors to the server so it can get the request ID out of the gRPC metadata and put it in the common logger stored in the context so as all the log lines using the common logger from the context will have the request ID logged
106 lines
3.1 KiB
Go
106 lines
3.1 KiB
Go
package grpc_logrus_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/testing"
|
|
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
|
|
"github.com/sirupsen/logrus"
|
|
"golang.org/x/net/context"
|
|
"google.golang.org/grpc/codes"
|
|
)
|
|
|
|
var (
|
|
goodPing = &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 9999}
|
|
)
|
|
|
|
type loggingPingService struct {
|
|
pb_testproto.TestServiceServer
|
|
}
|
|
|
|
func customCodeToLevel(c codes.Code) logrus.Level {
|
|
if c == codes.Unauthenticated {
|
|
// Make this a special case for tests, and an error.
|
|
return logrus.ErrorLevel
|
|
}
|
|
level := grpc_logrus.DefaultCodeToLevel(c)
|
|
return level
|
|
}
|
|
|
|
func (s *loggingPingService) Ping(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
|
|
grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
|
|
ctx_logrus.AddFields(ctx, logrus.Fields{"custom_field": "custom_value"})
|
|
ctx_logrus.Extract(ctx).Info("some ping")
|
|
return s.TestServiceServer.Ping(ctx, ping)
|
|
}
|
|
|
|
func (s *loggingPingService) PingError(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.Empty, error) {
|
|
return s.TestServiceServer.PingError(ctx, ping)
|
|
}
|
|
|
|
func (s *loggingPingService) PingList(ping *pb_testproto.PingRequest, stream pb_testproto.TestService_PingListServer) error {
|
|
grpc_ctxtags.Extract(stream.Context()).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
|
|
ctx_logrus.AddFields(stream.Context(), logrus.Fields{"custom_field": "custom_value"})
|
|
ctx_logrus.Extract(stream.Context()).Info("some pinglist")
|
|
return s.TestServiceServer.PingList(ping, stream)
|
|
}
|
|
|
|
func (s *loggingPingService) PingEmpty(ctx context.Context, empty *pb_testproto.Empty) (*pb_testproto.PingResponse, error) {
|
|
return s.TestServiceServer.PingEmpty(ctx, empty)
|
|
}
|
|
|
|
type logrusBaseSuite struct {
|
|
*grpc_testing.InterceptorTestSuite
|
|
mutexBuffer *grpc_testing.MutexReadWriter
|
|
buffer *bytes.Buffer
|
|
logger *logrus.Logger
|
|
}
|
|
|
|
func newLogrusBaseSuite(t *testing.T) *logrusBaseSuite {
|
|
b := &bytes.Buffer{}
|
|
muB := grpc_testing.NewMutexReadWriter(b)
|
|
logger := logrus.New()
|
|
logger.Out = muB
|
|
logger.Formatter = &logrus.JSONFormatter{DisableTimestamp: true}
|
|
return &logrusBaseSuite{
|
|
logger: logger,
|
|
buffer: b,
|
|
mutexBuffer: muB,
|
|
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{
|
|
TestService: &loggingPingService{&grpc_testing.TestPingService{T: t}},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (s *logrusBaseSuite) SetupTest() {
|
|
s.mutexBuffer.Lock()
|
|
s.buffer.Reset()
|
|
s.mutexBuffer.Unlock()
|
|
}
|
|
|
|
func (s *logrusBaseSuite) getOutputJSONs() []map[string]interface{} {
|
|
ret := make([]map[string]interface{}, 0)
|
|
dec := json.NewDecoder(s.mutexBuffer)
|
|
|
|
for {
|
|
var val map[string]interface{}
|
|
err := dec.Decode(&val)
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
s.T().Fatalf("failed decoding output from Logrus JSON: %v", err)
|
|
}
|
|
|
|
ret = append(ret, val)
|
|
}
|
|
|
|
return ret
|
|
}
|