feat: more logging
This commit is contained in:
@@ -117,21 +117,43 @@ type FirmwareUpdateResponse struct {
|
||||
func (c *SporeClient) GetClusterStatus() (*ClusterStatusResponse, error) {
|
||||
url := fmt.Sprintf("%s/api/cluster/members", c.BaseURL)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"endpoint": "/api/cluster/members",
|
||||
}).Debug("Fetching cluster status from SPORE node")
|
||||
|
||||
resp, err := c.HTTPClient.Get(url)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to fetch cluster status from SPORE node")
|
||||
return nil, fmt.Errorf("failed to get cluster status: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"status_code": resp.StatusCode,
|
||||
}).Debug("Cluster status request returned non-OK status")
|
||||
return nil, fmt.Errorf("cluster status request failed with status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var clusterStatus ClusterStatusResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&clusterStatus); err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to decode cluster status response")
|
||||
return nil, fmt.Errorf("failed to decode cluster status response: %w", err)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"member_count": len(clusterStatus.Members),
|
||||
}).Debug("Successfully fetched cluster status from SPORE node")
|
||||
|
||||
return &clusterStatus, nil
|
||||
}
|
||||
|
||||
@@ -139,21 +161,44 @@ func (c *SporeClient) GetClusterStatus() (*ClusterStatusResponse, error) {
|
||||
func (c *SporeClient) GetTaskStatus() (*TaskStatusResponse, error) {
|
||||
url := fmt.Sprintf("%s/api/tasks/status", c.BaseURL)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"endpoint": "/api/tasks/status",
|
||||
}).Debug("Fetching task status from SPORE node")
|
||||
|
||||
resp, err := c.HTTPClient.Get(url)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to fetch task status from SPORE node")
|
||||
return nil, fmt.Errorf("failed to get task status: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"status_code": resp.StatusCode,
|
||||
}).Debug("Task status request returned non-OK status")
|
||||
return nil, fmt.Errorf("task status request failed with status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var taskStatus TaskStatusResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&taskStatus); err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to decode task status response")
|
||||
return nil, fmt.Errorf("failed to decode task status response: %w", err)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"total_tasks": taskStatus.Summary.TotalTasks,
|
||||
"active_tasks": taskStatus.Summary.ActiveTasks,
|
||||
}).Debug("Successfully fetched task status from SPORE node")
|
||||
|
||||
return &taskStatus, nil
|
||||
}
|
||||
|
||||
@@ -161,21 +206,44 @@ func (c *SporeClient) GetTaskStatus() (*TaskStatusResponse, error) {
|
||||
func (c *SporeClient) GetSystemStatus() (*SystemStatusResponse, error) {
|
||||
url := fmt.Sprintf("%s/api/node/status", c.BaseURL)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"endpoint": "/api/node/status",
|
||||
}).Debug("Fetching system status from SPORE node")
|
||||
|
||||
resp, err := c.HTTPClient.Get(url)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to fetch system status from SPORE node")
|
||||
return nil, fmt.Errorf("failed to get system status: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"status_code": resp.StatusCode,
|
||||
}).Debug("System status request returned non-OK status")
|
||||
return nil, fmt.Errorf("system status request failed with status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var systemStatus SystemStatusResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&systemStatus); err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to decode system status response")
|
||||
return nil, fmt.Errorf("failed to decode system status response: %w", err)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"free_heap": systemStatus.FreeHeap,
|
||||
"chip_id": systemStatus.ChipID,
|
||||
}).Debug("Successfully fetched system status from SPORE node")
|
||||
|
||||
return &systemStatus, nil
|
||||
}
|
||||
|
||||
@@ -183,21 +251,43 @@ func (c *SporeClient) GetSystemStatus() (*SystemStatusResponse, error) {
|
||||
func (c *SporeClient) GetCapabilities() (*CapabilitiesResponse, error) {
|
||||
url := fmt.Sprintf("%s/api/node/endpoints", c.BaseURL)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"endpoint": "/api/node/endpoints",
|
||||
}).Debug("Fetching capabilities from SPORE node")
|
||||
|
||||
resp, err := c.HTTPClient.Get(url)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to fetch capabilities from SPORE node")
|
||||
return nil, fmt.Errorf("failed to get capabilities: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"status_code": resp.StatusCode,
|
||||
}).Debug("Capabilities request returned non-OK status")
|
||||
return nil, fmt.Errorf("capabilities request failed with status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var capabilities CapabilitiesResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to decode capabilities response")
|
||||
return nil, fmt.Errorf("failed to decode capabilities response: %w", err)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"endpoint_count": len(capabilities.Endpoints),
|
||||
}).Debug("Successfully fetched capabilities from SPORE node")
|
||||
|
||||
return &capabilities, nil
|
||||
}
|
||||
|
||||
@@ -205,16 +295,30 @@ func (c *SporeClient) GetCapabilities() (*CapabilitiesResponse, error) {
|
||||
func (c *SporeClient) UpdateFirmware(firmwareData []byte, filename string) (*FirmwareUpdateResponse, error) {
|
||||
url := fmt.Sprintf("%s/api/node/update", c.BaseURL)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"endpoint": "/api/node/update",
|
||||
"filename": filename,
|
||||
"data_size": len(firmwareData),
|
||||
}).Debug("Preparing firmware upload to SPORE node")
|
||||
|
||||
// Create multipart form
|
||||
var requestBody bytes.Buffer
|
||||
contentType := createMultipartForm(&requestBody, firmwareData, filename)
|
||||
|
||||
if contentType == "" {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
}).Debug("Failed to create multipart form for firmware upload")
|
||||
return nil, fmt.Errorf("failed to create multipart form")
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, &requestBody)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to create firmware update request")
|
||||
return nil, fmt.Errorf("failed to create firmware update request: %w", err)
|
||||
}
|
||||
|
||||
@@ -226,9 +330,10 @@ func (c *SporeClient) UpdateFirmware(firmwareData []byte, filename string) (*Fir
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_ip": c.BaseURL,
|
||||
"status": "sending_firmware",
|
||||
}).Debug("Sending firmware to SPORE device")
|
||||
"node_url": c.BaseURL,
|
||||
"filename": filename,
|
||||
"data_size": len(firmwareData),
|
||||
}).Debug("Uploading firmware to SPORE node")
|
||||
|
||||
resp, err := firmwareClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -277,9 +382,19 @@ func (c *SporeClient) UpdateFirmware(firmwareData []byte, filename string) (*Fir
|
||||
func (c *SporeClient) UpdateNodeLabels(labels map[string]string) error {
|
||||
targetURL := fmt.Sprintf("%s/api/node/config", c.BaseURL)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"endpoint": "/api/node/config",
|
||||
"labels": labels,
|
||||
}).Debug("Updating node labels on SPORE node")
|
||||
|
||||
// Convert labels to JSON
|
||||
labelsJSON, err := json.Marshal(labels)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to marshal labels")
|
||||
return fmt.Errorf("failed to marshal labels: %w", err)
|
||||
}
|
||||
|
||||
@@ -289,6 +404,10 @@ func (c *SporeClient) UpdateNodeLabels(labels map[string]string) error {
|
||||
|
||||
req, err := http.NewRequest("POST", targetURL, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to create labels update request")
|
||||
return fmt.Errorf("failed to create labels update request: %w", err)
|
||||
}
|
||||
|
||||
@@ -296,19 +415,28 @@ func (c *SporeClient) UpdateNodeLabels(labels map[string]string) error {
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to update node labels")
|
||||
return fmt.Errorf("failed to update node labels: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"status_code": resp.StatusCode,
|
||||
"error_body": string(body),
|
||||
}).Debug("Node labels update returned non-OK status")
|
||||
return fmt.Errorf("node labels update failed with status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_ip": c.BaseURL,
|
||||
"labels": labels,
|
||||
}).Info("Node labels updated successfully")
|
||||
"node_url": c.BaseURL,
|
||||
"labels": labels,
|
||||
}).Debug("Successfully updated node labels on SPORE node")
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -318,17 +446,43 @@ func (c *SporeClient) ProxyCall(method, uri string, params map[string]interface{
|
||||
// Build target URL
|
||||
targetURL := fmt.Sprintf("%s%s", c.BaseURL, uri)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"method": method,
|
||||
"endpoint": uri,
|
||||
"param_count": len(params),
|
||||
}).Debug("Making proxy call to SPORE node")
|
||||
|
||||
// Parse parameters and build request
|
||||
req, err := c.buildProxyRequest(method, targetURL, params)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"method": method,
|
||||
"endpoint": uri,
|
||||
"error": err.Error(),
|
||||
}).Debug("Failed to build proxy request")
|
||||
return nil, fmt.Errorf("failed to build proxy request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"method": method,
|
||||
"endpoint": uri,
|
||||
"error": err.Error(),
|
||||
}).Debug("Proxy call failed")
|
||||
return nil, fmt.Errorf("proxy call failed: %w", err)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_url": c.BaseURL,
|
||||
"method": method,
|
||||
"endpoint": uri,
|
||||
"status_code": resp.StatusCode,
|
||||
}).Debug("Proxy call completed successfully")
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user