tracinginterceptor.go 692 B

12345678910111213141516171819202122232425
  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. // TracingInterceptor is an interceptor that handles tracing.
  9. func TracingInterceptor(ctx context.Context, method string, req, reply interface{},
  10. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  11. ctx, span := trace.StartClientSpan(ctx, cc.Target(), method)
  12. defer span.Finish()
  13. var pairs []string
  14. span.Visit(func(key, val string) bool {
  15. pairs = append(pairs, key, val)
  16. return true
  17. })
  18. ctx = metadata.AppendToOutgoingContext(ctx, pairs...)
  19. return invoker(ctx, method, req, reply, cc, opts...)
  20. }