tracinginterceptor.go 789 B

1234567891011121314151617181920212223242526272829
  1. package serverinterceptors
  2. import (
  3. "context"
  4. "git.i2edu.net/i2/go-zero/core/trace"
  5. "google.golang.org/grpc"
  6. "google.golang.org/grpc/metadata"
  7. )
  8. // UnaryTracingInterceptor returns a func that handles tracing with given service name.
  9. func UnaryTracingInterceptor(serviceName string) grpc.UnaryServerInterceptor {
  10. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  11. handler grpc.UnaryHandler) (resp interface{}, err error) {
  12. md, ok := metadata.FromIncomingContext(ctx)
  13. if !ok {
  14. return handler(ctx, req)
  15. }
  16. carrier, err := trace.Extract(trace.GrpcFormat, md)
  17. if err != nil {
  18. return handler(ctx, req)
  19. }
  20. ctx, span := trace.StartServerSpan(ctx, carrier, serviceName, info.FullMethod)
  21. defer span.Finish()
  22. return handler(ctx, req)
  23. }
  24. }