tracinginterceptor.go 704 B

12345678910111213141516171819202122232425262728
  1. package serverinterceptors
  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 UnaryTracingInterceptor(serviceName string) grpc.UnaryServerInterceptor {
  9. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  10. handler grpc.UnaryHandler) (resp interface{}, err error) {
  11. md, ok := metadata.FromIncomingContext(ctx)
  12. if !ok {
  13. return handler(ctx, req)
  14. }
  15. carrier, err := trace.Extract(trace.GrpcFormat, md)
  16. if err != nil {
  17. return handler(ctx, req)
  18. }
  19. ctx, span := trace.StartServerSpan(ctx, carrier, serviceName, info.FullMethod)
  20. defer span.Finish()
  21. return handler(ctx, req)
  22. }
  23. }