|
|
@@ -7,6 +7,7 @@ import (
|
|
|
tracesdk "go.opentelemetry.io/otel/sdk/trace"
|
|
|
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
|
|
|
"strings"
|
|
|
+ "sync"
|
|
|
)
|
|
|
|
|
|
type RootData struct {
|
|
|
@@ -75,8 +76,16 @@ type MapInfo struct {
|
|
|
}
|
|
|
|
|
|
type TraceMapT struct {
|
|
|
- RootData RootData `json:"root_data"`
|
|
|
- Index int `json:"index"`
|
|
|
+ RootData RootData
|
|
|
+ Index int
|
|
|
+ lock *sync.RWMutex
|
|
|
+ TheEnd bool
|
|
|
+}
|
|
|
+
|
|
|
+var TraceRootMap map[string]*TraceMapT
|
|
|
+
|
|
|
+func init() {
|
|
|
+ TraceRootMap = make(map[string]*TraceMapT)
|
|
|
}
|
|
|
|
|
|
func tracetransformData(sdl []tracesdk.ReadOnlySpan) []RootData {
|
|
|
@@ -87,28 +96,35 @@ func tracetransformData(sdl []tracesdk.ReadOnlySpan) []RootData {
|
|
|
//traceMap := make(map[string][]MapInfo)
|
|
|
//traceIndexMap := make(map[string]int)
|
|
|
|
|
|
- traceRoot := make(map[string]*TraceMapT)
|
|
|
- sendData := []RootData{}
|
|
|
+ //traceRoot := make(map[string]*TraceMapT)
|
|
|
for _, sd := range sdl {
|
|
|
if sd == nil {
|
|
|
continue
|
|
|
}
|
|
|
traceId := sd.SpanContext().TraceID().String()
|
|
|
- if _, ok := traceRoot[traceId]; !ok {
|
|
|
- traceRoot[traceId] = &TraceMapT{RootData: initRootData(traceId), Index: 1}
|
|
|
+ if _, ok := TraceRootMap[traceId]; !ok {
|
|
|
+ TraceRootMap[traceId] = &TraceMapT{RootData: initRootData(traceId), Index: 1}
|
|
|
}
|
|
|
-
|
|
|
- traceRoot[traceId].Index++
|
|
|
- initMapInfo(sd, traceRoot[traceId])
|
|
|
+ TraceRootMap[traceId].Index++
|
|
|
+ initMapInfo(sd, TraceRootMap[traceId])
|
|
|
}
|
|
|
- for _, v := range traceRoot {
|
|
|
- sendData = append(sendData, v.RootData)
|
|
|
+ // 发送完整数据 | 大量长耗时请求会增加内存占用
|
|
|
+ sendData := []RootData{}
|
|
|
+ for traceId, v := range TraceRootMap {
|
|
|
+ if v.TheEnd {
|
|
|
+ sendData = append(sendData, v.RootData)
|
|
|
+ delete(TraceRootMap, traceId)
|
|
|
+ fmt.Println("the end!")
|
|
|
+ } else {
|
|
|
+ fmt.Println("not end!")
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// Transform the categorized map into a slice
|
|
|
aa, _ := json.Marshal(sendData)
|
|
|
fmt.Println(string(aa))
|
|
|
fmt.Println(len(sendData))
|
|
|
+ fmt.Println(len(TraceRootMap))
|
|
|
return sendData
|
|
|
}
|
|
|
|
|
|
@@ -197,12 +213,32 @@ func initMapInfo(sd tracesdk.ReadOnlySpan, traceRoot *TraceMapT) MapInfo {
|
|
|
case "db.statement":
|
|
|
query := attr.Value.AsString()
|
|
|
mNode.Ps = []string{query}
|
|
|
- upperOperType := strings.ToUpper(query[:6])
|
|
|
- if upperOperType == "SELECT" ||
|
|
|
- upperOperType == "UPDATE" ||
|
|
|
- upperOperType == "INSERT" ||
|
|
|
- upperOperType == "DELETE" {
|
|
|
- mNode.OperType = upperOperType
|
|
|
+ words := strings.Fields(query)
|
|
|
+ if len(words) > 0 {
|
|
|
+ mNode.OperType = strings.ToUpper(words[0])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ traceRoot.RootData.Maps = append(traceRoot.RootData.Maps, mNode)
|
|
|
+ case "REDIS":
|
|
|
+ fmt.Println("REDIS")
|
|
|
+ mNode.ServiceName = mapType
|
|
|
+ mNode.ServiceType = "NOSQL"
|
|
|
+ mNode.MethodName = "(redis)"
|
|
|
+ mNode.MethodDesc = "(redis)"
|
|
|
+ for _, attr := range sd.Attributes() {
|
|
|
+ fmt.Println(attr.Key, ":", attr.Value.AsInterface())
|
|
|
+ switch attr.Key {
|
|
|
+ case "net.peer.name":
|
|
|
+ mNode.Ip = attr.Value.AsString()
|
|
|
+ case "net.peer.port":
|
|
|
+ mNode.Port = attr.Value.AsInt64()
|
|
|
+ case "db.statement":
|
|
|
+ query := attr.Value.AsString()
|
|
|
+ mNode.Ps = []string{query}
|
|
|
+ words := strings.Fields(query)
|
|
|
+ if len(words) > 0 {
|
|
|
+ mNode.OperType = strings.ToUpper(words[0])
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -210,8 +246,8 @@ func initMapInfo(sd tracesdk.ReadOnlySpan, traceRoot *TraceMapT) MapInfo {
|
|
|
case "APPLICATION":
|
|
|
mNode.ServiceName = "GO"
|
|
|
mNode.ServiceType = "APPLICATION"
|
|
|
- mNode.MethodName = "main"
|
|
|
- mNode.MethodDesc = "main"
|
|
|
+ mNode.MethodName = "endpoint"
|
|
|
+ mNode.MethodDesc = "endpoint"
|
|
|
mNode.Level = 1
|
|
|
mNode.Nid = 1
|
|
|
mNode.Pid = 0
|
|
|
@@ -235,6 +271,7 @@ func initMapInfo(sd tracesdk.ReadOnlySpan, traceRoot *TraceMapT) MapInfo {
|
|
|
traceRoot.RootData.LocalPort = attr.Value.AsInt64()
|
|
|
}
|
|
|
}
|
|
|
+ traceRoot.TheEnd = true
|
|
|
traceRoot.RootData.Maps = append([]MapInfo{mNode}, traceRoot.RootData.Maps...)
|
|
|
}
|
|
|
return mNode
|