Skip to content

Commit c6a2be0

Browse files
author
奇淼(piexlmax
authored
Merge pull request #30 from piexlmax/main
fixed:grpc
2 parents 02db124 + e05c6e4 commit c6a2be0

File tree

6 files changed

+48
-15
lines changed

6 files changed

+48
-15
lines changed

core/gin/ginEngineServerHTTP/replacement.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/gin-gonic/gin"
1212
"net/http"
1313
"reflect"
14+
"strconv"
1415
"strings"
1516
)
1617

@@ -24,6 +25,8 @@ func MyServer(server *gin.Engine, w http.ResponseWriter, r *http.Request) {
2425
for k, v := range r.Header {
2526
headerBase += k + ": " + strings.Join(v, ",") + "\n"
2627
}
28+
tranceID := global.TraceId + "." + strconv.Itoa(global.AgentId) + ".0.0.0"
29+
headerBase += "dt-traceid:" + tranceID
2730
if t.Kind() == reflect.Ptr {
2831
buf := t.
2932
Elem().

core/grpc/clientConn/replacement.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ func Invoke(cl *grpc.ClientConn, ctx context.Context, method string, args, reply
2020
}
2121

2222
if tranceid == "" {
23-
tranceid = global.TraceId + "." + strconv.Itoa(global.AgentId) + ".0." + strconv.Itoa(int(worker.GetId()))
23+
tranceid = global.TraceId + "." + strconv.Itoa(global.AgentId) + ".0.0." + strconv.Itoa(int(worker.GetId()))
2424
} else {
2525
four := strconv.Itoa(int(worker.GetId()))
2626
tranceids := strings.Split(tranceid, ".")
2727
tranceids[1] = strconv.Itoa(global.AgentId)
28-
int, _ := strconv.Atoi(tranceids[2])
29-
tranceids[2] = strconv.Itoa(int + 1)
30-
tranceids[3] = four
28+
num, _ := strconv.Atoi(tranceids[3])
29+
tranceids[3] = strconv.Itoa(num + 1)
30+
tranceids[4] = four
3131
newId := ""
32-
for i := range tranceids {
33-
if i == 3 {
34-
newId = tranceids[i]
32+
for i := 0; i < len(tranceids); i++ {
33+
if i == 4 {
34+
newId += tranceids[i]
35+
} else {
36+
newId += tranceids[i] + "."
3537
}
36-
newId = tranceids[i] + "."
3738
}
3839
tranceid = newId
3940
}

core/grpc/newServer/replacement.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1517
func NewServer(opt ...grpc.ServerOption) *grpc.Server {
@@ -20,6 +22,24 @@ func NewServer(opt ...grpc.ServerOption) *grpc.Server {
2022
// interceptor 一元拦截器
2123
func 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
}

core/http/httpServeHTTP/replacement.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/HXSecurity/DongTai-agent-go/utils"
1111
"net/http"
1212
"reflect"
13+
"strconv"
1314
"strings"
1415
)
1516

@@ -23,6 +24,8 @@ func MyServer(server *http.ServeMux, w http.ResponseWriter, r *http.Request) {
2324
for k, v := range r.Header {
2425
headerBase += k + ": " + strings.Join(v, ",") + "\n"
2526
}
27+
tranceID := global.TraceId + "." + strconv.Itoa(global.AgentId) + ".0.0.0"
28+
headerBase += "dt-traceid:" + tranceID
2629
if t.Kind() == reflect.Ptr {
2730
buf := t.
2831
Elem().

core/httpRouter/httpRouter/replacement.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/julienschmidt/httprouter"
1313
"net/http"
1414
"reflect"
15+
"strconv"
1516
"strings"
1617
)
1718

@@ -35,6 +36,8 @@ func MyHttpRouterServer(server *httprouter.Router, w http.ResponseWriter, r *htt
3536
for k, v := range r.Header {
3637
headerBase += k + ": " + strings.Join(v, ",") + "\n"
3738
}
39+
tranceID := global.TraceId + "." + strconv.Itoa(global.AgentId) + ".0.0.0"
40+
headerBase += "dt-traceid:" + tranceID
3841
if t.Kind() == reflect.Ptr {
3942
buf := t.
4043
Elem().

run/base/base.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import (
2222
_ "github.com/HXSecurity/DongTai-agent-go/core/base/urlUrlString"
2323
"github.com/HXSecurity/DongTai-agent-go/hook"
2424
"github.com/HXSecurity/DongTai-agent-go/service"
25-
"github.com/google/uuid"
25+
"github.com/HXSecurity/DongTai-agent-go/utils"
26+
"strconv"
2627

2728
"github.com/HXSecurity/DongTai-agent-go/global"
2829
)
@@ -33,5 +34,7 @@ func init() {
3334
global.InitViper()
3435
_ = service.AgentRegister()
3536
b.HookAll()
36-
global.TraceId = uuid.New().String() + "-" + uuid.New().String()
37+
worker, _ := utils.NewWorker(global.AgentId)
38+
strconv.Itoa(int(worker.GetId()))
39+
global.TraceId = strconv.Itoa(int(worker.GetId())) + "-" + strconv.Itoa(int(worker.GetId()))
3740
}

0 commit comments

Comments
 (0)