@@ -12,6 +12,7 @@ import (
12
12
"google.golang.org/grpc/status"
13
13
"google.golang.org/protobuf/proto"
14
14
"google.golang.org/protobuf/reflect/protoreflect"
15
+ "google.golang.org/protobuf/types/known/structpb"
15
16
16
17
"github.com/KyberNetwork/service-framework/pkg/common"
17
18
"github.com/KyberNetwork/service-framework/pkg/observe/kmetric"
@@ -28,12 +29,14 @@ func UnaryServerInterceptor(cfg grpcserver.Config) grpc.UnaryServerInterceptor {
28
29
wrapper := grpcStatusWrapper {cfg : cfg }
29
30
return func (ctx context.Context , req any , info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (res any ,
30
31
err error ) {
32
+ var traceIdStr string
31
33
if traceId , ok := common .TraceIdFromCtx (ctx ); ok {
32
- traceIdStr : = traceId .String ()
34
+ traceIdStr = traceId .String ()
33
35
_ = grpc .SetHeader (ctx , metadata .Pairs (common .HeaderXTraceId , traceIdStr ))
34
36
ctx = klog .CtxWithLogger (ctx ,
35
37
klog .WithFields (ctx , klog.Fields {common .LogFieldTraceId : traceIdStr }))
36
38
defer func () {
39
+
37
40
if res , ok := res .(proto.Message ); ok && res != nil {
38
41
m := res .ProtoReflect ()
39
42
if fd := m .Descriptor ().Fields ().ByName (FieldNameRequestId ); fd != nil &&
@@ -52,6 +55,11 @@ func UnaryServerInterceptor(cfg grpcserver.Config) grpc.UnaryServerInterceptor {
52
55
res , err = handler (ctx , req )
53
56
if err != nil {
54
57
st := wrapper .GrpcStatus (err )
58
+ if reqIdDetail , err := structpb .NewStruct (map [string ]any {FieldNameRequestId : traceIdStr }); err == nil {
59
+ if stWithReqId , err := st .WithDetails (reqIdDetail ); err == nil {
60
+ st = stWithReqId
61
+ }
62
+ }
55
63
code = st .Code ()
56
64
return nil , st .Err ()
57
65
}
0 commit comments