apm_register_app.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package containers
  2. import (
  3. "fmt"
  4. "github.com/coroot/coroot-node-agent/utils"
  5. . "github.com/coroot/coroot-node-agent/utils/modelse"
  6. klog "github.com/sirupsen/logrus"
  7. "time"
  8. )
  9. func (c *Container) RegisterAppInfo(r *Registry, pid uint32) error {
  10. if r.isFusing {
  11. return fmt.Errorf("euspace in fusing can not to regist app")
  12. }
  13. if c.WhiteSettingInfo.AppName == "" {
  14. return fmt.Errorf("appname is empty.")
  15. }
  16. var err error
  17. originAppName := c.AppInfo.AppName
  18. originAppId := c.AppInfo.AppIdHash.IntVal
  19. originInstanceId := c.AppInfo.InstanceIdHash.IntVal
  20. whiteAppName := c.WhiteSettingInfo.AppName
  21. reRegFlag := c.checkReRegTime(RegisterAppInterval)
  22. if originAppName != whiteAppName || reRegFlag {
  23. cl := "first "
  24. if reRegFlag {
  25. cl = "re "
  26. }
  27. if c.AppInfo.CodeType.IsUnknownCode() || c.AppInfo.CodeType.IsWaitCheck() {
  28. return fmt.Errorf("[%sregister app] Unknown app code[%s],Wait buildIDs.", cl, c.AppInfo.CodeType.String())
  29. }
  30. c.AppInfo.AppIdHash.IntVal, err = utils.BuildInt64ID(whiteAppName).ToInt64()
  31. c.AppInfo.AppIdHash.HashtVal = utils.BuildInt64ID(whiteAppName).ToHashByte()
  32. if err != nil {
  33. return err
  34. }
  35. c.AppInfo.ServiceType = c.AppInfo.CodeType.ServiceTypeString()
  36. // 注册
  37. nodeInfo := r.nodeInfo.GetNodeInfo()
  38. if nodeInfo == nil {
  39. return fmt.Errorf("[%sregister app] Unknown node info.", cl)
  40. }
  41. // if reRegFlag {
  42. c.ReBuildIds(pid)
  43. // }
  44. registerAppReq := RegisterAppReq{
  45. AppId: c.AppInfo.AppIdHash.IntVal,
  46. AppName: c.WhiteSettingInfo.AppName,
  47. AccountId: nodeInfo.AccountID,
  48. HostId: nodeInfo.HostID,
  49. AgentId: c.AppInfo.AgentId,
  50. Sn: c.AppInfo.Sn,
  51. Sport: c.AppInfo.Sport,
  52. ServiceType: c.AppInfo.ServiceType,
  53. CodeType: c.AppInfo.CodeType.Int(),
  54. App_type: 1,
  55. AppLang: "ebpf",
  56. }
  57. //klog.Infof("[%sregister app] Register App req: %s.", cl, registerAppReq.String())
  58. klog.Infof("[%sregister app] Register App", cl)
  59. err = r.connServer.RegisterApp(registerAppReq)
  60. if err != nil {
  61. klog.WithError(err).Errorf("[%sregister app] Failed Register App %s.", cl, registerAppReq.String())
  62. return err
  63. }
  64. c.AppInfo.RegisterAt = time.Now().Unix()
  65. c.AppInfo.AppName = c.WhiteSettingInfo.AppName
  66. // reset kernel
  67. if c.Isl7AttachSuccess() {
  68. if (originAppId != c.AppInfo.AppIdHash.IntVal && originAppId != 0) ||
  69. (originInstanceId != c.AppInfo.InstanceIdHash.IntVal && originInstanceId != 0) {
  70. klog.Infoln("[kprobe] reset kernel proc_info.")
  71. err = r.tracer.InitKProcInfo(pid, &c.AppInfo)
  72. if err != nil {
  73. klog.WithError(err).Errorf("[kprobe] Failed reset KProcInfo.")
  74. return err
  75. }
  76. c.AppInfo.UpdateAt = time.Now().Unix()
  77. }
  78. }
  79. }
  80. c.AppInfo.SetAppSuccess()
  81. return nil
  82. }
  83. func (c *Container) checkReRegTime(duration time.Duration) bool {
  84. if c.AppInfo.RegisterAt != 0 {
  85. elapsed := time.Since(time.Unix(c.AppInfo.RegisterAt, 0))
  86. return elapsed > duration
  87. }
  88. return false
  89. }