tracinginterceptor.go 630 B

123456789101112131415161718192021222324
  1. package clientinterceptors
  2. import (
  3. "context"
  4. "github.com/tal-tech/go-zero/core/trace"
  5. "google.golang.org/grpc"
  6. "google.golang.org/grpc/metadata"
  7. )
  8. func TracingInterceptor(ctx context.Context, method string, req, reply interface{},
  9. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  10. ctx, span := trace.StartClientSpan(ctx, cc.Target(), method)
  11. defer span.Finish()
  12. var pairs []string
  13. span.Visit(func(key, val string) bool {
  14. pairs = append(pairs, key, val)
  15. return true
  16. })
  17. ctx = metadata.AppendToOutgoingContext(ctx, pairs...)
  18. return invoker(ctx, method, req, reply, cc, opts...)
  19. }