feat: enhance logging for agent registration, heartbeat, and configuration sync processes

This commit is contained in:
ryan
2026-03-10 14:55:35 +08:00
parent f396c8c74e
commit e7dc18e6ca
7 changed files with 215 additions and 80 deletions
+30 -1
View File
@@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"log"
"net/http"
"strings"
"time"
@@ -29,6 +30,7 @@ func New(baseURL string, token string, timeout time.Duration) *Client {
}
func (c *Client) RegisterNode(ctx context.Context, payload protocol.NodePayload) error {
log.Printf("http register node request: node_id=%s current_version=%s", payload.NodeID, payload.CurrentVersion)
return c.postJSON(ctx, "/api/agent/nodes/register", payload, nil)
}
@@ -37,6 +39,7 @@ func (c *Client) Heartbeat(ctx context.Context, payload protocol.NodePayload) er
}
func (c *Client) GetActiveConfig(ctx context.Context) (*protocol.ActiveConfigResponse, error) {
log.Printf("http get active config request")
resp := protocol.APIResponse[protocol.ActiveConfigResponse]{}
if err := c.getJSON(ctx, "/api/agent/config-versions/active", &resp); err != nil {
return nil, err
@@ -44,10 +47,12 @@ func (c *Client) GetActiveConfig(ctx context.Context) (*protocol.ActiveConfigRes
if !resp.Success {
return nil, errors.New(resp.Message)
}
log.Printf("http get active config response: version=%s checksum=%s support_files=%d", resp.Data.Version, resp.Data.Checksum, len(resp.Data.SupportFiles))
return &resp.Data, nil
}
func (c *Client) ReportApplyLog(ctx context.Context, payload protocol.ApplyLogPayload) error {
log.Printf("http report apply log request: node_id=%s version=%s result=%s", payload.NodeID, payload.Version, payload.Result)
return c.postJSON(ctx, "/api/agent/apply-logs", payload, nil)
}
@@ -75,23 +80,47 @@ func (c *Client) postJSON(ctx context.Context, path string, body any, target any
}
func (c *Client) do(req *http.Request, target any) error {
if !isHeartbeatRequest(req) {
log.Printf("http request start: method=%s path=%s", req.Method, req.URL.Path)
}
res, err := c.httpClient.Do(req)
if err != nil {
log.Printf("http request failed: method=%s path=%s error=%v", req.Method, req.URL.Path, err)
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
log.Printf("http request returned non-200: method=%s path=%s status=%s", req.Method, req.URL.Path, res.Status)
return errors.New(res.Status)
}
if target == nil {
var wrapper protocol.APIResponse[json.RawMessage]
if err = json.NewDecoder(res.Body).Decode(&wrapper); err != nil {
log.Printf("http response decode failed: method=%s path=%s error=%v", req.Method, req.URL.Path, err)
return err
}
if !wrapper.Success {
log.Printf("http api response failed: method=%s path=%s message=%s", req.Method, req.URL.Path, wrapper.Message)
return errors.New(wrapper.Message)
}
if !isHeartbeatRequest(req) {
log.Printf("http request succeeded: method=%s path=%s", req.Method, req.URL.Path)
}
return nil
}
return json.NewDecoder(res.Body).Decode(target)
if err = json.NewDecoder(res.Body).Decode(target); err != nil {
log.Printf("http response decode failed: method=%s path=%s error=%v", req.Method, req.URL.Path, err)
return err
}
if !isHeartbeatRequest(req) {
log.Printf("http request succeeded: method=%s path=%s", req.Method, req.URL.Path)
}
return nil
}
func isHeartbeatRequest(req *http.Request) bool {
if req == nil || req.URL == nil {
return false
}
return req.Method == http.MethodPost && req.URL.Path == "/api/agent/nodes/heartbeat"
}