Explorar o código

Fixed #TASK_QT-9810 nativeAgent优化:只加载一次NativeSo

Carl hai 1 ano
pai
achega
cda9191a2d

+ 2 - 1
containers/apm_register_app.go

@@ -17,7 +17,7 @@ func (c *Container) RegisterAppInfo(r *Registry, pid uint32) error {
 	whiteAppName := c.WhiteSettingInfo.AppName
 	if originAppName != whiteAppName {
 		if c.AppInfo.CodeType.IsUnknownCode() || c.AppInfo.CodeType.IsWaitCheck() {
-			return fmt.Errorf("[register app] Unknown app code,Wait buildIDs.")
+			return fmt.Errorf("[register app] Unknown app code[%s],Wait buildIDs.", c.AppInfo.CodeType.String())
 		}
 		//c.AppInfo.AppId, err = utils.BuildInt64ID(whiteAppName).ToInt64()
 		c.AppInfo.AppIdHash.IntVal, err = utils.BuildInt64ID(whiteAppName).ToInt64()
@@ -64,5 +64,6 @@ func (c *Container) RegisterAppInfo(r *Registry, pid uint32) error {
 			c.AppInfo.UpdateAt = time.Now().Unix()
 		}
 	}
+	c.AppInfo.AppSuccess()
 	return nil
 }

+ 2 - 2
containers/container_apm.go

@@ -542,8 +542,8 @@ func (c *Container) detachUprobes(pid uint32) {
 			p.DynamicClose()
 			// 关闭7层监控
 			c.l7Attach = false
-			// 清空应用注册信息
-			c.AppInfo = AppInfo{}
+			// 变更应用为卸载状态
+			c.AppInfo.AppUninstall()
 		}
 	}
 }

+ 21 - 4
containers/process.go

