core/telemetry: move requestid to pkg directory (#4911)

This commit is contained in:
Caleb Doxsey 2024-01-19 13:18:16 -07:00 committed by GitHub
parent 803baeb9e1
commit 4301da3648
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 13 additions and 13 deletions

View file

@ -0,0 +1,40 @@
package requestid
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// StreamClientInterceptor returns a new gRPC StreamClientInterceptor which puts the request ID in the outgoing
// metadata.
func StreamClientInterceptor() grpc.StreamClientInterceptor {
return func(ctx context.Context,
desc *grpc.StreamDesc, cc *grpc.ClientConn,
method string, streamer grpc.Streamer, opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
ctx = toMetadata(ctx)
return streamer(ctx, desc, cc, method, opts...)
}
}
// UnaryClientInterceptor returns a new gRPC UnaryClientInterceptor which puts the request ID in the outgoing
// metadata.
func UnaryClientInterceptor() grpc.UnaryClientInterceptor {
return func(ctx context.Context,
method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
ctx = toMetadata(ctx)
return invoker(ctx, method, req, reply, cc, opts...)
}
}
func toMetadata(ctx context.Context) context.Context {
requestID := FromContext(ctx)
if requestID == "" {
requestID = New()
}
return metadata.AppendToOutgoingContext(ctx, headerName, requestID)
}