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
99 lines
3.4 KiB
Go
99 lines
3.4 KiB
Go
package grpc_zap_test
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/grpc-ecosystem/go-grpc-middleware"
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/tags/zap"
|
|
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
var (
|
|
zapLogger *zap.Logger
|
|
customFunc grpc_zap.CodeToLevel
|
|
)
|
|
|
|
// Initialization shows a relatively complex initialization sequence.
|
|
func Example_initialization() {
|
|
// Shared options for the logger, with a custom gRPC code to log level function.
|
|
opts := []grpc_zap.Option{
|
|
grpc_zap.WithLevels(customFunc),
|
|
}
|
|
// Make sure that log statements internal to gRPC library are logged using the zapLogger as well.
|
|
grpc_zap.ReplaceGrpcLogger(zapLogger)
|
|
// Create a server, make sure we put the grpc_ctxtags context before everything else.
|
|
_ = grpc.NewServer(
|
|
grpc_middleware.WithUnaryServerChain(
|
|
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
|
|
grpc_zap.UnaryServerInterceptor(zapLogger, opts...),
|
|
),
|
|
grpc_middleware.WithStreamServerChain(
|
|
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
|
|
grpc_zap.StreamServerInterceptor(zapLogger, opts...),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Initialization shows an initialization sequence with the duration field generation overridden.
|
|
func Example_initializationWithDurationFieldOverride() {
|
|
opts := []grpc_zap.Option{
|
|
grpc_zap.WithDurationField(func(duration time.Duration) zapcore.Field {
|
|
return zap.Int64("grpc.time_ns", duration.Nanoseconds())
|
|
}),
|
|
}
|
|
|
|
_ = grpc.NewServer(
|
|
grpc_middleware.WithUnaryServerChain(
|
|
grpc_ctxtags.UnaryServerInterceptor(),
|
|
grpc_zap.UnaryServerInterceptor(zapLogger, opts...),
|
|
),
|
|
grpc_middleware.WithStreamServerChain(
|
|
grpc_ctxtags.StreamServerInterceptor(),
|
|
grpc_zap.StreamServerInterceptor(zapLogger, opts...),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Simple unary handler that adds custom fields to the requests's context. These will be used for all log statements.
|
|
func ExampleExtract_unary() {
|
|
_ = func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
|
|
// Add fields the ctxtags of the request which will be added to all extracted loggers.
|
|
grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
|
|
|
|
// Extract a single request-scoped zap.Logger and log messages. (containing the grpc.xxx tags)
|
|
l := ctx_zap.Extract(ctx)
|
|
l.Info("some ping")
|
|
l.Info("another ping")
|
|
return &pb_testproto.PingResponse{Value: ping.Value}, nil
|
|
}
|
|
}
|
|
|
|
func Example_initializationWithDecider() {
|
|
opts := []grpc_zap.Option{
|
|
grpc_zap.WithDecider(func(fullMethodName string, err error) bool {
|
|
// will not log gRPC calls if it was a call to healthcheck and no error was raised
|
|
if err == nil && fullMethodName == "foo.bar.healthcheck" {
|
|
return false
|
|
}
|
|
|
|
// by default everything will be logged
|
|
return true
|
|
}),
|
|
}
|
|
|
|
_ = []grpc.ServerOption{
|
|
grpc_middleware.WithStreamServerChain(
|
|
grpc_ctxtags.StreamServerInterceptor(),
|
|
grpc_zap.StreamServerInterceptor(zap.NewNop(), opts...)),
|
|
grpc_middleware.WithUnaryServerChain(
|
|
grpc_ctxtags.UnaryServerInterceptor(),
|
|
grpc_zap.UnaryServerInterceptor(zap.NewNop(), opts...)),
|
|
}
|
|
}
|