2024-04-12 00:34:46 -07:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use clap::{Parser, ValueEnum};
|
|
|
|
|
use krata::{
|
|
|
|
|
events::EventStream,
|
|
|
|
|
v1::{
|
2024-07-18 20:47:18 -07:00
|
|
|
common::ZoneMetricNode,
|
|
|
|
|
control::{control_service_client::ControlServiceClient, ReadZoneMetricsRequest},
|
2024-04-12 00:34:46 -07:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use tonic::transport::Channel;
|
|
|
|
|
|
|
|
|
|
use crate::format::{kv2line, metrics_flat, metrics_tree, proto2dynamic};
|
|
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
use super::resolve_zone;
|
2024-04-12 00:34:46 -07:00
|
|
|
|
|
|
|
|
#[derive(ValueEnum, Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
enum MetricsFormat {
|
|
|
|
|
Tree,
|
|
|
|
|
Json,
|
|
|
|
|
JsonPretty,
|
|
|
|
|
Yaml,
|
|
|
|
|
KeyValue,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Parser)]
|
2024-07-18 20:47:18 -07:00
|
|
|
#[command(about = "Read metrics from the zone")]
|
2024-04-12 00:34:46 -07:00
|
|
|
pub struct MetricsCommand {
|
|
|
|
|
#[arg(short, long, default_value = "tree", help = "Output format")]
|
|
|
|
|
format: MetricsFormat,
|
2024-07-18 20:47:18 -07:00
|
|
|
#[arg(help = "Zone to read metrics for, either the name or the uuid")]
|
|
|
|
|
zone: String,
|
2024-04-12 00:34:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MetricsCommand {
|
|
|
|
|
pub async fn run(
|
|
|
|
|
self,
|
|
|
|
|
mut client: ControlServiceClient<Channel>,
|
|
|
|
|
_events: EventStream,
|
|
|
|
|
) -> Result<()> {
|
2024-07-18 20:47:18 -07:00
|
|
|
let zone_id: String = resolve_zone(&mut client, &self.zone).await?;
|
2024-04-12 00:34:46 -07:00
|
|
|
let root = client
|
2024-07-18 20:47:18 -07:00
|
|
|
.read_zone_metrics(ReadZoneMetricsRequest { zone_id })
|
2024-04-12 00:34:46 -07:00
|
|
|
.await?
|
|
|
|
|
.into_inner()
|
|
|
|
|
.root
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
match self.format {
|
|
|
|
|
MetricsFormat::Tree => {
|
|
|
|
|
self.print_metrics_tree(root)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MetricsFormat::Json | MetricsFormat::JsonPretty | MetricsFormat::Yaml => {
|
|
|
|
|
let value = serde_json::to_value(proto2dynamic(root)?)?;
|
|
|
|
|
let encoded = if self.format == MetricsFormat::JsonPretty {
|
|
|
|
|
serde_json::to_string_pretty(&value)?
|
|
|
|
|
} else if self.format == MetricsFormat::Yaml {
|
|
|
|
|
serde_yaml::to_string(&value)?
|
|
|
|
|
} else {
|
|
|
|
|
serde_json::to_string(&value)?
|
|
|
|
|
};
|
|
|
|
|
println!("{}", encoded.trim());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MetricsFormat::KeyValue => {
|
|
|
|
|
self.print_key_value(root)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
fn print_metrics_tree(&self, root: ZoneMetricNode) -> Result<()> {
|
2024-04-12 00:34:46 -07:00
|
|
|
print!("{}", metrics_tree(root));
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
fn print_key_value(&self, metrics: ZoneMetricNode) -> Result<()> {
|
2024-04-12 00:34:46 -07:00
|
|
|
let kvs = metrics_flat(metrics);
|
|
|
|
|
println!("{}", kv2line(kvs));
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|