added error handling in metrics handle
This commit is contained in:
@@ -159,43 +159,58 @@ impl DockerManager {
|
|||||||
|
|
||||||
let container_infos: Vec<DockerCollectMetricDto> = container_infos_total
|
let container_infos: Vec<DockerCollectMetricDto> = container_infos_total
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|info| DockerCollectMetricDto {
|
.filter_map(|info| {
|
||||||
id: Some(info.container.unwrap().id).unwrap_or("".to_string()),
|
// Safely handle container extraction
|
||||||
cpu: info
|
let container = match info.container {
|
||||||
.cpu
|
Some(c) => c,
|
||||||
.unwrap()
|
None => {
|
||||||
.cpu_usage_percent
|
eprintln!("Warning: Container info missing container data, skipping");
|
||||||
.map(|load| DockerContainerCpuDto {
|
return None;
|
||||||
cpu_load: Some(load),
|
}
|
||||||
})
|
};
|
||||||
.unwrap_or(DockerContainerCpuDto { cpu_load: None }),
|
|
||||||
ram: info
|
// Safely handle CPU data with defaults
|
||||||
.ram
|
let cpu_dto = if let Some(cpu) = info.cpu {
|
||||||
.unwrap()
|
DockerContainerCpuDto {
|
||||||
.memory_usage_percent
|
cpu_load: cpu.cpu_usage_percent,
|
||||||
.map(|load| DockerContainerRamDto {
|
}
|
||||||
cpu_load: Some(load),
|
} else {
|
||||||
})
|
DockerContainerCpuDto { cpu_load: None }
|
||||||
.unwrap_or(DockerContainerRamDto { cpu_load: None }),
|
};
|
||||||
network: DockerContainerNetworkDto {
|
|
||||||
net_in: info
|
// Safely handle RAM data with defaults
|
||||||
.network
|
let ram_dto = if let Some(ram) = info.ram {
|
||||||
.as_ref()
|
DockerContainerRamDto {
|
||||||
.unwrap()
|
ram_load: ram.memory_usage_percent,
|
||||||
.rx_bytes
|
}
|
||||||
.map(|bytes| bytes as f64)
|
} else {
|
||||||
.or(Some(0.0)),
|
DockerContainerRamDto { ram_load: None }
|
||||||
net_out: info
|
};
|
||||||
.network
|
|
||||||
.unwrap()
|
// Safely handle network data with defaults
|
||||||
.tx_bytes
|
let network_dto = if let Some(net) = info.network {
|
||||||
.map(|bytes| bytes as f64)
|
DockerContainerNetworkDto {
|
||||||
.or(Some(0.0)),
|
net_in: net.rx_bytes.map(|bytes| bytes as f64).or(Some(0.0)),
|
||||||
},
|
net_out: net.tx_bytes.map(|bytes| bytes as f64).or(Some(0.0)),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DockerContainerNetworkDto {
|
||||||
|
net_in: Some(0.0),
|
||||||
|
net_out: Some(0.0),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(DockerCollectMetricDto {
|
||||||
|
id: container.id,
|
||||||
|
cpu: cpu_dto,
|
||||||
|
ram: ram_dto,
|
||||||
|
network: network_dto,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let dto = DockerMetricDto {
|
let dto = DockerMetricDto {
|
||||||
server_id: 0,
|
server_id: 0, // This should be set by the caller
|
||||||
containers: serde_json::to_string(&container_infos)?,
|
containers: serde_json::to_string(&container_infos)?,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -207,9 +222,9 @@ impl DockerManager {
|
|||||||
) -> Result<DockerRegistrationDto, Box<dyn Error + Send + Sync>> {
|
) -> Result<DockerRegistrationDto, Box<dyn Error + Send + Sync>> {
|
||||||
let containers = self.get_containers().await?;
|
let containers = self.get_containers().await?;
|
||||||
let dto = DockerRegistrationDto {
|
let dto = DockerRegistrationDto {
|
||||||
server_id: 0,
|
server_id: 0, // This will be set by the caller
|
||||||
//container_count,
|
containers: serde_json::to_string(&containers)
|
||||||
containers: serde_json::to_string(&containers)?,
|
.unwrap_or_else(|_| "[]".to_string()), // Fallback to empty array
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(dto)
|
Ok(dto)
|
||||||
|
|||||||
@@ -153,7 +153,13 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
|||||||
let docker_manager = docker_manager.as_ref().cloned().unwrap();
|
let docker_manager = docker_manager.as_ref().cloned().unwrap();
|
||||||
async move {
|
async move {
|
||||||
let mut collector = metrics::Collector::new(server_id, ip, docker_manager);
|
let mut collector = metrics::Collector::new(server_id, ip, docker_manager);
|
||||||
collector.run(&server_url).await
|
if let Err(e) = collector.run(&server_url).await {
|
||||||
|
eprintln!("Metrics collection error: {}", e);
|
||||||
|
// Don't panic, just return the error
|
||||||
|
Err(e)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -235,7 +235,7 @@ pub struct DockerContainerCpuDto {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct DockerContainerRamDto {
|
pub struct DockerContainerRamDto {
|
||||||
pub cpu_load: Option<f64>,
|
pub ram_load: Option<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
|
|||||||
Reference in New Issue
Block a user