Skip to content

Commit a8f16ef

Browse files
odeke-emolavloite
andauthored
fix(spanner): inject "x-goog-spanner-request-id" into outgoing client context (#11544)
Injects metadata derived from the associated gax.CallOptions that were correctly being generated and tested, but accidentally not injected into the outgoing context. Fixes #11543 Co-authored-by: Knut Olav Løite <[email protected]>
1 parent 59fe58a commit a8f16ef

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

spanner/request_id_header.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (r requestID) augmentErrorWithRequestID(err error) error {
116116
}
117117
}
118118

119-
func gRPCCallOptionsToRequestID(opts []grpc.CallOption) (reqID requestID, found bool) {
119+
func gRPCCallOptionsToRequestID(opts []grpc.CallOption) (md metadata.MD, reqID requestID, found bool) {
120120
for _, opt := range opts {
121121
hdrOpt, ok := opt.(grpc.HeaderCallOption)
122122
if !ok {
@@ -126,6 +126,7 @@ func gRPCCallOptionsToRequestID(opts []grpc.CallOption) (reqID requestID, found
126126
metadata := hdrOpt.HeaderAddr
127127
reqIDs := metadata.Get(xSpannerRequestIDHeader)
128128
if len(reqIDs) != 0 && len(reqIDs[0]) != 0 {
129+
md = *metadata
129130
reqID = requestID(reqIDs[0])
130131
found = true
131132
break
@@ -137,7 +138,11 @@ func gRPCCallOptionsToRequestID(opts []grpc.CallOption) (reqID requestID, found
137138
func (wr *requestIDHeaderInjector) interceptUnary(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
138139
// It is imperative to search for the requestID before the call
139140
// because gRPC's internals will consume the headers.
140-
reqID, foundRequestID := gRPCCallOptionsToRequestID(opts)
141+
metadataWithRequestID, reqID, foundRequestID := gRPCCallOptionsToRequestID(opts)
142+
if foundRequestID {
143+
ctx = metadata.NewOutgoingContext(ctx, metadataWithRequestID)
144+
}
145+
141146
err := invoker(ctx, method, req, reply, cc, opts...)
142147
if !foundRequestID {
143148
return err
@@ -174,7 +179,11 @@ type requestIDHeaderInjector int
174179
func (wr *requestIDHeaderInjector) interceptStream(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
175180
// It is imperative to search for the requestID before the call
176181
// because gRPC's internals will consume the headers.
177-
reqID, foundRequestID := gRPCCallOptionsToRequestID(opts)
182+
metadataWithRequestID, reqID, foundRequestID := gRPCCallOptionsToRequestID(opts)
183+
if foundRequestID {
184+
ctx = metadata.NewOutgoingContext(ctx, metadataWithRequestID)
185+
}
186+
178187
cs, err := streamer(ctx, desc, cc, method, opts...)
179188
if !foundRequestID {
180189
return cs, err

0 commit comments

Comments
 (0)