envoy: use envoy request id for logging across systems with http and gRPC (#691)

This commit is contained in:
Caleb Doxsey 2020-05-12 06:55:55 -06:00 committed by Travis Groth
parent 593c47f8ac
commit 41855e5419
16 changed files with 228 additions and 253 deletions

View file

@ -0,0 +1,56 @@
package requestid
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type grpcStream struct {
grpc.ServerStream
ctx context.Context
}
func (ss grpcStream) Context() context.Context {
return ss.ctx
}
// StreamServerInterceptor returns a new gRPC StreamServerInterceptor which populates the request id
// from the incoming metadata.
func StreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := ss.Context()
requestID := fromMetadata(ctx)
ctx = WithValue(ctx, requestID)
ss = grpcStream{
ServerStream: ss,
ctx: ctx,
}
return handler(srv, ss)
}
}
// UnaryServerInterceptor returns a new gRPC UnaryServerInterceptor which populates the request id
// from the incoming metadata.
func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
requestID := fromMetadata(ctx)
ctx = WithValue(ctx, requestID)
return handler(ctx, req)
}
}
func fromMetadata(ctx context.Context) string {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return New()
}
headers := md.Get(headerName)
if len(headers) == 0 || headers[0] == "" {
return New()
}
return headers[0]
}