Преглед на файлове

Fixed #TASK_QT-9810 Header去除host id

Carl преди 1 година
родител
ревизия
d78ee67560

+ 2 - 0
cloudwise-apm-euspace.yaml

@@ -69,6 +69,8 @@ spec:
               value: 'false'
             - name: DISABLE_STACK_TRACING
               value: 'true'
+            - name: DISABLE_REG_HOST
+              value: 'false'
       volumes:
         - name: sys-fs-cgroup
           hostPath:

+ 6 - 3
containers/apm_register_app.go

@@ -27,19 +27,22 @@ func (c *Container) RegisterAppInfo(r *Registry, pid uint32) error {
 		}
 		c.AppInfo.ServiceName = c.AppInfo.CodeType.ServiceTypeString()
 		// 注册
-		hostId, _ := utils.GetHostID()
+		nodeInfo := r.nodeInfo.GetNodeInfo()
+		if nodeInfo == nil {
+			return fmt.Errorf("[register app] Unknown node info.")
+		}
 		registerAppReq := RegisterAppReq{
 			AppId:   c.AppInfo.AppIdHash.IntVal,
 			AppName: c.WhiteSettingInfo.AppName,
 			// todo AccountId
-			AccountId:   utils.GetAccountID(),
+			AccountId:   nodeInfo.AccountID,
+			HostId:      nodeInfo.HostID,
 			AgentId:     c.AppInfo.AgentId,
 			Sn:          c.AppInfo.Sn,
 			Sport:       c.AppInfo.Sport,
 			ServiceType: c.AppInfo.ServiceName,
 			CodeType:    c.AppInfo.CodeType.Int(),
 			App_type:    1,
-			HostId:      hostId,
 		}
 		klog.Infof("[register app] Register App req: %s.", registerAppReq.String())
 		err = r.connServer.RegisterApp(registerAppReq)

+ 1 - 2
containers/apm_white_list.go

@@ -3,7 +3,6 @@ package containers
 import (
 	"fmt"
 	"github.com/coroot/coroot-node-agent/common"
-	"github.com/coroot/coroot-node-agent/utils"
 	. "github.com/coroot/coroot-node-agent/utils/modelse"
 	log "github.com/sirupsen/logrus"
 )
@@ -37,7 +36,7 @@ func (r *Registry) getWhiteList() (bool, error) {
 	}
 	whiteListReq := WhiteListReq{
 		HostId:    nodeInfo.HostID,
-		AccountId: utils.GetAccountID(),
+		AccountId: nodeInfo.AccountID,
 	}
 	whiteData, err := r.connServer.WhiteList(whiteListReq)
 

+ 1 - 1
ebpftracer/jvm.go

@@ -123,7 +123,7 @@ func (t *Tracer) AttachJavaNioReadUprobes(pid uint32, codeType CodeType, rootfs
 				if err != nil {
 					return nil, fmt.Errorf("failed to attach uprobe_ret_Java_sun_nio_ch_FileDispatcherImpl_read0 uprobe")
 				}
-				klog.Infof("[attach] java symbol offset is  %s", offset)
+				klog.Infof("[attach] java symbol offset is  %d", offset)
 				links = append(links, l)
 			}
 		}

+ 1 - 1
ebpftracer/stack.go

@@ -59,7 +59,7 @@ func (t *Tracer) AttachJVMStackUprobes(pid uint32, appInfo AppInfo) ([]link.Link
 		fmt.Sprintf("%s=%s", filepath.Join(nativeBasePath, "lib", "apmAgent.jar"), nativeBasePath),
 		fmt.Sprintf("%s=%d", "appId", appInfo.AppIdHash.IntVal),
 		fmt.Sprintf("%s=%d", "agentId", appInfo.AgentId),
-		fmt.Sprintf("%s=%d", "hostId", utils.GetIntHostID()),
+		fmt.Sprintf("%s=%d", "hostId", utils.GetHostID()),
 		fmt.Sprintf("%s=%d", "accountId", utils.GetAccountID()),
 	}
 	argkv := strings.Join(kvPairs, ",")

+ 2 - 2
ebpftracer/tracer/socket.go

@@ -156,7 +156,7 @@ func set_offset_map(collectionSpec *ebpf.CollectionSpec, opts *ebpf.CollectionOp
 // __u64 io_event_collect_mode;
 // __u64 io_event_minimal_duration;
 func set_conf_map_default(collectionSpec *ebpf.CollectionSpec, opts *ebpf.CollectionOptions) {
-	_, charHostID := utils.GetHostID()
+	//charHostID := utils.GetHostIDBPFString()
 	//_, charAppID := utils.GetAppID()
 
 	uidBase := uint64(time.Now().UnixNano()/int64(time.Millisecond)) & 0xffffffffffffff
@@ -173,7 +173,7 @@ func set_conf_map_default(collectionSpec *ebpf.CollectionSpec, opts *ebpf.Collec
 			GoTracingTimeout:       120,
 			IOEventCollectMode:     1,
 			IOEventMinimalDuartion: 1000000,
-			HostID:                 charHostID,
+			//HostID:                 utils.GetHostIDBPFString(),
 			//APPID:                  charAppID,
 			TotalCpus: uint64(nCPU),
 		}

+ 3 - 1
node/apm_host_info.go

@@ -30,10 +30,11 @@ func NewNodeInfo(name, kv string) (*NodeInfoT, error) {
 		HostIp:        ip,
 		KernelVersion: kv,
 		SystemUUID:    utils.SystemUUID(),
-		HostID:        utils.GetIntHostID(),
+		HostID:        utils.GetHostID(),
 		AccountID:     utils.GetAccountID(),
 		LicenseKey:    *flags.LicenseKey,
 	}
+	utils.SaveNodeInfo(n)
 	klog.Infof("run standalone.")
 	return n, nil
 }
@@ -71,6 +72,7 @@ func newNodeInfoFromCommonIni(name, kv string) (*NodeInfoT, error) {
 					AccountID:     accountId,
 					LicenseKey:    *flags.LicenseKey,
 				}
+				utils.SaveNodeInfo(n)
 				return n, nil
 			}
 		}

+ 3 - 2
pkg/go.opentelemetry.io/otel/exporters/otlp/otlptrace/apm_exporter.go

