apm_register_app.go 3.3 KB

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