apm_register_app.go 1.9 KB

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