@@ -10,6 +10,8 @@ import (
1010 "github.com/HXSecurity/DongTai-agent-go/utils"
1111 "google.golang.org/grpc"
1212 "google.golang.org/grpc/metadata"
13+ "strconv"
14+ "strings"
1315)
1416
1517func NewServer (opt ... grpc.ServerOption ) * grpc.Server {
@@ -20,6 +22,24 @@ func NewServer(opt ...grpc.ServerOption) *grpc.Server {
2022// interceptor 一元拦截器
2123func interceptor (ctx context.Context , req interface {}, info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (interface {}, error ) {
2224 md , _ := metadata .FromIncomingContext (ctx )
25+ Traceid := md .Get ("dt-traceid" )[0 ]
26+ worker , _ := utils .NewWorker (global .AgentId )
27+ four := strconv .Itoa (int (worker .GetId ()))
28+ tranceids := strings .Split (Traceid , "." )
29+ tranceids [1 ] = strconv .Itoa (global .AgentId )
30+ num , _ := strconv .Atoi (tranceids [3 ])
31+ tranceids [3 ] = strconv .Itoa (num + 1 )
32+ tranceids [4 ] = four
33+ newId := ""
34+ for i := 0 ; i < len (tranceids ); i ++ {
35+ if i == 4 {
36+ newId += tranceids [i ]
37+ } else {
38+ newId += tranceids [i ] + "."
39+ }
40+ }
41+ global .TraceId = tranceids [0 ]
42+
2343 id := utils .CatGoroutineID ()
2444 request .FmtHookPool (request.PoolReq {
2545 Reqs : request .Collect (req ),
@@ -33,17 +53,17 @@ func interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInf
3353 go func () {
3454 worker , _ := utils .NewWorker (global .AgentId )
3555 onlyKey := int (worker .GetId ())
36- header := base64 .StdEncoding .EncodeToString ([]byte (md . Get ( "headers" )[ 0 ] ))
56+ header := base64 .StdEncoding .EncodeToString ([]byte ("dt-traceid:" + newId ))
3757 HookGroup := & request.UploadReq {
3858 Type : 36 ,
3959 InvokeId : onlyKey ,
4060 Detail : request.Detail {
4161 AgentId : global .AgentId ,
4262 Function : request.Function {
4363 Method : "RPC" ,
44- Url : md . Get ( "requestURL" )[ 0 ] ,
45- Uri : md . Get ( "requestURI" )[ 0 ] ,
46- Protocol : md . Get ( "protocol" )[ 0 ] ,
64+ Url : info . FullMethod ,
65+ Uri : info . FullMethod ,
66+ Protocol : "ProtoBuf" ,
4767 ClientIp : "" ,
4868 Language : "GO" ,
4969 Scheme : "GRPC" ,
@@ -52,7 +72,7 @@ func interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInf
5272 ReqBody : "" ,
5373 QueryString : "" ,
5474 Pool : []request.Pool {},
55- TraceId : md . Get ( "dt-traceid" )[ 0 ] ,
75+ TraceId : newId ,
5676 },
5777 },
5878 }
0 commit comments