apm_register_app.go 3.3 KB

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