修复服务器收到数据后提示400的错误
This commit is contained in:
@@ -3,8 +3,8 @@
|
|||||||
"id": "yunc",
|
"id": "yunc",
|
||||||
"name": "cloud",
|
"name": "cloud",
|
||||||
"device_id": "yunc",
|
"device_id": "yunc",
|
||||||
"token": "0b1f00e76e28beaed3be71d13e25aceb",
|
"token": "bedb47554f06ebabc075b52d8320428f",
|
||||||
"interval": "10s",
|
"interval": "10s",
|
||||||
"debug": true,
|
"debug": true,
|
||||||
"api_port": 8082
|
"api_port": 8081
|
||||||
}
|
}
|
||||||
@@ -108,15 +108,6 @@ func HandleMetricsPost(c *gin.Context) {
|
|||||||
agentName = device.Name
|
agentName = device.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析请求体
|
|
||||||
var req MetricsRequest
|
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
|
||||||
"error": "Invalid request body",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新设备状态为active
|
// 更新设备状态为active
|
||||||
if err := deviceStorage.UpdateDeviceStatus(deviceID, "active"); err != nil {
|
if err := deviceStorage.UpdateDeviceStatus(deviceID, "active"); err != nil {
|
||||||
// 只记录警告,不影响指标处理
|
// 只记录警告,不影响指标处理
|
||||||
@@ -128,6 +119,25 @@ func HandleMetricsPost(c *gin.Context) {
|
|||||||
"agent_name": agentName,
|
"agent_name": agentName,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理请求体,支持单指标对象和指标数组
|
||||||
|
var metricsList []MetricsRequest
|
||||||
|
|
||||||
|
// 首先尝试解析为数组
|
||||||
|
if err := c.ShouldBindJSON(&metricsList); err != nil {
|
||||||
|
// 如果解析数组失败,尝试解析为单个对象
|
||||||
|
var singleMetric MetricsRequest
|
||||||
|
if err := c.ShouldBindJSON(&singleMetric); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
"error": "Invalid request body",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 将单个对象添加到列表中
|
||||||
|
metricsList = append(metricsList, singleMetric)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理所有指标
|
||||||
|
for i, req := range metricsList {
|
||||||
// 写入CPU指标
|
// 写入CPU指标
|
||||||
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "cpu", req.CPU, baseTags); err != nil {
|
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "cpu", req.CPU, baseTags); err != nil {
|
||||||
// 只记录警告,不影响后续指标处理
|
// 只记录警告,不影响后续指标处理
|
||||||
@@ -170,7 +180,8 @@ func HandleMetricsPost(c *gin.Context) {
|
|||||||
log.Printf("Warning: Failed to write network received metrics: %v", err)
|
log.Printf("Warning: Failed to write network received metrics: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 广播指标更新消息
|
// 广播指标更新消息,只广播最后一个指标
|
||||||
|
if i == len(metricsList)-1 {
|
||||||
metrics := map[string]interface{}{
|
metrics := map[string]interface{}{
|
||||||
"cpu": req.CPU,
|
"cpu": req.CPU,
|
||||||
"memory": req.Memory,
|
"memory": req.Memory,
|
||||||
@@ -181,10 +192,13 @@ func HandleMetricsPost(c *gin.Context) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
broadcastMetricsUpdate(deviceID, metrics)
|
broadcastMetricsUpdate(deviceID, metrics)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 返回成功响应
|
// 返回成功响应
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "Metrics received successfully",
|
"message": "Metrics received successfully",
|
||||||
|
"count": len(metricsList),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user