@@ -391,10 +391,11 @@ func buildLevelFromEvent(sdl *RootDataT) {
 //}
 
 func initRootDataFromEvent() RootDataT {
-	hostID, _ := utils.GetHostID()
+	hostID := utils.GetHostID()
+	accountID := utils.GetAccountID()
 	data := RootDataT{
 		// todo AccountId
-		AccountId:      110,
+		AccountId:      accountID,
 		AgentId:        0, // 基于 ip:port + process_name + exe路径生成
 		AgentVersion:   "2.1.0",
 		AppId:          0, // 基于appname生成

+ 51 - 35
utils/id.go

@@ -5,33 +5,25 @@ import (
 	"fmt"
 	. "github.com/coroot/coroot-node-agent/utils/modelse"
 	"math"
+	"math/big"
 	"os"
 	"path"
 	"strconv"
 	"strings"
 )
 
-func init() {
-
+func SaveNodeInfo(n *NodeInfoT) {
+	if n != nil {
+		NODE_INFO = n
+	}
 }
 
-var (
-	APP_ID_INT64      int64
-	HOST_ID_INT64     int64
-	ACCOUNT_ID_INT    int
-	INSTANCE_ID_INT64 int64
-
-	APP_ID_BYTE      HashByte
-	HOST_ID_BYTE     HashByte
-	INSTANCE_ID_BYTE HashByte
-)
-
 func BuildInt64ID(str string) ID_STRING {
-	srcCode := md5.Sum([]byte(str))
-	code := fmt.Sprintf("%x", srcCode)
-
-	id_string := md5ToDec(code)
-	return ID_STRING(id_string)
+	//srcCode := md5.Sum([]byte(str))
+	//code := fmt.Sprintf("%x", srcCode)
+	//id_string := md5ToDec(code)
+	//return ID_STRING(id_string)
+	return ID_STRING(hashTo16DigitString(str))
 }
 
 func hexStringToBPFBytes(str string, out *HashByte) {
@@ -46,17 +38,16 @@ func hexStringToBPFBytes(str string, out *HashByte) {
 
 // todo account id
 func GetAccountID() int {
-	if ACCOUNT_ID_INT != 0 {
-		return ACCOUNT_ID_INT
+	if NODE_INFO != nil && NODE_INFO.AccountID != 0 {
+		return NODE_INFO.AccountID
 	}
-	ACCOUNT_ID_INT = 110
-	return ACCOUNT_ID_INT
+	NODE_INFO.AccountID = 110
+	return NODE_INFO.AccountID
 }
 
-// todo HostId
-func GetHostID() (int64, HashByte) {
-	if HOST_ID_INT64 != 0 {
-		return HOST_ID_INT64, HOST_ID_BYTE
+func GetHostID() int64 {
+	if NODE_INFO != nil && NODE_INFO.HostID != 0 {
+		return NODE_INFO.HostID
 	}
 	uuid := SystemUUID()
 	// todo AccountId
@@ -66,33 +57,39 @@ func GetHostID() (int64, HashByte) {
 	host_id_string := md5ToDec(code)
 	id, err := strconv.ParseInt(host_id_string, 10, 64)
 	if err != nil {
-		return 0, HashByte{}
+		return 0
 	}
 
-	HOST_ID_INT64 = id
+	NODE_INFO.HostID = id
 
-	var charArray HashByte
-	hexStringToBPFBytes(host_id_string, &charArray)
-	HOST_ID_BYTE = charArray
+	//var charArray HashByte
+	//hexStringToBPFBytes(host_id_string, &charArray)
+	//HOST_ID_BYTE = charArray
 	// 将rune切片复制到char数组中
 	//for i := 0; i < HASH_SIZE; i++ {
 	//	charArray[i] = []byte(host_id_string)[i]
 	//}
-	return id, charArray
+	return id
 }
 
-func GetIntHostID() int64 {
-	id, _ := GetHostID()
-	return id
+func GetHostIDBPFString() HashByte {
+	host_id_string := strconv.FormatInt(GetHostID(), 10)
+	var charArray HashByte
+	hexStringToBPFBytes(host_id_string, &charArray)
+	return charArray
 }
 
 func SystemUUID() string {
+	if NODE_INFO != nil && NODE_INFO.SystemUUID != "" {
+		return NODE_INFO.SystemUUID
+	}
 	for _, p := range []string{"sys/devices/virtual/dmi/id/product_uuid", "etc/machine-id", "var/lib/dbus/machine-id"} {
 		payload, err := os.ReadFile(path.Join("/proc/1/root", p))
 		if err != nil {
 			continue
 		}
 		id := strings.TrimSpace(strings.Replace(string(payload), "\n", "", -1))
+		NODE_INFO.SystemUUID = id
 		return id
 	}
 	return ""
@@ -113,3 +110,22 @@ func md5ToDec(str string) string {
 	}
 	return strCode
 }
+
+func hashTo16DigitString(appName string) string {
+	// 计算 MD5 哈希
+	hash := md5.Sum([]byte(appName))
+	// 将哈希值转换为一个大整数
+	bigInt := new(big.Int).SetBytes(hash[:])
+	// 转换为十进制字符串
+	intStr := bigInt.String()
+	// 如果结果少于 16 位,补零
+	if len(intStr) < 16 {
+		intStr = fmt.Sprintf("%016s", intStr) // 左侧补零
+		// 如果第一个字符是 0,将其替换为 1
+		if intStr[0] == '0' {
+			intStr = "1" + intStr[1:]
+		}
+	}
+	// 截取前 16 位
+	return intStr[:16]
+}

+ 8 - 0
utils/modelse/node.go

@@ -4,6 +4,10 @@ import (
 	"sync"
 )
 
+var (
+	NODE_INFO *NodeInfoT
+)
+
 type NodeInfoT struct {
 	Hostname      string       `json:"hostname"`
 	HostIp        string       `json:"host_ip"`
@@ -19,6 +23,10 @@ type NodeInfoInterface interface {
 	GetNodeInfo() *NodeInfoT
 }
 
+func init() {
+	NODE_INFO = new(NodeInfoT)
+}
+
 func (n *NodeInfoT) GetNodeInfo() *NodeInfoT {
 	n.Lock.Lock()
 	defer n.Lock.Unlock()