Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame^] | 1 | // Copyright 2017 Michal Witkowski. All Rights Reserved. |
| 2 | // See LICENSE for licensing terms. |
| 3 | |
| 4 | package grpc_ctxtags |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | |
| 9 | "google.golang.org/grpc" |
| 10 | "google.golang.org/grpc/peer" |
| 11 | |
| 12 | "github.com/grpc-ecosystem/go-grpc-middleware" |
| 13 | ) |
| 14 | |
| 15 | // UnaryServerInterceptor returns a new unary server interceptors that sets the values for request tags. |
| 16 | func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor { |
| 17 | o := evaluateOptions(opts) |
| 18 | return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { |
| 19 | newCtx := newTagsForCtx(ctx) |
| 20 | if o.requestFieldsFunc != nil { |
| 21 | setRequestFieldTags(newCtx, o.requestFieldsFunc, info.FullMethod, req) |
| 22 | } |
| 23 | return handler(newCtx, req) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // StreamServerInterceptor returns a new streaming server interceptor that sets the values for request tags. |
| 28 | func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor { |
| 29 | o := evaluateOptions(opts) |
| 30 | return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { |
| 31 | newCtx := newTagsForCtx(stream.Context()) |
| 32 | if o.requestFieldsFunc == nil { |
| 33 | // Short-circuit, don't do the expensive bit of allocating a wrappedStream. |
| 34 | wrappedStream := grpc_middleware.WrapServerStream(stream) |
| 35 | wrappedStream.WrappedContext = newCtx |
| 36 | return handler(srv, wrappedStream) |
| 37 | } |
| 38 | wrapped := &wrappedStream{stream, info, o, newCtx, true} |
| 39 | err := handler(srv, wrapped) |
| 40 | return err |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // wrappedStream is a thin wrapper around grpc.ServerStream that allows modifying context and extracts log fields from the initial message. |
| 45 | type wrappedStream struct { |
| 46 | grpc.ServerStream |
| 47 | info *grpc.StreamServerInfo |
| 48 | opts *options |
| 49 | // WrappedContext is the wrapper's own Context. You can assign it. |
| 50 | WrappedContext context.Context |
| 51 | initial bool |
| 52 | } |
| 53 | |
| 54 | // Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context() |
| 55 | func (w *wrappedStream) Context() context.Context { |
| 56 | return w.WrappedContext |
| 57 | } |
| 58 | |
| 59 | func (w *wrappedStream) RecvMsg(m interface{}) error { |
| 60 | err := w.ServerStream.RecvMsg(m) |
| 61 | // We only do log fields extraction on the single-request of a server-side stream. |
| 62 | if !w.info.IsClientStream || w.opts.requestFieldsFromInitial && w.initial { |
| 63 | w.initial = false |
| 64 | |
| 65 | setRequestFieldTags(w.Context(), w.opts.requestFieldsFunc, w.info.FullMethod, m) |
| 66 | } |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | func newTagsForCtx(ctx context.Context) context.Context { |
| 71 | t := NewTags() |
| 72 | if peer, ok := peer.FromContext(ctx); ok { |
| 73 | t.Set("peer.address", peer.Addr.String()) |
| 74 | } |
| 75 | return SetInContext(ctx, t) |
| 76 | } |
| 77 | |
| 78 | func setRequestFieldTags(ctx context.Context, f RequestFieldExtractorFunc, fullMethodName string, req interface{}) { |
| 79 | if valMap := f(fullMethodName, req); valMap != nil { |
| 80 | t := Extract(ctx) |
| 81 | for k, v := range valMap { |
| 82 | t.Set("grpc.request."+k, v) |
| 83 | } |
| 84 | } |
| 85 | } |