mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 09:40:24 +08:00
30 lines
682 B
Go
30 lines
682 B
Go
|
package serverinterceptors
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"zero/core/trace"
|
||
|
|
||
|
"google.golang.org/grpc"
|
||
|
"google.golang.org/grpc/metadata"
|
||
|
)
|
||
|
|
||
|
func UnaryTracingInterceptor(serviceName string) grpc.UnaryServerInterceptor {
|
||
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
|
||
|
handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||
|
md, ok := metadata.FromIncomingContext(ctx)
|
||
|
if !ok {
|
||
|
return handler(ctx, req)
|
||
|
}
|
||
|
|
||
|
carrier, err := trace.Extract(trace.GrpcFormat, md)
|
||
|
if err != nil {
|
||
|
return handler(ctx, req)
|
||
|
}
|
||
|
|
||
|
ctx, span := trace.StartServerSpan(ctx, carrier, serviceName, info.FullMethod)
|
||
|
defer span.Finish()
|
||
|
return handler(ctx, req)
|
||
|
}
|
||
|
}
|