Просмотр исходного кода

Fixed #JiraBug28171 ebpf探针采集到的堆栈start_time时间单位为纳秒和其他探针使用单位不一致,导致在堆栈追踪中耗时占比瀑布图画的不对

Carl 1 год назад
Родитель
Сommit
f02ac41e39
1 измененных файлов с 117 добавлено и 105 удалено
  1. 117 105
      pkg/go.opentelemetry.io/otel/exporters/otlp/otlptrace/apm_exporter.go

+ 117 - 105
pkg/go.opentelemetry.io/otel/exporters/otlp/otlptrace/apm_exporter.go

@@ -7,6 +7,8 @@ import (
 	. "github.com/coroot/coroot-node-agent/ebpftracer"
 	"github.com/coroot/coroot-node-agent/ebpftracer/l7"
 	"github.com/coroot/coroot-node-agent/utils"
+	klog "github.com/sirupsen/logrus"
+	"math"
 	"net/url"
 	"sort"
 	"strconv"
@@ -109,8 +111,10 @@ type MapInfoT struct {
 	PureTime        uint64   `json:"pure_time"`
 	ServiceName     string   `json:"service_name"`
 	ServiceType     string   `json:"service_type"`
-	StartTime       uint64   `json:"start_time"`
-	EndTime         uint64   `json:"end_time"`
+	StartTime       uint64   `json:"-"`
+	EndTime         uint64   `json:"-"`
+	StartTimeMs     uint64   `json:"start_time"`
+	EndTimeMs       uint64   `json:"end_time"`
 	WallTime        uint64   `json:"wall_time"`
 	Schema          string   `json:"schema,omitempty"`
 	AssumedAppId    int64    `json:"assumed_app_id,omitempty"`
@@ -223,8 +227,8 @@ func tracetransformData(sdl []tracesdk.ReadOnlySpan) map[int][]RootDataT {
 	//}
 
 	//Transform the categorized map into a slice
-	aa, _ := json.Marshal(sendDataMap)
-	fmt.Println(string(aa))
+	data, _ := json.Marshal(sendDataMap)
+	klog.Info(string(data))
 	//fmt.Println(len(sendData))
 	//fmt.Println("sdl len:", len(sdl))
 	return sendDataMap
@@ -243,64 +247,64 @@ type TimeMap struct {
 //	TheEnd   bool
 //}
 
-func buildLevel(sdl *TraceMapT) {
-	nidMap := make(map[int]*MapInfoT)
-
-	mapSlice := []TimeMap{}
-
-	for i, v := range sdl.RootData.Maps {
-		if v.ServiceType == "APPLICATION" {
-			continue
-		}
-		nidMap[v.Nid] = &sdl.RootData.Maps[i]
-		timeStartMap := TimeMap{
-			Time: v.StartTime,
-			Type: 0,
-			Map:  &sdl.RootData.Maps[i],
-		}
-		mapSlice = append(mapSlice, timeStartMap)
-		timeEndMap := TimeMap{
-			Time: v.EndTime,
-			Type: 1,
-			Map:  &sdl.RootData.Maps[i],
-		}
-		mapSlice = append(mapSlice, timeEndMap)
-	}
-	sort.Slice(mapSlice, func(i, j int) bool {
-		return mapSlice[i].Time < mapSlice[j].Time
-	})
-
-	funStack := []TimeMap{}
-
-	currentNid := 1
-	Nid := 2
-	level := 2
-
-	for _, v := range mapSlice {
-		// fmt.Println("SliceSliceindex", k, "value", v.Time, v.Type, v.Map.MethodName, v.Map.Nid)
-		if v.Type == 0 {
-			// 函数入口
-			funStack = append(funStack, v)
-			v.Map.Pid = currentNid
-			v.Map.Level = level
-			v.Map.Nid = Nid
-			currentNid = Nid
-			level += 1
-			Nid += 1
-		} else if v.Type == 1 {
-			// 函数出口
-			len := len(funStack)
-			funStack = funStack[:len-1]
-			if (len - 2) < 0 {
-				currentNid = 1
-			} else {
-				currentNid = funStack[len-2].Map.Nid
-			}
-
-			level -= 1
-		}
-	}
-}
+//func buildLevel(sdl *TraceMapT) {
+//	nidMap := make(map[int]*MapInfoT)
+//
+//	mapSlice := []TimeMap{}
+//
+//	for i, v := range sdl.RootData.Maps {
+//		if v.ServiceType == "APPLICATION" {
+//			continue
+//		}
+//		nidMap[v.Nid] = &sdl.RootData.Maps[i]
+//		timeStartMap := TimeMap{
+//			Time: v.StartTime,
+//			Type: 0,
+//			Map:  &sdl.RootData.Maps[i],
+//		}
+//		mapSlice = append(mapSlice, timeStartMap)
+//		timeEndMap := TimeMap{
+//			Time: v.EndTime,
+//			Type: 1,
+//			Map:  &sdl.RootData.Maps[i],
+//		}
+//		mapSlice = append(mapSlice, timeEndMap)
+//	}
+//	sort.Slice(mapSlice, func(i, j int) bool {
+//		return mapSlice[i].Time < mapSlice[j].Time
+//	})
+//
+//	funStack := []TimeMap{}
+//
+//	currentNid := 1
+//	Nid := 2
+//	level := 2
+//
+//	for _, v := range mapSlice {
+//		// fmt.Println("SliceSliceindex", k, "value", v.Time, v.Type, v.Map.MethodName, v.Map.Nid)
+//		if v.Type == 0 {
+//			// 函数入口
+//			funStack = append(funStack, v)
+//			v.Map.Pid = currentNid
+//			v.Map.Level = level
+//			v.Map.Nid = Nid
+//			currentNid = Nid
+//			level += 1
+//			Nid += 1
+//		} else if v.Type == 1 {
+//			// 函数出口
+//			len := len(funStack)
+//			funStack = funStack[:len-1]
+//			if (len - 2) < 0 {
+//				currentNid = 1
+//			} else {
+//				currentNid = funStack[len-2].Map.Nid
+//			}
+//
+//			level -= 1
+//		}
+//	}
+//}
 
 func buildLevelFromEvent(sdl *RootDataT) {
 	nidMap := make(map[int]*MapInfoT)
@@ -504,41 +508,41 @@ func initRootDataJava() RootDataT {
 	return data
 }
 
-func initMapNode(spanSd *tracepb.Span) (MapInfoT, string) {
-	mNode := MapInfoT{
-		Exception:      0,
-		ExceptionMsg:   "",
-		ExceptionStack: "",
-		Ip:             "",
-		Level:          2,
-		Pid:            1,
-		Port:           0,
-		Ps:             []string{},
-		ServiceName:    "",
-		ServiceType:    "",
-		WallTime:       0,
-	}
-	mNode.MethodName = spanSd.Name
-	mNode.PureTime = (spanSd.EndTimeUnixNano - spanSd.StartTimeUnixNano) / 1e3
-	mNode.WallTime = mNode.PureTime
-	mNode.StartTime = spanSd.StartTimeUnixNano
-	mNode.EndTime = spanSd.EndTimeUnixNano
-
-	for _, attr := range spanSd.GetAttributes() {
-		fmt.Println(attr.Key, ":", attr.Value.GetValue())
-
-		switch attr.Key {
-		case "nid":
-			mNode.Nid = int(attr.Value.GetIntValue())
-		case "pid":
-			mNode.Pid = int(attr.Value.GetIntValue())
-		case "level":
-			mNode.Level = int(attr.Value.GetIntValue())
-		}
-	}
-
-	return mNode, spanSd.Name
-}
+//func initMapNode(spanSd *tracepb.Span) (MapInfoT, string) {
+//	mNode := MapInfoT{
+//		Exception:      0,
+//		ExceptionMsg:   "",
+//		ExceptionStack: "",
+//		Ip:             "",
+//		Level:          2,
+//		Pid:            1,
+//		Port:           0,
+//		Ps:             []string{},
+//		ServiceName:    "",
+//		ServiceType:    "",
+//		WallTime:       0,
+//	}
+//	mNode.MethodName = spanSd.Name
+//	mNode.PureTime = (spanSd.EndTimeUnixNano - spanSd.StartTimeUnixNano) / 1e3
+//	mNode.WallTime = mNode.PureTime
+//	mNode.StartTime = spanSd.StartTimeUnixNano
+//	mNode.EndTime = spanSd.EndTimeUnixNano
+//
+//	for _, attr := range spanSd.GetAttributes() {
+//		fmt.Println(attr.Key, ":", attr.Value.GetValue())
+//
+//		switch attr.Key {
+//		case "nid":
+//			mNode.Nid = int(attr.Value.GetIntValue())
+//		case "pid":
+//			mNode.Pid = int(attr.Value.GetIntValue())
+//		case "level":
+//			mNode.Level = int(attr.Value.GetIntValue())
+//		}
+//	}
+//
+//	return mNode, spanSd.Name
+//}
 
 func buildMapNodeFromEvent(event tracesdk.Event) MapInfoT {
 	mNode := MapInfoT{
@@ -569,9 +573,9 @@ func buildMapNodeFromEvent(event tracesdk.Event) MapInfoT {
 		case "level":
 			mNode.Level = int(attr.Value.AsInt64())
 		case "time.start_at":
-			mNode.StartTime = uint64(attr.Value.AsInt64())
+			mNode.StartTime, mNode.StartTimeMs = cleanNsTime(attr.Value.AsInt64())
 		case "time.end_at":
-			mNode.EndTime = uint64(attr.Value.AsInt64())
+			mNode.EndTime, mNode.EndTimeMs = cleanNsTime(attr.Value.AsInt64())
 		case "time.duration":
 			//mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
 			mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
@@ -740,10 +744,10 @@ func buildAppMapFromEvent(traceRoot *RootDataT, sd apmTraceSpan) int {
 		case "server.type_from":
 			traceRoot.TypeFrom = attr.Value.AsString()
 		case "time.start_at":
-			traceRoot.CollTime = uint64(attr.Value.AsInt64())
-			mNode.StartTime = traceRoot.CollTime
+			mNode.StartTime, mNode.StartTimeMs = cleanNsTime(attr.Value.AsInt64())
+			traceRoot.CollTime = mNode.StartTime
 		case "time.end_at":
-			mNode.EndTime = uint64(attr.Value.AsInt64())
+			mNode.EndTime, mNode.EndTimeMs = cleanNsTime(attr.Value.AsInt64())
 		case "time.duration":
 			traceRoot.RespTime = uint64(attr.Value.AsInt64()) / 1e3
 			mNode.PureTime = traceRoot.RespTime
@@ -801,8 +805,9 @@ func buildHttpMapFromEvent(mNode *MapInfoT, event tracesdk.Event) {
 	mNode.ServiceName = HTTP_SERVICE_NAME
 	mNode.ServiceType = HTTP_SERVICE_TYPE
 	mNode.Schema = "http"
-	mNode.MethodName = "HTTP"
+	//mNode.MethodName = "HTTP"
 	//var descAddr string
+	var method string
 	for _, attr := range event.Attributes {
 		fmt.Println("HTTP--->", attr.Key, ":", attr.Value.AsInterface())
 		switch attr.Key {
@@ -812,11 +817,12 @@ func buildHttpMapFromEvent(mNode *MapInfoT, event tracesdk.Event) {
 		case "http.port":
 			mNode.Port = attr.Value.AsInt64()
 		case "http.method":
-			mNode.MethodName += " " + attr.Value.AsString()
+			//mNode.MethodName += " " + attr.Value.AsString()
+			method = attr.Value.AsString()
 			//descAddr += ":" + attr.Value.AsString()
 		case "http.uri":
 			mNode.Uri = attr.Value.AsString()
-			mNode.MethodName += " " + attr.Value.AsString()
+			//mNode.MethodName += " " + attr.Value.AsString()
 		case "http.assumed_app_id":
 			mNode.AssumedAppId = attr.Value.AsInt64()
 		case "http.span_id":
@@ -834,6 +840,7 @@ func buildHttpMapFromEvent(mNode *MapInfoT, event tracesdk.Event) {
 			mNode.DestinationAddr = attr.Value.AsString()
 		}
 	}
+	mNode.MethodName = fmt.Sprintf("%s %s %s:%d%s", "HTTP", method, mNode.Ip, mNode.Port, mNode.Uri)
 	//mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
 }
 
@@ -1100,3 +1107,8 @@ func Md5ToInt64(strParam string, Len int) int64 {
 	}
 	return int64Data
 }
+
+// ns,ms
+func cleanNsTime(time int64) (uint64, uint64) {
+	return uint64(time), uint64(math.Round(float64(time) / 1e6))
+}