apm_register_app.go 3.4 KB

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