Bladeren bron

Fixed #JiraBug28171 应用周期注册

Carl 1 jaar geleden
bovenliggende
commit
fd748fa92d
3 gewijzigde bestanden met toevoegingen van 71 en 25 verwijderingen
  1. 37 20
      containers/apm_register_app.go
  2. 6 5
      containers/container.go
  3. 28 0
      containers/container_apm.go

+ 37 - 20
containers/apm_register_app.go

@@ -14,12 +14,18 @@ func (c *Container) RegisterAppInfo(r *Registry, pid uint32) error {
 	}
 	var err error
 	originAppName := c.AppInfo.AppName
+	originAppId := c.AppInfo.AppIdHash.IntVal
+	originInstanceId := c.AppInfo.InstanceIdHash.IntVal
 	whiteAppName := c.WhiteSettingInfo.AppName
-	if originAppName != whiteAppName {
+	reRegFlag := c.checkReRegTime(time.Minute)
+	if originAppName != whiteAppName || reRegFlag {
+		cl := "first "
+		if reRegFlag {
+			cl = "re "
+		}
 		if c.AppInfo.CodeType.IsUnknownCode() || c.AppInfo.CodeType.IsWaitCheck() {
-			return fmt.Errorf("[register app] Unknown app code[%s],Wait buildIDs.", c.AppInfo.CodeType.String())
+			return fmt.Errorf("[%sregister app] Unknown app code[%s],Wait buildIDs.", cl, c.AppInfo.CodeType.String())
 		}
-		//c.AppInfo.AppId, err = utils.BuildInt64ID(whiteAppName).ToInt64()
 		c.AppInfo.AppIdHash.IntVal, err = utils.BuildInt64ID(whiteAppName).ToInt64()
 		c.AppInfo.AppIdHash.HashtVal = utils.BuildInt64ID(whiteAppName).ToHashByte()
 		if err != nil {
@@ -29,12 +35,14 @@ func (c *Container) RegisterAppInfo(r *Registry, pid uint32) error {
 		// 注册
 		nodeInfo := r.nodeInfo.GetNodeInfo()
 		if nodeInfo == nil {
-			return fmt.Errorf("[register app] Unknown node info.")
+			return fmt.Errorf("[%sregister app] Unknown node info.", cl)
+		}
+		if reRegFlag {
+			c.ReBuildIds(pid)
 		}
 		registerAppReq := RegisterAppReq{
-			AppId:   c.AppInfo.AppIdHash.IntVal,
-			AppName: c.WhiteSettingInfo.AppName,
-			// todo AccountId
+			AppId:       c.AppInfo.AppIdHash.IntVal,
+			AppName:     c.WhiteSettingInfo.AppName,
 			AccountId:   nodeInfo.AccountID,
 			HostId:      nodeInfo.HostID,
 			AgentId:     c.AppInfo.AgentId,
@@ -45,29 +53,38 @@ func (c *Container) RegisterAppInfo(r *Registry, pid uint32) error {
 			App_type:    1,
 			AppLang:     "ebpf",
 		}
-		klog.Infof("[register app] Register App req: %s.", registerAppReq.String())
+		klog.Infof("[%sregister app] Register App req: %s.", cl, registerAppReq.String())
 		err = r.connServer.RegisterApp(registerAppReq)
 		if err != nil {
-			klog.WithError(err).Errorf("[register] Failed Register App %s.", registerAppReq.String())
+			klog.WithError(err).Errorf("[%sregister app] Failed Register App %s.", cl, registerAppReq.String())
 			return err
 		}
 
+		c.AppInfo.RegisterAt = time.Now().Unix()
 		c.AppInfo.AppName = c.WhiteSettingInfo.AppName
 
-		if originAppName == "" {
-			c.AppInfo.RegisterAt = time.Now().Unix()
-		}
-
-		if originAppName != "" && c.Isl7AttachSuccess() {
-			klog.Infoln("[kprobe] reset kernel proc_info.")
-			err = r.tracer.InitKProcInfo(pid, &c.AppInfo)
-			if err != nil {
-				klog.WithError(err).Errorf("[kprobe] Failed reset KProcInfo.")
-				return err
+		// reset kernel
+		if c.Isl7AttachSuccess() {
+			if (originAppId != c.AppInfo.AppIdHash.IntVal && originAppId != 0) ||
+				(originInstanceId != c.AppInfo.InstanceIdHash.IntVal && originInstanceId != 0) {
+				klog.Infoln("[kprobe] reset kernel proc_info.")
+				err = r.tracer.InitKProcInfo(pid, &c.AppInfo)
+				if err != nil {
+					klog.WithError(err).Errorf("[kprobe] Failed reset KProcInfo.")
+					return err
+				}
+				c.AppInfo.UpdateAt = time.Now().Unix()
 			}
-			c.AppInfo.UpdateAt = time.Now().Unix()
 		}
 	}
 	c.AppInfo.SetAppSuccess()
 	return nil
 }
+
+func (c *Container) checkReRegTime(duration time.Duration) bool {
+	if c.AppInfo.RegisterAt != 0 {
+		elapsed := time.Since(time.Unix(c.AppInfo.RegisterAt, 0))
+		return elapsed > duration
+	}
+	return false
+}

+ 6 - 5
containers/container.go

@@ -96,10 +96,10 @@ type ActiveConnection struct {
 	BytesReceived    uint64
 	PerBytesReceived uint64
 
-	ConEstTime       time.Duration
-	FirstReadTime    uint64
-	FirstWriteTime   uint64
-	NewReadTime      uint64
+	ConEstTime     time.Duration
+	FirstReadTime  uint64
+	FirstWriteTime uint64
+	NewReadTime    uint64
 
 	http2Parser    *l7.Http2Parser
 	postgresParser *l7.PostgresParser
@@ -179,7 +179,7 @@ type Container struct {
 	connectsSuccessful map[AddrPair]*ConnectionStats // dst:actual_dst -> count
 	// connectsFailed     map[netaddr.IPPort]int64      // dst -> count
 	connectsFailed     map[AddrPair]int64
-	connectLastAttempt map[netaddr.IPPort]time.Time  // dst -> time
+	connectLastAttempt map[netaddr.IPPort]time.Time // dst -> time
 	connectionsActive  map[AddrPair]*ActiveConnection
 	connectionsByPidFd map[PidFd]*ActiveConnection
 
@@ -219,6 +219,7 @@ type Container struct {
 	WhiteSettingInfo WhiteSettingInfo
 	// 应用详情
 	AppInfo AppInfo
+	RegTime int
 }
 
 func NewContainer(id ContainerID, cg *cgroup.Cgroup, md *ContainerMetadata, hostConntrack *Conntrack, pid uint32, registry *Registry) (*Container, error) {

+ 28 - 0
containers/container_apm.go

@@ -332,6 +332,34 @@ func (c *Container) buildIDs(pid uint32) bool {
 	return false
 }
 
+func (c *Container) ReBuildIds(pid uint32) {
+	c.lock.Lock()
+	defer c.lock.Unlock()
+	var sns []string
+	var sport uint16
+	for address, val := range c.getListens() {
+		if val == 1 {
+			ip := address.IP()
+			if ip.Is4() && !ip.IsLoopback() {
+				// 获取端口号
+				sport = address.Port()
+				sns = append(sns, fmt.Sprintf("%s:%d", ip, sport))
+			}
+		}
+	}
+	if len(sns) > 0 {
+		snsStr := strings.Join(sns, ",")
+		c.AppInfo.Sn = snsStr
+		c.AppInfo.Sport = int(sport)
+		strInstanceID := utils.BuildInt64ID(fmt.Sprintf("%s:%d", c.AppInfo.Sn, sport))
+		c.AppInfo.InstanceIdHash.IntVal, _ = strInstanceID.ToInt64()
+		c.AppInfo.InstanceIdHash.HashtVal = strInstanceID.ToHashByte()
+		strAgentID := utils.BuildInt64ID(fmt.Sprintf("%s:%s", utils.GetHostIP(), string(proc.GetExe(pid))))
+		c.AppInfo.AgentId, _ = strAgentID.ToInt64()
+		c.AppInfo.CodeType = c.GetCodeTypeFromCache(pid)
+	}
+}
+
 func (c *Container) StackProcess(event ebpftracer.StackEvent, tracer *ebpftracer.Tracer) {
 	c.lock.Lock()
 	defer c.lock.Unlock()