mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-04 01:09:36 +02:00
envoy: use envoy request id for logging across systems with http and gRPC (#691)
This commit is contained in:
parent
593c47f8ac
commit
41855e5419
16 changed files with 228 additions and 253 deletions
56
internal/telemetry/requestid/grpc_server.go
Normal file
56
internal/telemetry/requestid/grpc_server.go
Normal 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]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue