Skip to content

Instantly share code, notes, and snippets.

@Rizary
Last active September 18, 2020 12:46
Show Gist options
  • Save Rizary/9c28506b998ea44dbbd08bf51d2c2911 to your computer and use it in GitHub Desktop.
Save Rizary/9c28506b998ea44dbbd08bf51d2c2911 to your computer and use it in GitHub Desktop.
trying to create an async function in Rust
async fn docker_json_to_smth(arg: (Docker, &ContainerSummary)) -> () {
let (docker, csummary) = arg;
let container = docker
.inspect_container(
csummary.id.as_ref().unwrap(),
None::<InspectContainerOptions>,
)
.await
.unwrap();
let container_name = get_container_name(container.clone());
let mut pcontainer = PContainerInfo::new();
pcontainer.labels.job = container_name.clone();
pcontainer.labels.name = container_name.clone();
pcontainer.labels.id = get_container_hostname(container.clone());
let docker_labels = match container.clone().config.and_then(|e| e.labels) {
Some(x) => x,
_ => HashMap::new(),
};
if !docker_labels.is_empty() {
match get_scrape_enabled(docker_labels.clone()).unwrap_or(false) {
true => {
let job_name = get_config_job(docker_labels.clone());
debug!("Container {} is enabled for prometheus.", container_name);
if let true = !job_name.is_empty() {
pcontainer.labels.job = job_name.clone();
debug!("Set job name to {}.", job_name.clone())
}
debug!("Job name is not set, using default value.")
}
false => debug!(
"Container {} has no \"prometheus-scrape.enabled\" label and is ignored.",
container_name
),
}
} else {
error!("Docker doesn't have labels")
}
let port = get_config_port(docker_labels.clone());
let hostname = get_config_hostname(docker_labels.clone(), container_name.clone());
let target = format!("{}:{}", hostname, port);
pcontainer.targets.push(target);
pcontainer.labels.scheme = get_config_scheme(docker_labels.clone());
pcontainer.labels.metrics_path = get_config_metrics_path(docker_labels.clone());
pcontainer.labels.com_docker_compose_service =
get_config_docker_compose_service(docker_labels.clone());
println!("{:#?}", pcontainer)
}
#[derive(Debug)]
struct PContainerLabel {
job: String,
name: String,
id: String,
scheme: String,
metrics_path: String,
com_docker_compose_service: String,
}
impl Default for PContainerLabel {
fn default() -> Self {
PContainerLabel {
job: String::new(),
name: String::new(),
id: String::new(),
scheme: String::new(),
metrics_path: String::new(),
com_docker_compose_service: String::new(),
}
}
}
impl PContainerLabel {
fn new() -> Self {
Default::default()
}
}
#[derive(Debug)]
struct PContainerInfo {
// labels for container information
labels: PContainerLabel,
targets: Vec<String>,
}
impl PContainerInfo {
fn new() -> Self {
Default::default()
}
}
impl Default for PContainerInfo {
fn default() -> Self {
PContainerInfo {
labels: PContainerLabel::new(),
targets: Vec::new(),
}
}
}
fn get_config_metrics_path(hash: HashMap<String, String, RandomState>) -> String {
let config_port = &String::from("prometheus-scrape.metrics_path");
let metrics_path = String::from("/metrics");
if let Some(new_path) = hash.get(config_port).map(|e| e.to_string()) {
debug!("Port is set to {}.", new_path);
return new_path;
}
debug!("Job name is not set, using default value.");
return metrics_path;
}
fn get_container_name(ctr: ContainerInspectResponse) -> String {
match ctr.name {
Some(x) => x
.strip_prefix("/")
.map(|n| n.to_string())
.unwrap_or(String::from("")),
_ => String::from(""),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment