apm_register_app.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 c.WhiteSettingInfo.AppName == "" {
  11. return fmt.Errorf("appname is empty.")
  12. }
  13. var err error
  14. originAppName := c.AppInfo.AppName
  15. whiteAppName := c.WhiteSettingInfo.AppName
  16. if originAppName != whiteAppName {
  17. if c.AppInfo.CodeType.IsUnknownCode() || c.AppInfo.CodeType.IsWaitCheck() {
  18. return fmt.Errorf("unknown app code")
  19. }
  20. //c.AppInfo.AppId, err = utils.BuildInt64ID(whiteAppName).ToInt64()
  21. c.AppInfo.AppIdHash.IntVal, err = utils.BuildInt64ID(whiteAppName).ToInt64()
  22. c.AppInfo.AppIdHash.HashtVal = utils.BuildInt64ID(whiteAppName).ToHashByte()
  23. if err != nil {
  24. return err
  25. }
  26. c.AppInfo.ServiceName = c.AppInfo.CodeType.ServiceTypeString()
  27. // 注册
  28. hostId, _ := utils.GetHostID()
  29. registerAppReq := RegisterAppReq{
  30. AppId: c.AppInfo.AppIdHash.IntVal,
  31. AppName: c.WhiteSettingInfo.AppName,
  32. // todo AccountId
  33. AccountId: utils.GetAccountID(),
  34. AgentId: c.AppInfo.AgentId,
  35. Sn: c.AppInfo.Sn,
  36. Sport: c.AppInfo.Sport,
  37. ServiceType: c.AppInfo.ServiceName,
  38. CodeType: c.AppInfo.CodeType.Int(),
  39. App_type: 1,
  40. HostId: hostId,
  41. }
  42. klog.Infof("[register] Register App: %s.", registerAppReq.String())
  43. err = r.connServer.RegisterApp(registerAppReq)
  44. if err != nil {
  45. klog.WithError(err).Errorf("[register] Failed Register App %s.", registerAppReq.String())
  46. return err
  47. }
  48. c.AppInfo.AppName = c.WhiteSettingInfo.AppName
  49. if originAppName == "" {
  50. c.AppInfo.RegisterAt = time.Now().Unix()
  51. }
  52. if originAppName != "" && c.checkL7AttachReady() {
  53. klog.Infoln("[kprobe] reset kernel proc_info.")
  54. err = r.tracer.InitKProcInfo(pid, &c.AppInfo)
  55. if err != nil {
  56. klog.WithError(err).Errorf("[kprobe] Failed reset KProcInfo.")
  57. return err
  58. }
  59. c.AppInfo.UpdateAt = time.Now().Unix()
  60. }
  61. }
  62. return nil
  63. }