|
| 1 | +use crate::{client::MetricMetadata, Prometheus, Source}; |
| 2 | +use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; |
| 3 | +use nu_protocol::{LabeledError, Signature, SyntaxShape, Type, Value}; |
| 4 | +use prometheus_http_query::Client; |
| 5 | + |
| 6 | +#[derive(Clone, Default)] |
| 7 | +pub struct MetricMetadataCommand; |
| 8 | + |
| 9 | +impl SimplePluginCommand for MetricMetadataCommand { |
| 10 | + type Plugin = Prometheus; |
| 11 | + |
| 12 | + fn name(&self) -> &str { |
| 13 | + "prometheus metric metadata" |
| 14 | + } |
| 15 | + |
| 16 | + fn signature(&self) -> nu_protocol::Signature { |
| 17 | + Signature::build(self.name()) |
| 18 | + .usage(self.usage()) |
| 19 | + .named( |
| 20 | + "source", |
| 21 | + SyntaxShape::String, |
| 22 | + "Prometheus source to query", |
| 23 | + Some('s'), |
| 24 | + ) |
| 25 | + .named( |
| 26 | + "url", |
| 27 | + SyntaxShape::String, |
| 28 | + "Prometheus source url to query", |
| 29 | + Some('u'), |
| 30 | + ) |
| 31 | + .named( |
| 32 | + "limit", |
| 33 | + SyntaxShape::Int, |
| 34 | + "Maximum number of metrics to retrieve", |
| 35 | + None, |
| 36 | + ) |
| 37 | + .named( |
| 38 | + "limit-per-metric", |
| 39 | + SyntaxShape::Int, |
| 40 | + "Maximum number of metadata to retrieve per metric", |
| 41 | + None, |
| 42 | + ) |
| 43 | + .input_output_types(vec![ |
| 44 | + (Type::Nothing, Type::record()), |
| 45 | + (Type::String, Type::record()), |
| 46 | + ]) |
| 47 | + } |
| 48 | + |
| 49 | + fn usage(&self) -> &str { |
| 50 | + "Retrieve metric metadata" |
| 51 | + } |
| 52 | + |
| 53 | + fn run( |
| 54 | + &self, |
| 55 | + _plugin: &Self::Plugin, |
| 56 | + engine: &EngineInterface, |
| 57 | + call: &EvaluatedCall, |
| 58 | + metric: &Value, |
| 59 | + ) -> Result<Value, LabeledError> { |
| 60 | + let span = metric.span(); |
| 61 | + |
| 62 | + let client: Client = Source::from(call, engine)?.try_into()?; |
| 63 | + |
| 64 | + let mut builder = client.metric_metadata(); |
| 65 | + |
| 66 | + if let Value::String { val: metric, .. } = metric { |
| 67 | + builder = builder.metric(metric); |
| 68 | + }; |
| 69 | + |
| 70 | + if let Some(limit) = call.get_flag::<i64>("limit")? { |
| 71 | + let limit = limit.try_into().map_err(|_| { |
| 72 | + let span = call.get_flag_value("limit").unwrap().span(); |
| 73 | + |
| 74 | + LabeledError::new("Limit too large").with_label("does not fit in i32", span) |
| 75 | + })?; |
| 76 | + |
| 77 | + builder = builder.limit(limit); |
| 78 | + } |
| 79 | + |
| 80 | + if let Some(limit_per_metric) = call.get_flag::<i64>("limit-per-metric")? { |
| 81 | + let limit_per_metric = limit_per_metric.try_into().map_err(|_| { |
| 82 | + let span = call.get_flag_value("limit-per-metric").unwrap().span(); |
| 83 | + |
| 84 | + LabeledError::new("Limit per metric too large") |
| 85 | + .with_label("does not fit in i32", span) |
| 86 | + })?; |
| 87 | + |
| 88 | + builder = builder.limit_per_metric(limit_per_metric); |
| 89 | + } |
| 90 | + |
| 91 | + MetricMetadata::new(builder, span).run() |
| 92 | + } |
| 93 | +} |
0 commit comments