forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate request ID through gRPC context (thanos-io#7356)
* Propagate request ID through gRPC context The request ID only gets propagated through HTTP calls and is not available in gRPC servers. This commit adds intereceptors to grpc servers and clients to make sure request ID propagation happens. Signed-off-by: Filip Petkovski <[email protected]> * Add license Signed-off-by: Filip Petkovski <[email protected]> --------- Signed-off-by: Filip Petkovski <[email protected]>
- Loading branch information
1 parent
fc1ee71
commit df06df7
Showing
4 changed files
with
75 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package grpc | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
|
||
"github.com/thanos-io/thanos/pkg/server/http/middleware" | ||
) | ||
|
||
const requestIDKey = "request-id" | ||
|
||
func NewUnaryClientRequestIDInterceptor() grpc.UnaryClientInterceptor { | ||
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
reqID, ok := middleware.RequestIDFromContext(ctx) | ||
if ok { | ||
ctx = metadata.AppendToOutgoingContext(ctx, requestIDKey, reqID) | ||
} | ||
return invoker(ctx, method, req, reply, cc, opts...) | ||
} | ||
} | ||
|
||
func NewUnaryServerRequestIDInterceptor() grpc.UnaryServerInterceptor { | ||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { | ||
if vals := metadata.ValueFromIncomingContext(ctx, requestIDKey); len(vals) == 1 { | ||
ctx = middleware.NewContextWithRequestID(ctx, vals[0]) | ||
} | ||
return handler(ctx, req) | ||
} | ||
} | ||
|
||
func NewStreamClientRequestIDInterceptor() grpc.StreamClientInterceptor { | ||
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { | ||
reqID, ok := middleware.RequestIDFromContext(ctx) | ||
if ok { | ||
ctx = metadata.AppendToOutgoingContext(ctx, requestIDKey, reqID) | ||
} | ||
return streamer(ctx, desc, cc, method, opts...) | ||
} | ||
} | ||
|
||
func NewStreamServerRequestIDInterceptor() grpc.StreamServerInterceptor { | ||
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | ||
if vals := metadata.ValueFromIncomingContext(ss.Context(), requestIDKey); len(vals) == 1 { | ||
ctx := middleware.NewContextWithRequestID(ss.Context(), vals[0]) | ||
return handler(srv, newStreamWithContext(ctx, ss)) | ||
} | ||
return handler(srv, ss) | ||
} | ||
} | ||
|
||
type streamWithContext struct { | ||
grpc.ServerStream | ||
ctx context.Context | ||
} | ||
|
||
func newStreamWithContext(ctx context.Context, serverStream grpc.ServerStream) *streamWithContext { | ||
return &streamWithContext{ServerStream: serverStream, ctx: ctx} | ||
} | ||
|
||
func (s streamWithContext) Context() context.Context { | ||
return s.ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters