apm_register_app.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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("[register app] Unknown app code[%s],Wait buildIDs.", c.AppInfo.CodeType.String())
  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.ServiceType = c.AppInfo.CodeType.ServiceTypeString()
  27. // 注册
  28. nodeInfo := r.nodeInfo.GetNodeInfo()
  29. if nodeInfo == nil {
  30. return fmt.Errorf("[register app] Unknown node info.")
  31. }
  32. registerAppReq := RegisterAppReq{
  33. AppId: c.AppInfo.AppIdHash.IntVal,
  34. AppName: c.WhiteSettingInfo.AppName,
  35. // todo AccountId
  36. AccountId: nodeInfo.AccountID,
  37. HostId: nodeInfo.HostID,
  38. AgentId: c.AppInfo.AgentId,
  39. Sn: c.AppInfo.Sn,
  40. Sport: c.AppInfo.Sport,
  41. ServiceType: c.AppInfo.ServiceType,
  42. CodeType: c.AppInfo.CodeType.Int(),
  43. App_type: 1,
  44. AppLang: "ebpf",
  45. }
  46. klog.Infof("[register app] Register App req: %s.", registerAppReq.String())
  47. err = r.connServer.RegisterApp(registerAppReq)
  48. if err != nil {
  49. klog.WithError(err).Errorf("[register] Failed Register App %s.", registerAppReq.String())
  50. return err
  51. }
  52. c.AppInfo.AppName = c.WhiteSettingInfo.AppName
  53. if originAppName == "" {
  54. c.AppInfo.RegisterAt = time.Now().Unix()
  55. }
  56. if originAppName != "" && c.Isl7AttachSuccess() {
  57. klog.Infoln("[kprobe] reset kernel proc_info.")
  58. err = r.tracer.InitKProcInfo(pid, &c.AppInfo)
  59. if err != nil {
  60. klog.WithError(err).Errorf("[kprobe] Failed reset KProcInfo.")
  61. return err
  62. }
  63. c.AppInfo.UpdateAt = time.Now().Unix()
  64. }
  65. }
  66. c.AppInfo.SetAppSuccess()
  67. return nil
  68. }