Browse Source

Feature #TASK_QT-18250 解析accountID

Carl 8 tháng trước cách đây
mục cha
commit
01ecd58ea5
5 tập tin đã thay đổi với 41 bổ sung19 xóa
  1. 11 3
      containers/apm_register_host.go
  2. 4 4
      flags/flags.go
  3. 5 4
      utils/id.go
  4. 5 0
      utils/modelse/models.go
  5. 16 8
      utils/worker/serverWorker.go

+ 11 - 3
containers/apm_register_host.go

@@ -2,10 +2,11 @@ package containers
 
 import (
 	"fmt"
-	. "github.com/coroot/coroot-node-agent/utils/modelse"
-	klog "github.com/sirupsen/logrus"
 	"runtime"
 	"time"
+
+	. "github.com/coroot/coroot-node-agent/utils/modelse"
+	klog "github.com/sirupsen/logrus"
 )
 
 func (r *Registry) RegisterHost() error {
@@ -27,10 +28,17 @@ func (r *Registry) RegisterHost() error {
 		Ebpf:          true,
 		AgentType:     2020,
 	}
-	err := r.connServer.RegisterHost(req)
+	accountID, err := r.connServer.RegisterHost(req)
 	if err != nil {
 		return err
 	}
+
+	// 更新nodeInfo中的AccountID
+	if r.nodeInfo != nil {
+		r.nodeInfo.AccountID = accountID
+		klog.Infof("[register host] Updated account_id to %d", accountID)
+	}
+
 	return nil
 }
 

+ 4 - 4
flags/flags.go

@@ -39,10 +39,10 @@ var (
 	DisableL7Tracing  = kingpin.Flag("disable-l7-tracing", "Disable L7 tracing").Default("false").Envar("DISABLE_L7_TRACING").Bool()
 
 	ExternalNetworksWhitelist = kingpin.
-		Flag("track-public-network", "Allow track connections to the specified IP networks, all private networks are allowed by default (e.g., Y.Y.Y.Y/mask)").
-		Envar("TRACK_PUBLIC_NETWORK").
-		Default("0.0.0.0/0").
-		Strings()
+					Flag("track-public-network", "Allow track connections to the specified IP networks, all private networks are allowed by default (e.g., Y.Y.Y.Y/mask)").
+					Envar("TRACK_PUBLIC_NETWORK").
+					Default("0.0.0.0/0").
+					Strings()
 	EphemeralPortRange = kingpin.Flag("ephemeral-port-range", "Destination and Listen TCP ports from this range will be skipped").Default("").Envar("EPHEMERAL_PORT_RANGE").String()
 
 	Provider          = kingpin.Flag("provider", "`provider` label for `node_cloud_info` metric").Envar("PROVIDER").String()

+ 5 - 4
utils/id.go

@@ -3,13 +3,14 @@ package utils
 import (
 	"crypto/md5"
 	"fmt"
-	. "github.com/coroot/coroot-node-agent/utils/modelse"
 	"math"
 	"math/big"
 	"os"
 	"path"
 	"strconv"
 	"strings"
+
+	. "github.com/coroot/coroot-node-agent/utils/modelse"
 )
 
 func SaveNodeInfo(n *NodeInfoT) {
@@ -36,13 +37,13 @@ func hexStringToBPFBytes(str string, out *HashByte) {
 	}
 }
 
-// todo account id
+// GetAccountID 获取账户ID,如果未注册则返回0
 func GetAccountID() int {
 	if NODE_INFO != nil && NODE_INFO.AccountID != 0 {
 		return NODE_INFO.AccountID
 	}
-	NODE_INFO.AccountID = 110
-	return NODE_INFO.AccountID
+	// 如果未注册,返回0,等待注册完成后更新
+	return 0
 }
 
 func GetHostIP() string {

+ 5 - 0
utils/modelse/models.go

@@ -112,6 +112,11 @@ type RegisterHostReq struct {
 	AgentType     int    `json:"agent_type"`
 }
 
+// RegisterHostResponse 注册主机响应结构
+type RegisterHostResponse struct {
+	AccountID int `json:"account_id"`
+}
+
 type RegisterAppReq struct {
 	AppId       int64  `json:"appId"`
 	AppName     string `json:"appName"`

+ 16 - 8
utils/worker/serverWorker.go

@@ -3,12 +3,13 @@ package worker
 import (
 	"encoding/json"
 	"fmt"
+	"net/http"
+	"sync"
+
 	"github.com/coroot/coroot-node-agent/flags"
 	"github.com/coroot/coroot-node-agent/utils"
 	. "github.com/coroot/coroot-node-agent/utils/modelse"
 	log "github.com/sirupsen/logrus"
-	"net/http"
-	"sync"
 	//
 	//log "github.com/sirupsen/logrus"
 )
@@ -29,7 +30,7 @@ const (
 type ServerWorker interface {
 	//InstallReport(r ReportRequest) error
 
-	RegisterHost(RegisterHostReq) error
+	RegisterHost(RegisterHostReq) (int, error)
 
 	RegisterApp(RegisterAppReq) error
 
@@ -85,16 +86,23 @@ func (w *ServerHTTPWorker) PullAllAppInfo(request EbpfAppReq) (EbpfAppResp, erro
 	return response, nil
 }
 
-func (w *ServerHTTPWorker) RegisterHost(request RegisterHostReq) error {
+func (w *ServerHTTPWorker) RegisterHost(request RegisterHostReq) (int, error) {
 	//log.Infof("[server register host] request:%v.", utils.ToString(request))
-	//result, err := w.requestServer("/v2/app/registerHost", request)
-	_, err := w.requestServer("/v2/app/registerHost", request)
+	result, err := w.requestServer("/v2/app/registerHost", request)
 	//log.Infof("[server register host] resp:%v.", string(result))
 	if err != nil {
 		log.WithError(err).Errorf("[server register] Failed RegisterHost request:%v.", utils.ToString(request))
-		return err
+		return 0, err
 	}
-	return nil
+
+	var response RegisterHostResponse
+	err = json.Unmarshal(result, &response)
+	if err != nil {
+		log.WithError(err).Errorf("[server register] Failed to parse RegisterHost response: %s", string(result))
+		return 0, err
+	}
+	log.Infof("[server register] RegisterHost success, account_id: %d", response.AccountID)
+	return response.AccountID, nil
 }
 
 func (w *ServerHTTPWorker) RegisterApp(request RegisterAppReq) error {