@@ -2,8 +2,13 @@ package containers
 
 import (
 	"context"
+	"fmt"
+	"github.com/coroot/coroot-node-agent/ebpftracer/tracer/jattach"
+	"github.com/coroot/coroot-node-agent/utils"
 	. "github.com/coroot/coroot-node-agent/utils/modelse"
 	"os"
+	"path/filepath"
+	"strings"
 	"time"
 
 	"github.com/jpillora/backoff"
@@ -87,12 +92,24 @@ func (p *Process) DynamicClose() {
 	p.goTlsUprobesChecked = false
 	p.openSslUprobesChecked = false
 	p.jvmUprobesChecked = false
-	// TODO 移除 tiny-agent
 	if p.codeType.IsJvmCode() && p.stackUprobesChecked {
-
+		nativeBasePath := utils.GetDefaultAgentsPath("NativeAgent")
+		kvPairs := []string{
+			fmt.Sprintf("%s=%s", filepath.Join(nativeBasePath, "lib", "apmAgent.jar"), nativeBasePath),
+			"UNINSTALL",
+		}
+		argkv := strings.Join(kvPairs, ",")
+		args := []string{"load", "instrument", "false", argkv}
+		jattacher := jattach.JvmJattacher{
+			Pid:         p.Pid,
+			Args:        args,
+			PrintOutput: 1,
+		}
+		_, err := jattacher.JDetach()
+		if err == nil {
+			p.stackUprobesChecked = false
+		}
 	}
-
-	p.stackUprobesChecked = false
 	p.uprobes = []link.Link{}
 }
 

+ 4 - 1
containers/registry.go

@@ -238,6 +238,7 @@ func (r *Registry) handleEvents(ch <-chan ebpftracer.Event) {
 							Sn:         c.AppInfo.Sn,
 							Sport:      c.AppInfo.Sport,
 							RegisterAt: time.Unix(c.AppInfo.RegisterAt, 0).Format("060102 15:04:05"),
+							Status:     c.AppInfo.Status,
 						}
 						if c.AppInfo.UpdateAt != 0 {
 							detail.UpdateAt = time.Unix(c.AppInfo.UpdateAt, 0).Format("060102 15:04:05")
@@ -534,6 +535,8 @@ func (r *Registry) getOrCreateContainer(pid uint32) *Container {
 }
 
 func calcId(cg *cgroup.Cgroup, md *ContainerMetadata, pid uint32) (ContainerID, map[string]string) {
+	// 卡一下防止概率性获取为bash
+	time.Sleep(1 * time.Millisecond)
 	extensionTag := map[string]string{Namespace: "", Workload: "", PodName: "", ProcessName: ""}
 	if cg.ContainerType == cgroup.ContainerTypeSystemdService {
 		if strings.HasPrefix(cg.ContainerId, "/system.slice/crio-conmon-") {
@@ -544,7 +547,7 @@ func calcId(cg *cgroup.Cgroup, md *ContainerMetadata, pid uint32) (ContainerID,
 	if cg.ContainerType == cgroup.ContainerTypeStandaloneProcess {
 		procName := proc.GetProcName(pid)
 		extensionTag[ProcessName] = procName
-		return ContainerID(fmt.Sprintf("/%s/%s/%d", "standalone", proc.GetProcName(pid), pid)), extensionTag
+		return ContainerID(fmt.Sprintf("/%s/%s/%d", "standalone", procName, pid)), extensionTag
 	}
 	switch cg.ContainerType {
 	case cgroup.ContainerTypeDocker, cgroup.ContainerTypeContainerd, cgroup.ContainerTypeSandbox, cgroup.ContainerTypeCrio:

BIN=BIN
dist/package_dir/agents/NativeAgent/lib/apmAgent.jar


+ 22 - 2
ebpftracer/tracer/jattach/jattach_linux_amd64.go

@@ -14,6 +14,7 @@ import (
 	"fmt"
 	"github.com/coroot/coroot-node-agent/utils"
 	"github.com/coroot/coroot-node-agent/utils/enums"
+	klog "github.com/sirupsen/logrus"
 	"os"
 	"strings"
 )
@@ -35,10 +36,10 @@ func (j *JvmJattacher) JAttach() (int, error) {
 	if err != nil {
 		return -1, err
 	}
-	if strings.Contains(result, "JVM response code = 0") {
+	if strings.Contains(result, "JVM response code = 0\n0") {
 		return 0, nil
 	} else {
-		return -1, fmt.Errorf("JVM returned %s", result)
+		return -1, fmt.Errorf("jattach jvm returned %s", result)
 	}
 
 	//
@@ -56,3 +57,22 @@ func (j *JvmJattacher) JAttach() (int, error) {
 	//}
 	//return int(result), nil
 }
+
+func (j *JvmJattacher) JDetach() (int, error) {
+	// cmd方式
+	cwJattachCmdPath := utils.GetDefaultScriptsPath("cwjattach")
+	// 执行升级
+	args := []string{fmt.Sprintf("%d", j.Pid)}
+	args = append(args, j.Args...)
+	result, err := utils.AuthoritativeCMD(enums.RootUser, enums.DefaultTimeOut, os.Environ(), cwJattachCmdPath, args...)
+
+	if err != nil {
+		klog.WithError(err).Errorf("Failed to detach JVM process")
+		return -1, err
+	}
+	if strings.Contains(result, "JVM response code = 0\n0") {
+		return 0, nil
+	} else {
+		return -1, fmt.Errorf("detach JVM returned %s", result)
+	}
+}

+ 20 - 0
ebpftracer/tracer/jattach/jattach_linux_arm64.go

@@ -14,6 +14,7 @@ import (
 	"fmt"
 	"github.com/coroot/coroot-node-agent/utils"
 	"github.com/coroot/coroot-node-agent/utils/enums"
+	klog "github.com/sirupsen/logrus"
 	"os"
 	"strings"
 )
@@ -56,3 +57,22 @@ func (j *JvmJattacher) JAttach() (int, error) {
 	//}
 	//return int(result), nil
 }
+
+func (j *JvmJattacher) JDetach() (int, error) {
+	// cmd方式
+	cwJattachCmdPath := utils.GetDefaultScriptsPath("cwjattach")
+	// 执行升级
+	args := []string{fmt.Sprintf("%d", j.Pid)}
+	args = append(args, j.Args...)
+	result, err := utils.AuthoritativeCMD(enums.RootUser, enums.DefaultTimeOut, os.Environ(), cwJattachCmdPath, args...)
+
+	if err != nil {
+		klog.WithError(err).Errorf("Failed to detach JVM process")
+		return -1, err
+	}
+	if strings.Contains(result, "JVM response code = 0") {
+		return 0, nil
+	} else {
+		return -1, fmt.Errorf("JVM returned %s", result)
+	}
+}

+ 3 - 1
flags/flags.go

@@ -156,6 +156,7 @@ func DumpTableFeatures() {
 			//info.AgentID,
 			info.RegisterAt,
 			info.UpdateAt,
+			info.Status.String(),
 		})
 	}
 	t.SetAutoIndex(true)
@@ -167,8 +168,9 @@ func DumpTableFeatures() {
 		"service",
 		"app id",
 		//"agent id",
-		"register at",
+		"first register at",
 		"update at",
+		"status",
 	})
 	fmt.Println(t.Render())
 	os.Exit(0)

+ 38 - 0
utils/modelse/app_info.go

@@ -1,5 +1,7 @@
 package modelse
 
+import "time"
+
 const (
 	APP_NAME    = "app_name"
 	APP_ID      = "app_id"
@@ -7,6 +9,25 @@ const (
 	INSTANCE_ID = "instance_id"
 )
 
+type APP_TYPE int
+
+const (
+	APP_WAIT APP_TYPE = iota
+	APP_SUCCESS
+	APP_UNINSTALL
+)
+
+func (s APP_TYPE) String() string {
+	switch s {
+	case APP_SUCCESS:
+		return "SUCCESS"
+	case APP_UNINSTALL:
+		return "UNINSTALL"
+	default:
+		return "APP_WAIT"
+	}
+}
+
 type AppInfo struct {
 	AppName        string        `json:"app_name"`
 	AppIdHash      INT_HASH_ID   `json:"app_id_hash"`
@@ -19,4 +40,21 @@ type AppInfo struct {
 	EBPFProcInfo   *EbpfProcInfo `json:"ebpf_proc_info"`
 	RegisterAt     int64         `json:"register_at"`
 	UpdateAt       int64         `json:"update_at"`
+	Status         APP_TYPE      `json:"status"`
+}
+
+func (a *AppInfo) UpdateAtTime() {
+	a.UpdateAt = time.Now().Unix()
+}
+
+func (a *AppInfo) AppSuccess() {
+	if a.Status != APP_SUCCESS {
+		a.Status = APP_SUCCESS
+		a.UpdateAtTime()
+	}
+}
+
+func (a *AppInfo) AppUninstall() {
+	a.Status = APP_UNINSTALL
+	a.UpdateAtTime()
 }

+ 12 - 11
utils/modelse/status.go

@@ -1,15 +1,16 @@
 package modelse
 
 type AppStatusInfo struct {
-	Pid        uint32 `json:"pid"`
-	ProcName   string `json:"proc_name"`
-	AppName    string `json:"app_name"`
-	Language   string `json:"language"`
-	AppID      int64  `json:"app_id"`
-	AgentID    int64  `json:"agent_id,omitempty"`
-	InstanceID int64  `json:"instance_id,omitempty"`
-	Sn         string `json:"sn"`
-	Sport      int    `json:"sport"`
-	RegisterAt string `json:"register_at"`
-	UpdateAt   string `json:"update_at"`
+	Pid        uint32   `json:"pid"`
+	ProcName   string   `json:"proc_name"`
+	AppName    string   `json:"app_name"`
+	Language   string   `json:"language"`
+	AppID      int64    `json:"app_id"`
+	AgentID    int64    `json:"agent_id,omitempty"`
+	InstanceID int64    `json:"instance_id,omitempty"`
+	Sn         string   `json:"sn"`
+	Sport      int      `json:"sport"`
+	RegisterAt string   `json:"register_at"`
+	UpdateAt   string   `json:"update_at"`
+	Status     APP_TYPE `json:"status"`
 }