Skip to content

Commit

Permalink
Merge pull request #18 from j2ghz/master
Browse files Browse the repository at this point in the history
Add timestamp option to render_sample
  • Loading branch information
Francesco Cogno authored Jun 16, 2020
2 parents 6d41976 + ebad233 commit faf2e07
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
4 changes: 2 additions & 2 deletions examples/folder_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ async fn main() {

let mut attributes = Vec::new();
attributes.push(("folder", "/var/log/"));
s.push_str(&pc.render_sample(Some(&attributes), total_size_log));
s.push_str(&pc.render_sample(Some(&attributes), total_size_log, None));

attributes[0].1 = "/tmp";
s.push_str(&pc.render_sample(Some(&attributes), total_size_tmp));
s.push_str(&pc.render_sample(Some(&attributes), total_size_tmp, None));

Ok(s)
}
Expand Down
27 changes: 12 additions & 15 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,22 @@ async fn main() {
let addr = ([0, 0, 0, 0], 32221).into();
println!("starting exporter on {}", addr);

render_prometheus(addr, MyOptions::default(), |request, options| {
async move {
println!(
"in our render_prometheus(request == {:?}, options == {:?})",
request, options
);
render_prometheus(addr, MyOptions::default(), |request, options| async move {
println!(
"in our render_prometheus(request == {:?}, options == {:?})",
request, options
);

let total_size_log = calculate_file_size("/var/log").unwrap();
let total_size_log = calculate_file_size("/var/log").unwrap();

let pc =
PrometheusMetric::new("folder_size", MetricType::Counter, "Size of the folder");
let mut s = pc.render_header();
let pc = PrometheusMetric::new("folder_size", MetricType::Counter, "Size of the folder");
let mut s = pc.render_header();

let mut attributes = Vec::new();
attributes.push(("folder", "/var/log/"));
s.push_str(&pc.render_sample(Some(&attributes), total_size_log));
let mut attributes = Vec::new();
attributes.push(("folder", "/var/log/"));
s.push_str(&pc.render_sample(Some(&attributes), total_size_log, None));

Ok(s)
}
Ok(s)
})
.await;
}
38 changes: 27 additions & 11 deletions src/render_to_prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<'a> PrometheusMetric<'a> {
/// * `labels` - A slice of pairs indicating Label-Value. It is optional.
/// * `value` - A number that will be appended as counter value. Remember, all
/// values in Prometheus are float but you can pass any num::Num values here.
/// * `timestamp` - An int64 number representing milliseconds since epoch, i.e. 1970-01-01 00:00:00 UTC, excluding leap seconds.
///
/// # Example
///
Expand All @@ -49,21 +50,26 @@ impl<'a> PrometheusMetric<'a> {
/// let mut labels = Vec::new();
/// labels.push(("folder", "/var/log/"));
/// s.push_str(&pc.render_sample(Some(&labels), 1024));
/// s.push_str(&pc.render_sample(Some(&labels), 1024, None));
/// labels[0].1 = "/tmp";
/// s.push_str(&pc.render_sample(Some(&labels), 5_000_000));
/// s.push_str(&pc.render_sample(Some(&labels), 5_000_000, Some(1592070947954)));
/// ```
pub fn render_sample<N>(&self, labels: Option<&[(&'a str, &'a str)]>, value: N) -> String
pub fn render_sample<N>(
&self,
labels: Option<&[(&'a str, &'a str)]>,
value: N,
timestamp: Option<i64>,
) -> String
where
N: Num + std::fmt::Display,
{
let mut s = format!("{}", self.counter_name);
if let Some(labels) = labels {
if labels.is_empty() {
format!("{} {}\n", self.counter_name, value.to_string())
s.push_str(&format!(" {}", value.to_string()));
} else {
let mut s = format!("{}{{", self.counter_name);

s.push_str("{");
let mut first = true;
for (key, val) in labels.iter() {
if !first {
Expand All @@ -75,12 +81,18 @@ impl<'a> PrometheusMetric<'a> {
s.push_str(&format!("{}=\"{}\"", key, val));
}

s.push_str(&format!("}} {}\n", value.to_string()));
s
s.push_str(&format!("}} {}", value.to_string()));
}
} else {
format!("{} {}\n", self.counter_name, value.to_string())
s.push_str(" ");
s.push_str(&value.to_string());
}
if let Some(timestamp) = timestamp {
s.push_str(" ");
s.push_str(&timestamp.to_string());
}
s.push_str("\n");
s
}
}

Expand Down Expand Up @@ -115,7 +127,7 @@ mod tests {
let number_string = number.to_string();
labels.push(("instance", &number_string));

let ret = pc.render_sample(Some(&labels), number);
let ret = pc.render_sample(Some(&labels), number, None);

assert_eq!(
ret,
Expand All @@ -132,8 +144,12 @@ mod tests {
fn test_no_labels() {
let pc = PrometheusMetric::new("gigino_total", MetricType::Counter, "Number of giginos");
assert_eq!(
pc.render_sample(None, 100),
pc.render_sample(None, 100, None),
format!("gigino_total {}\n", 100)
);
assert_eq!(
pc.render_sample(None, 100, Some(9223372036854775807)),
format!("gigino_total 100 9223372036854775807\n")
);
}
}

0 comments on commit faf2e07

Please sign in to comment.