1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use nanocl_error::http_client::HttpClientResult;
use nanocl_stubs::{
generic::GenericFilter,
metric::{Metric, MetricPartial},
};
use super::http_client::NanocldClient;
impl NanocldClient {
/// ## Default path for metrics
const METRIC_PATH: &'static str = "/metrics";
/// List existing metrics in the system
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.list_metric(None).await;
/// ```
pub async fn list_metric(
&self,
query: Option<&GenericFilter>,
) -> HttpClientResult<Vec<Metric>> {
let query = Self::convert_query(query)?;
let res = self.send_get(Self::METRIC_PATH, Some(&query)).await?;
Self::res_json(res).await
}
/// Create a new metric in the system
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
/// use nanocld_client::stubs::metric::MetricPartial;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.list_metric(&MetricPartial {
/// kind: "my-source.io/type".to_owned(),
/// data: serde_json::json!({
/// "name": "my-metric",
/// "description": "My metric",
/// }),
/// }).await;
/// ```
pub async fn create_metric(
&self,
metric: &MetricPartial,
) -> HttpClientResult<Metric> {
let res = self
.send_post(Self::METRIC_PATH, Some(metric), None::<String>)
.await?;
Self::res_json(res).await
}
/// Inspect a metric in the system
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.inspect_metric("my-metric-key").await;
/// ```
pub async fn inspect_metric(&self, key: &str) -> HttpClientResult<Metric> {
let res = self
.send_get(
&format!("{}/{key}/inspect", Self::METRIC_PATH),
None::<String>,
)
.await?;
Self::res_json(res).await
}
}
#[cfg(test)]
mod tests {
use crate::ConnectOpts;
use super::*;
#[ntex::test]
async fn basic() {
let client = NanocldClient::connect_to(&ConnectOpts {
url: "http://nanocl.internal:8585".into(),
..Default::default()
})
.expect("Failed to create a nanocl client");
let metric = client
.create_metric(&MetricPartial {
kind: "my-source.io/type".to_owned(),
data: serde_json::json!({
"name": "my-metric",
"description": "My metric",
}),
note: None,
})
.await
.unwrap();
assert_eq!(metric.kind, "my-source.io/type");
let metrics = client.list_metric(None).await.unwrap();
assert!(!metrics.is_empty());
client
.inspect_metric(metrics[0].key.to_string().as_str())
.await
.unwrap();
}
}