prometheus_http_query/direct.rs
1use crate::client::*;
2use crate::error::Error;
3use std::str::FromStr;
4
5/// Execute an instant query.
6///
7/// This is just a convenience function for one-off requests, see [`Client::query`].
8///
9/// ```rust
10/// use prometheus_http_query::query;
11///
12/// #[tokio::main(flavor = "current_thread")]
13/// async fn main() -> Result<(), anyhow::Error> {
14/// let q = "sum(prometheus_http_requests_total)";
15///
16/// let response = query("http://localhost:9090", q)?.timeout((1000.0).into()).get().await?;
17///
18/// assert!(response.data().as_vector().is_some());
19///
20/// // Or make a POST request.
21/// let response = query("http://localhost:9090", q)?.timeout((1000.0).into()).post().await?;
22///
23/// assert!(response.data().as_vector().is_some());
24///
25/// Ok(())
26/// }
27/// ```
28pub fn query(host: &str, query: impl std::fmt::Display) -> Result<InstantQueryBuilder, Error> {
29 Client::from_str(host).map(|c| c.query(query))
30}
31
32/// Execute a range query.
33///
34/// This is just a convenience function for one-off requests, see [`Client::query_range`].
35///
36/// ```rust
37/// use prometheus_http_query::query_range;
38///
39/// #[tokio::main(flavor = "current_thread")]
40/// async fn main() -> Result<(), anyhow::Error> {
41/// let q = "sum(prometheus_http_requests_total)";
42///
43/// let response = query_range("http://localhost:9090", q, 1648373100, 1648373300, 10.0)?.get().await?;
44///
45/// assert!(response.data().as_matrix().is_some());
46///
47/// // Or make a POST request.
48/// let response = query_range("http://localhost:9090", q, 1648373100, 1648373300, 10.0)?.post().await?;
49///
50/// assert!(response.data().as_matrix().is_some());
51///
52/// Ok(())
53/// }
54/// ```
55pub fn query_range(
56 host: &str,
57 query: impl std::fmt::Display,
58 start: i64,
59 end: i64,
60 step: f64,
61) -> Result<RangeQueryBuilder, Error> {
62 Client::from_str(host).map(|c| c.query_range(query, start, end, step))
63}
64
65/// Create a [`SeriesQueryBuilder`] to apply filters to a series metadata
66/// query before sending it to Prometheus.
67///
68/// This is just a convenience function for one-off requests, see [`Client::series`].
69///
70/// ```rust
71/// use prometheus_http_query::series;
72///
73/// #[tokio::main(flavor = "current_thread")]
74/// async fn main() -> Result<(), anyhow::Error> {
75/// let response = series("http://localhost:9090")?
76/// .match_many(&["{handler=\"/api/v1/query\"}"])
77/// .get()
78/// .await;
79///
80/// assert!(response.is_ok());
81///
82/// Ok(())
83/// }
84/// ```
85pub fn series(host: &str) -> Result<SeriesQueryBuilder, Error> {
86 Client::from_str(host).map(|c| c.series())
87}
88
89/// Create a [`LabelNamesQueryBuilder`] to apply filters to a query for the label
90/// names endpoint before sending it to Prometheus.
91///
92/// This is just a convenience function for one-off requests, see [`Client::label_names`].
93///
94/// ```rust
95/// use prometheus_http_query::label_names;
96///
97/// #[tokio::main(flavor = "current_thread")]
98/// async fn main() -> Result<(), anyhow::Error> {
99/// let response = label_names("http://localhost:9090")?.get().await;
100///
101/// assert!(response.is_ok());
102///
103/// Ok(())
104/// }
105/// ```
106pub fn label_names(host: &str) -> Result<LabelNamesQueryBuilder, Error> {
107 Client::from_str(host).map(|c| c.label_names())
108}
109
110/// Create a [`LabelValuesQueryBuilder`] to apply filters to a query for the label
111/// values endpoint before sending it to Prometheus.
112///
113/// This is just a convenience function for one-off requests, see [`Client::label_values`].
114///
115/// ```rust
116/// use prometheus_http_query::label_values;
117///
118/// #[tokio::main(flavor = "current_thread")]
119/// async fn main() -> Result<(), anyhow::Error> {
120/// let response = label_values("http://localhost:9090", "job")?.get().await;
121///
122/// assert!(response.is_ok());
123///
124/// Ok(())
125/// }
126/// ```
127pub fn label_values(host: &str, label: &str) -> Result<LabelValuesQueryBuilder, Error> {
128 Client::from_str(host).map(|c| c.label_values(label))
129}
130
131/// Query the current state of target discovery.
132///
133/// This is just a convenience function for one-off requests, see [`Client::targets`].
134///
135/// ```rust
136/// use prometheus_http_query::{targets, TargetState};
137///
138/// #[tokio::main(flavor = "current_thread")]
139/// async fn main() -> Result<(), anyhow::Error> {
140/// let response = targets("http://localhost:9090")?.get().await;
141///
142/// assert!(response.is_ok());
143///
144/// Ok(())
145/// }
146/// ```
147pub fn targets(host: &str) -> Result<TargetsQueryBuilder, Error> {
148 Client::from_str(host).map(|c| c.targets())
149}
150
151/// Create a [`RulesQueryBuilder`] to apply filters to the rules query before
152/// sending it to Prometheus.
153///
154/// This is just a convenience function for one-off requests, see [Client::rules].
155///
156/// ```rust
157/// use prometheus_http_query::{rules, RuleKind};
158///
159/// #[tokio::main(flavor = "current_thread")]
160/// async fn main() -> Result<(), anyhow::Error> {
161/// let response = rules("http://localhost:9090")?.kind(RuleKind::Recording).get().await;
162///
163/// assert!(response.is_ok());
164///
165/// Ok(())
166/// }
167/// ```
168pub fn rules(host: &str) -> Result<RulesQueryBuilder, Error> {
169 Client::from_str(host).map(|c| c.rules())
170}
171
172/// Retrieve a list of active alerts.
173///
174/// This is just a convenience function for one-off requests, see [`Client::alerts`].
175///
176/// ```rust
177/// use prometheus_http_query::alerts;
178///
179/// #[tokio::main(flavor = "current_thread")]
180/// async fn main() -> Result<(), anyhow::Error> {
181/// let response = alerts("http://localhost:9090")?.get().await;
182///
183/// assert!(response.is_ok());
184///
185/// Ok(())
186/// }
187/// ```
188pub fn alerts(host: &str) -> Result<AlertsQueryBuilder, Error> {
189 Client::from_str(host).map(|c| c.alerts())
190}
191
192/// Retrieve a list of flags that Prometheus was configured with.
193///
194/// This is just a convenience function for one-off requests, see [`Client::flags`].
195///
196/// ```rust
197/// use prometheus_http_query::flags;
198///
199/// #[tokio::main(flavor = "current_thread")]
200/// async fn main() -> Result<(), anyhow::Error> {
201/// let response = flags("http://localhost:9090")?.get().await;
202///
203/// assert!(response.is_ok());
204///
205/// Ok(())
206/// }
207/// ```
208pub fn flags(host: &str) -> Result<FlagsQueryBuilder, Error> {
209 Client::from_str(host).map(|c| c.flags())
210}
211
212/// Retrieve Prometheus server build information.
213///
214/// This is just a convenience function for one-off requests, see [`Client::build_information`].
215///
216/// ```rust
217/// use prometheus_http_query::build_information;
218///
219/// #[tokio::main(flavor = "current_thread")]
220/// async fn main() -> Result<(), anyhow::Error> {
221/// let response = build_information("http://localhost:9090")?.get().await;
222///
223/// assert!(response.is_ok());
224///
225/// Ok(())
226/// }
227/// ```
228pub fn build_information(host: &str) -> Result<BuildInformationQueryBuilder, Error> {
229 Client::from_str(host).map(|c| c.build_information())
230}
231
232/// Retrieve Prometheus server runtime information.
233///
234/// This is just a convenience function for one-off requests, see [`Client::runtime_information`].
235///
236/// ```rust
237/// use prometheus_http_query::runtime_information;
238///
239/// #[tokio::main(flavor = "current_thread")]
240/// async fn main() -> Result<(), anyhow::Error> {
241/// let response = runtime_information("http://localhost:9090")?.get().await;
242///
243/// assert!(response.is_ok());
244///
245/// Ok(())
246/// }
247/// ```
248pub fn runtime_information(host: &str) -> Result<RuntimeInformationQueryBuilder, Error> {
249 Client::from_str(host).map(|c| c.runtime_information())
250}
251
252/// Query the current state of alertmanager discovery.
253///
254/// This is just a convenience function for one-off requests, see [`Client::alertmanagers`].
255///
256/// ```rust
257/// use prometheus_http_query::alertmanagers;
258///
259/// #[tokio::main(flavor = "current_thread")]
260/// async fn main() -> Result<(), anyhow::Error> {
261/// let response = alertmanagers("http://localhost:9090")?.get().await;
262///
263/// assert!(response.is_ok());
264///
265/// Ok(())
266/// }
267/// ```
268pub fn alertmanagers(host: &str) -> Result<AlertmanagersQueryBuilder, Error> {
269 Client::from_str(host).map(|c| c.alertmanagers())
270}
271
272/// Create a [`TargetMetadataQueryBuilder`] to apply filters to a target metadata
273/// query before sending it to Prometheus.
274///
275/// This is just a convenience function for one-off requests, see [`Client::target_metadata`].
276///
277/// ```rust
278/// use prometheus_http_query::target_metadata;
279///
280/// #[tokio::main(flavor = "current_thread")]
281/// async fn main() -> Result<(), anyhow::Error> {
282/// let response = target_metadata("http://localhost:9090")?
283/// .metric("go_goroutines")
284/// .get()
285/// .await;
286///
287/// assert!(response.is_ok());
288///
289/// let response = target_metadata("http://localhost:9090")?
290/// .match_target("{job=\"prometheus\"}")
291/// .get()
292/// .await;
293///
294/// assert!(response.is_ok());
295///
296/// Ok(())
297/// }
298/// ```
299pub fn target_metadata(host: &str) -> Result<TargetMetadataQueryBuilder, Error> {
300 Client::from_str(host).map(|c| c.target_metadata())
301}
302
303/// Create a [`MetricMetadataQueryBuilder`] to apply filters to a metric metadata
304/// query before sending it to Prometheus.
305///
306/// This is just a convenience function for one-off requests, see [`Client::metric_metadata`].
307///
308/// ```rust
309/// use prometheus_http_query::metric_metadata;
310///
311/// #[tokio::main(flavor = "current_thread")]
312/// async fn main() -> Result<(), anyhow::Error> {
313/// let response = metric_metadata("http://localhost:9090")?.get().await;
314/// assert!(response.is_ok());
315///
316/// let response = metric_metadata("http://localhost:9090")?
317/// .metric("go_goroutines")
318/// .get()
319/// .await;
320/// assert!(response.is_ok());
321///
322/// Ok(())
323/// }
324/// ```
325pub fn metric_metadata(host: &str) -> Result<MetricMetadataQueryBuilder, Error> {
326 Client::from_str(host).map(|c| c.metric_metadata())
327}
328
329/// Retrieve various cardinality statistics about the Prometheus TSDB.
330///
331/// This is just a convenience function for one-off requests, see [`Client::tsdb_statistics`].
332///
333/// ```rust
334/// use prometheus_http_query::tsdb_statistics;
335///
336/// #[tokio::main(flavor = "current_thread")]
337/// async fn main() -> Result<(), anyhow::Error> {
338/// let response = tsdb_statistics("http://localhost:9090")?.get().await;
339///
340/// assert!(response.is_ok());
341///
342/// Ok(())
343/// }
344/// ```
345pub fn tsdb_statistics(host: &str) -> Result<TsdbStatisticsQueryBuilder, Error> {
346 Client::from_str(host).map(|c| c.tsdb_statistics())
347}
348
349/// Retrieve information about the WAL replay.
350///
351/// This is just a convenience function for one-off requests, see [`Client::wal_replay_statistics`].
352///
353/// ```rust
354/// use prometheus_http_query::wal_replay_statistics;
355///
356/// #[tokio::main(flavor = "current_thread")]
357/// async fn main() -> Result<(), anyhow::Error> {
358/// let response = wal_replay_statistics("http://localhost:9090")?.get().await;
359///
360/// assert!(response.is_ok());
361///
362/// Ok(())
363/// }
364/// ```
365pub fn wal_replay_statistics(host: &str) -> Result<WalReplayStatisticsQueryBuilder, Error> {
366 Client::from_str(host).map(|c| c.wal_replay_statistics())
367}
368
369/// Check Prometheus server health.
370///
371/// This is just a convenience function for one-off requests, see [`Client::is_server_healthy`].
372///
373/// ```rust
374/// use prometheus_http_query::is_server_healthy;
375///
376/// #[tokio::main(flavor = "current_thread")]
377/// async fn main() -> Result<(), anyhow::Error> {
378/// let response = is_server_healthy("http://localhost:9090").await;
379///
380/// assert!(response.unwrap_or_default());
381///
382/// Ok(())
383/// }
384/// ```
385pub async fn is_server_healthy(host: &str) -> Result<bool, Error> {
386 Client::from_str(host)?.is_server_healthy().await
387}
388
389/// Check Prometheus server readiness.
390///
391/// This is just a convenience function for one-off requests, see [`Client::is_server_ready`].
392///
393/// ```rust
394/// use prometheus_http_query::is_server_ready;
395///
396/// #[tokio::main(flavor = "current_thread")]
397/// async fn main() -> Result<(), anyhow::Error> {
398/// let response = is_server_ready("http://localhost:9090").await;
399///
400/// assert!(response.unwrap_or_default());
401///
402/// Ok(())
403/// }
404/// ```
405pub async fn is_server_ready(host: &str) -> Result<bool, Error> {
406 Client::from_str(host)?.is_server_ready().await
407}