prometheus_http_query/direct.rs
1use crate::client::*;
2use crate::error::Error;
3use crate::response::*;
4use crate::selector::Selector;
5use crate::util::TargetState;
6use std::borrow::Borrow;
7use std::collections::HashMap;
8use std::str::FromStr;
9
10/// Execute an instant query.
11///
12/// This is just a convenience function for one-off requests, see [`Client::query`].
13///
14/// ```rust
15/// use prometheus_http_query::query;
16///
17/// #[tokio::main(flavor = "current_thread")]
18/// async fn main() -> Result<(), anyhow::Error> {
19/// let q = "sum(prometheus_http_requests_total)";
20///
21/// let response = query("http://localhost:9090", q)?.timeout(1000).get().await?;
22///
23/// assert!(response.data().as_vector().is_some());
24///
25/// // Or make a POST request.
26/// let response = query("http://localhost:9090", q)?.timeout(1000).post().await?;
27///
28/// assert!(response.data().as_vector().is_some());
29///
30/// Ok(())
31/// }
32/// ```
33pub fn query(host: &str, query: impl std::fmt::Display) -> Result<InstantQueryBuilder, Error> {
34 Client::from_str(host).map(|c| c.query(query))
35}
36
37/// Execute a range query.
38///
39/// This is just a convenience function for one-off requests, see [`Client::query_range`].
40///
41/// ```rust
42/// use prometheus_http_query::query_range;
43///
44/// #[tokio::main(flavor = "current_thread")]
45/// async fn main() -> Result<(), anyhow::Error> {
46/// let q = "sum(prometheus_http_requests_total)";
47///
48/// let response = query_range("http://localhost:9090", q, 1648373100, 1648373300, 10.0)?.get().await?;
49///
50/// assert!(response.data().as_matrix().is_some());
51///
52/// // Or make a POST request.
53/// let response = query_range("http://localhost:9090", q, 1648373100, 1648373300, 10.0)?.post().await?;
54///
55/// assert!(response.data().as_matrix().is_some());
56///
57/// Ok(())
58/// }
59/// ```
60pub fn query_range(
61 host: &str,
62 query: impl std::fmt::Display,
63 start: i64,
64 end: i64,
65 step: f64,
66) -> Result<RangeQueryBuilder, Error> {
67 Client::from_str(host).map(|c| c.query_range(query, start, end, step))
68}
69
70/// Create a [`SeriesQueryBuilder`] to apply filters to a series metadata
71/// query before sending it to Prometheus.
72///
73/// This is just a convenience function for one-off requests, see [`Client::series`].
74///
75/// ```rust
76/// use prometheus_http_query::{series, Selector};
77///
78/// #[tokio::main(flavor = "current_thread")]
79/// async fn main() -> Result<(), anyhow::Error> {
80/// let select = Selector::new()
81/// .eq("handler", "/api/v1/query");
82///
83/// let response = series("http://localhost:9090", &[select])?.get().await;
84///
85/// assert!(response.is_ok());
86///
87/// Ok(())
88/// }
89/// ```
90pub fn series<'a, T>(host: &str, selectors: T) -> Result<SeriesQueryBuilder, Error>
91where
92 T: IntoIterator,
93 T::Item: Borrow<Selector<'a>>,
94{
95 Client::from_str(host).and_then(|c| c.series(selectors))
96}
97
98/// Create a [`LabelNamesQueryBuilder`] to apply filters to a query for the label
99/// names endpoint before sending it to Prometheus.
100///
101/// This is just a convenience function for one-off requests, see [`Client::label_names`].
102///
103/// ```rust
104/// use prometheus_http_query::label_names;
105///
106/// #[tokio::main(flavor = "current_thread")]
107/// async fn main() -> Result<(), anyhow::Error> {
108/// let response = label_names("http://localhost:9090")?.get().await;
109///
110/// assert!(response.is_ok());
111///
112/// Ok(())
113/// }
114/// ```
115pub fn label_names(host: &str) -> Result<LabelNamesQueryBuilder, Error> {
116 Client::from_str(host).map(|c| c.label_names())
117}
118
119/// Create a [`LabelValuesQueryBuilder`] to apply filters to a query for the label
120/// values endpoint before sending it to Prometheus.
121///
122/// This is just a convenience function for one-off requests, see [`Client::label_values`].
123///
124/// ```rust
125/// use prometheus_http_query::label_values;
126///
127/// #[tokio::main(flavor = "current_thread")]
128/// async fn main() -> Result<(), anyhow::Error> {
129/// let response = label_values("http://localhost:9090", "job")?.get().await;
130///
131/// assert!(response.is_ok());
132///
133/// Ok(())
134/// }
135/// ```
136pub fn label_values(host: &str, label: &str) -> Result<LabelValuesQueryBuilder, Error> {
137 Client::from_str(host).map(|c| c.label_values(label))
138}
139
140/// Query the current state of target discovery.
141///
142/// This is just a convenience function for one-off requests, see [`Client::targets`].
143///
144/// ```rust
145/// use prometheus_http_query::{targets, TargetState};
146///
147/// #[tokio::main(flavor = "current_thread")]
148/// async fn main() -> Result<(), anyhow::Error> {
149/// let response = targets("http://localhost:9090", Some(TargetState::Active)).await;
150///
151/// assert!(response.is_ok());
152///
153/// Ok(())
154/// }
155/// ```
156pub async fn targets(host: &str, state: Option<TargetState>) -> Result<Targets, Error> {
157 Client::from_str(host)?.targets(state).await
158}
159
160/// Create a [`RulesQueryBuilder`] to apply filters to the rules query before
161/// sending it to Prometheus.
162///
163/// This is just a convenience function for one-off requests, see [Client::rules].
164///
165/// ```rust
166/// use prometheus_http_query::{rules, RuleKind};
167///
168/// #[tokio::main(flavor = "current_thread")]
169/// async fn main() -> Result<(), anyhow::Error> {
170/// let response = rules("http://localhost:9090")?.kind(RuleKind::Recording).get().await;
171///
172/// assert!(response.is_ok());
173///
174/// Ok(())
175/// }
176/// ```
177pub fn rules(host: &str) -> Result<RulesQueryBuilder, Error> {
178 Client::from_str(host).map(|c| c.rules())
179}
180
181/// Retrieve a list of active alerts.
182///
183/// This is just a convenience function for one-off requests, see [`Client::alerts`].
184///
185/// ```rust
186/// use prometheus_http_query::alerts;
187///
188/// #[tokio::main(flavor = "current_thread")]
189/// async fn main() -> Result<(), anyhow::Error> {
190/// let response = alerts("http://localhost:9090").await;
191///
192/// assert!(response.is_ok());
193///
194/// Ok(())
195/// }
196/// ```
197pub async fn alerts(host: &str) -> Result<Vec<Alert>, Error> {
198 Client::from_str(host)?.alerts().await
199}
200
201/// Retrieve a list of flags that Prometheus was configured with.
202///
203/// This is just a convenience function for one-off requests, see [`Client::flags`].
204///
205/// ```rust
206/// use prometheus_http_query::flags;
207///
208/// #[tokio::main(flavor = "current_thread")]
209/// async fn main() -> Result<(), anyhow::Error> {
210/// let response = flags("http://localhost:9090").await;
211///
212/// assert!(response.is_ok());
213///
214/// Ok(())
215/// }
216/// ```
217pub async fn flags(host: &str) -> Result<HashMap<String, String>, Error> {
218 Client::from_str(host)?.flags().await
219}
220
221/// Retrieve Prometheus server build information.
222///
223/// This is just a convenience function for one-off requests, see [`Client::build_information`].
224///
225/// ```rust
226/// use prometheus_http_query::build_information;
227///
228/// #[tokio::main(flavor = "current_thread")]
229/// async fn main() -> Result<(), anyhow::Error> {
230/// let response = build_information("http://localhost:9090").await;
231///
232/// assert!(response.is_ok());
233///
234/// Ok(())
235/// }
236/// ```
237pub async fn build_information(host: &str) -> Result<BuildInformation, Error> {
238 Client::from_str(host)?.build_information().await
239}
240
241/// Retrieve Prometheus server runtime information.
242///
243/// This is just a convenience function for one-off requests, see [`Client::runtime_information`].
244///
245/// ```rust
246/// use prometheus_http_query::runtime_information;
247///
248/// #[tokio::main(flavor = "current_thread")]
249/// async fn main() -> Result<(), anyhow::Error> {
250/// let response = runtime_information("http://localhost:9090").await;
251///
252/// assert!(response.is_ok());
253///
254/// Ok(())
255/// }
256/// ```
257pub async fn runtime_information(host: &str) -> Result<RuntimeInformation, Error> {
258 Client::from_str(host)?.runtime_information().await
259}
260
261/// Query the current state of alertmanager discovery.
262///
263/// This is just a convenience function for one-off requests, see [`Client::alertmanagers`].
264///
265/// ```rust
266/// use prometheus_http_query::alertmanagers;
267///
268/// #[tokio::main(flavor = "current_thread")]
269/// async fn main() -> Result<(), anyhow::Error> {
270/// let response = alertmanagers("http://localhost:9090").await;
271///
272/// assert!(response.is_ok());
273///
274/// Ok(())
275/// }
276/// ```
277pub async fn alertmanagers(host: &str) -> Result<Alertmanagers, Error> {
278 Client::from_str(host)?.alertmanagers().await
279}
280
281/// Create a [`TargetMetadataQueryBuilder`] to apply filters to a target metadata
282/// query before sending it to Prometheus.
283///
284/// This is just a convenience function for one-off requests, see [`Client::target_metadata`].
285///
286/// ```rust
287/// use prometheus_http_query::{target_metadata, Selector};
288///
289/// #[tokio::main(flavor = "current_thread")]
290/// async fn main() -> Result<(), anyhow::Error> {
291/// let response = target_metadata("http://localhost:9090")?
292/// .metric("go_goroutines")
293/// .get()
294/// .await;
295/// assert!(response.is_ok());
296///
297/// let select = Selector::new().eq("job", "prometheus");
298/// let response = target_metadata("http://localhost:9090")?
299/// .match_target(&select)
300/// .get()
301/// .await;
302/// assert!(response.is_ok());
303///
304/// Ok(())
305/// }
306/// ```
307pub fn target_metadata(host: &str) -> Result<TargetMetadataQueryBuilder, Error> {
308 Client::from_str(host).map(|c| c.target_metadata())
309}
310
311/// Create a [`MetricMetadataQueryBuilder`] to apply filters to a metric metadata
312/// query before sending it to Prometheus.
313///
314/// This is just a convenience function for one-off requests, see [`Client::metric_metadata`].
315///
316/// ```rust
317/// use prometheus_http_query::{metric_metadata, Selector};
318///
319/// #[tokio::main(flavor = "current_thread")]
320/// async fn main() -> Result<(), anyhow::Error> {
321/// let response = metric_metadata("http://localhost:9090")?.get().await;
322/// assert!(response.is_ok());
323///
324/// let response = metric_metadata("http://localhost:9090")?
325/// .metric("go_goroutines")
326/// .get()
327/// .await;
328/// assert!(response.is_ok());
329///
330/// Ok(())
331/// }
332/// ```
333pub fn metric_metadata(host: &str) -> Result<MetricMetadataQueryBuilder, Error> {
334 Client::from_str(host).map(|c| c.metric_metadata())
335}