You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/zrpc/internal/clientinterceptors/tracinginterceptor.go

42 lines
1.3 KiB
Go

4 years ago
package clientinterceptors
import (
"context"
"github.com/tal-tech/go-zero/core/trace"
4 years ago
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// UnaryTracingInterceptor is an interceptor that handles tracing.
func UnaryTracingInterceptor(ctx context.Context, method string, req, reply interface{},
4 years ago
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx, span := trace.StartClientSpan(ctx, cc.Target(), method)
defer span.Finish()
var pairs []string
span.Visit(func(key, val string) bool {
pairs = append(pairs, key, val)
return true
})
ctx = metadata.AppendToOutgoingContext(ctx, pairs...)
return invoker(ctx, method, req, reply, cc, opts...)
}
// StreamTracingInterceptor is an interceptor that handles tracing for stream requests.
func StreamTracingInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn,
method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ctx, span := trace.StartClientSpan(ctx, cc.Target(), method)
defer span.Finish()
var pairs []string
span.Visit(func(key, val string) bool {
pairs = append(pairs, key, val)
return true
})
ctx = metadata.AppendToOutgoingContext(ctx, pairs...)
return streamer(ctx, desc, cc, method, opts...)
}