瀏覽代碼

Feature #JiraBug29576 应用通过ebpg注入javaagent后,升级了ebpf探针,未重启应用的前提下采集不到业务堆栈

Carl 1 年之前
父節點
當前提交
9289530a8c
共有 4 個文件被更改,包括 52 次插入5 次删除
  1. 1 0
      containers/registry.go
  2. 49 4
      ebpftracer/stack.go
  3. 1 0
      flags/flags.go
  4. 1 1
      main.go

+ 1 - 0
containers/registry.go

@@ -351,6 +351,7 @@ func (r *Registry) handleEvents(ch <-chan ebpftracer.Event) {
 					}
 					if common.IsOpenFilter() && common.IsFilterPid(e.Pid) {
 						c.WhiteSettingInfo.AppName = enums.TestApp
+						c.WhiteSettingInfo.WhiteStackSettingInfo.WhiteList = os.Getenv("WHITE_LIST")
 						err := c.RegisterAppInfo(r, e.Pid)
 						if err != nil {
 							klog.WithError(err).Errorf("[registry] Failed registerAppInfo. pid is %d", e.Pid)

+ 49 - 4
ebpftracer/stack.go

@@ -12,7 +12,9 @@ import (
 	klog "github.com/sirupsen/logrus"
 	"os"
 	"path/filepath"
+	"strconv"
 	"strings"
+	"syscall"
 )
 
 // AttachStackUprobes
@@ -139,7 +141,6 @@ func (t *Tracer) AttachJVMStackUprobes(pid uint32, appInfo AppInfo) ([]link.Link
 }
 
 func FindNativeSoFromMapped(pid uint32, prefix, suffix string) (string, error) {
-	// todo rootfs
 	mapsFile := fmt.Sprintf("/proc/%d/maps", pid)
 	tmpFile, err := os.Open(mapsFile)
 	if err != nil {
@@ -147,15 +148,33 @@ func FindNativeSoFromMapped(pid uint32, prefix, suffix string) (string, error) {
 	}
 	defer tmpFile.Close()
 
-	// 使用 bufio.Scanner 逐行读取文件内容
+	var selectedPath string
+	var maxTimestamp int64
+	fallbackPaths := make(map[string]struct{}) // 使用 map 去重
+
 	scanner := bufio.NewScanner(tmpFile)
 	for scanner.Scan() {
 		line := scanner.Text()
-		// 检查路径部分是否包含 "NativeAgentSo" 且以 ".tmp" 结尾
 		if strings.Contains(line, prefix) && strings.HasSuffix(line, suffix) {
 			parts := strings.Fields(line)
 			if len(parts) > 5 {
-				return parts[len(parts)-1], nil // 返回路径
+				path := parts[len(parts)-1]
+				baseName := filepath.Base(path)
+
+				if strings.HasPrefix(baseName, prefix) && strings.HasSuffix(baseName, suffix) {
+					middle := strings.TrimSuffix(strings.TrimPrefix(baseName, prefix), suffix)
+					segments := strings.Split(middle, ".")
+					if len(segments) >= 2 {
+						timestampStr := segments[len(segments)-1]
+						timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
+						if err == nil && timestamp > maxTimestamp {
+							maxTimestamp = timestamp
+							selectedPath = path
+						}
+					} else {
+						fallbackPaths[path] = struct{}{}
+					}
+				}
 			}
 		}
 	}
@@ -164,5 +183,31 @@ func FindNativeSoFromMapped(pid uint32, prefix, suffix string) (string, error) {
 		return "", fmt.Errorf("error reading maps file: %v", err)
 	}
 
+	if selectedPath != "" {
+		return selectedPath, nil
+	}
+
+	var latestPath string
+	var latestModTime int64
+	for path := range fallbackPaths {
+		info, err := os.Stat(path)
+		if err != nil {
+			continue
+		}
+		stat, ok := info.Sys().(*syscall.Stat_t)
+		if !ok {
+			continue
+		}
+		modTime := stat.Mtim.Sec
+		if modTime > latestModTime {
+			latestModTime = modTime
+			latestPath = path
+		}
+	}
+
+	if latestPath != "" {
+		return latestPath, nil
+	}
+
 	return "", fmt.Errorf("no matching path found")
 }

+ 1 - 0
flags/flags.go

@@ -21,6 +21,7 @@ var (
 	DumpApps            = kingpin.Flag("dump", "Dump app snap").Default("false").Bool()
 	Version             = kingpin.Flag("version", "show app version").Short('v').Bool()
 	LogLevel            = kingpin.Flag("log-level", "Log level").Envar("LOG_LEVEL").Default("info").String()
+	ConsoleLog          = kingpin.Flag("console-log", "Console log").Envar("CONSOLE_LOG").Default("false").Bool()
 	EbpfFilePath        = kingpin.Flag("ebpf-path", "Set ebpf file path").Envar("EBPF_FILE").Default("").String()
 	CommonIni           = kingpin.Flag("common.ini", "Set ebpf file path").Envar("COMMON_INI").Default("/opt/cloudwise/omniagent/conf/common.ini").String()
 	ServerPrefix        = kingpin.Flag("server-prefix", "server-prefix").Envar("SERVER_PREFIX").Default("").String()

+ 1 - 1
main.go

@@ -141,7 +141,7 @@ func main() {
 		MaxSize:    50, // 日志文件最大尺寸,单位MB
 		MaxBackups: 3,  // 最多保留的旧日志文件数
 		MaxAge:     3,  // 日志文件保留的最长时间,单位天
-		Console:    true,
+		Console:    *flags.ConsoleLog,
 	})
 
 	if err != nil {