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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
use crate::util::*;
use std::fmt;
/// A time series selector that is gradually built from a metric name and/or
/// a set of label matchers.
#[derive(Debug, Clone, PartialEq)]
pub struct Selector<'a> {
pub(crate) labels: Vec<Label<'a>>,
}
impl<'a> Default for Selector<'a> {
fn default() -> Self {
Self::new()
}
}
impl<'a> Selector<'a> {
/// Create a new instance of [Selector].
pub fn new() -> Self {
Selector { labels: vec![] }
}
/// Select a metric name for this [Selector].
///
/// ```rust
/// use prometheus_http_query::Selector;
///
/// let select = Selector::new().metric("http_requests_total");
///
/// // This is equal to:
/// let other_select = Selector::new().eq("__name__", "http_requests_total");
///
/// assert_eq!(select, other_select);
/// ```
pub fn metric(mut self, metric: &'a str) -> Self
where
Self: Sized,
{
self.labels.push(Label::Equal(("__name__", metric)));
self
}
/// Append a label matcher to the set of matchers of [Selector] that
/// selects labels that match the provided string.<br>
/// PromQL equivalent: `http_requests_total{job="apiserver"}`
///
/// ```rust
/// use prometheus_http_query::Selector;
///
/// let select = Selector::new()
/// .metric("http_requests_total")
/// .eq("job", "apiserver")
/// .to_string();
///
/// let expected = r#"{__name__="http_requests_total",job="apiserver"}"#.to_string();
///
/// assert_eq!(select, expected);
/// ```
pub fn eq(mut self, label: &'a str, value: &'a str) -> Self
where
Self: Sized,
{
self.labels.push(Label::Equal((label, value)));
self
}
/// Append a label matcher to the set of matchers of [Selector] that
/// selects labels that do not match the provided string.<br>
/// PromQL equivalent: `http_requests_total{job!="apiserver"}`
///
/// ```rust
/// use prometheus_http_query::Selector;
///
/// let select = Selector::new()
/// .metric("http_requests_total")
/// .ne("job", "apiserver")
/// .to_string();
///
/// let expected = r#"{__name__="http_requests_total",job!="apiserver"}"#.to_string();
///
/// assert_eq!(select, expected);
/// ```
pub fn ne(mut self, label: &'a str, value: &'a str) -> Self
where
Self: Sized,
{
self.labels.push(Label::NotEqual((label, value)));
self
}
/// Append a label matcher to the set of matchers of [Selector] that
/// selects labels that regex-match the provided string.
/// PromQL equivalent: `http_requests_total{job=~"apiserver"}`
///
/// ```rust
/// use prometheus_http_query::Selector;
///
/// let select = Selector::new()
/// .metric("http_requests_total")
/// .regex_eq("job", "apiserver")
/// .to_string();
///
/// let expected = r#"{__name__="http_requests_total",job=~"apiserver"}"#.to_string();
///
/// assert_eq!(select, expected);
/// ```
pub fn regex_eq(mut self, label: &'a str, value: &'a str) -> Self
where
Self: Sized,
{
self.labels.push(Label::RegexEqual((label, value)));
self
}
/// Append a label matcher to the set of matchers of [Selector] that
/// selects labels that do not regex-match the provided string.<br>
/// PromQL equivalent: `http_requests_total{job!~"apiserver"}`
///
/// ```rust
/// use prometheus_http_query::Selector;
///
/// let select = Selector::new()
/// .metric("http_requests_total")
/// .regex_ne("job", "apiserver")
/// .to_string();
///
/// let expected = r#"{__name__="http_requests_total",job!~"apiserver"}"#.to_string();
///
/// assert_eq!(select, expected);
/// ```
pub fn regex_ne(mut self, label: &'a str, value: &'a str) -> Self
where
Self: Sized,
{
self.labels.push(Label::RegexNotEqual((label, value)));
self
}
}
impl<'a> fmt::Display for Selector<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
let matchers = self
.labels
.iter()
.map(|l| l.to_string())
.collect::<Vec<String>>();
write!(f, "{{{}}}", matchers.as_slice().join(","))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::Label;
#[test]
fn test_selector_display_impl() {
let s = Selector {
labels: vec![
Label::Equal(("__name__", "http_requests_total")),
Label::Equal(("handler", "/api/comments")),
Label::RegexEqual(("job", ".*server")),
Label::RegexNotEqual(("status", "4..")),
Label::NotEqual(("env", "test")),
],
};
let result = String::from("{__name__=\"http_requests_total\",handler=\"/api/comments\",job=~\".*server\",status!~\"4..\",env!=\"test\"}");
assert_eq!(s.to_string(), result);
}
}