commit metrics-recorder-prometheus v0.1.0

This commit is contained in:
Toby Lawrence 2019-04-23 21:12:20 -04:00
parent 6317906b96
commit 7e302ed305
5 changed files with 176 additions and 0 deletions

View File

@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock

View File

@ -0,0 +1,27 @@
# The Code of Conduct
This document is based on the [Rust Code of Conduct](https://www.rust-lang.org/conduct.html) and outlines the standard of conduct which is both expected and enforced as part of this project.
## Conduct
**Contact**: [toby@nuclearfurnace.com](mailto:toby@nuclearfurnace.com)
* We are committed to providing a friendly, safe and welcoming environment for all, regardless of level of experience, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, nationality, or other similar characteristic.
* Avoid using overtly sexual nicknames or other nicknames that might detract from a friendly, safe and welcoming environment for all.
* Please be kind and courteous. There's no need to be mean or rude.
* Respect that people have differences of opinion and that every design or implementation choice carries a trade-off and numerous costs. There is seldom a right answer.
* Please keep unstructured critique to a minimum. If you have solid ideas you want to experiment with, make a fork and see how it works.
* We will exclude you from interaction if you insult, demean or harass anyone. That is not welcome behaviour. We interpret the term "harassment" as including the definition in the <a href="http://citizencodeofconduct.org/">Citizen Code of Conduct</a>; if you have any lack of clarity about what might be included in that concept, please read their definition. In particular, we don't tolerate behavior that excludes people in socially marginalized groups.
* Private harassment is also unacceptable. No matter who you are, if you feel you have been or are being harassed or made uncomfortable by a community member, please contact one of the repository Owners immediately. Whether you're a regular contributor or a newcomer, we care about making this community a safe place for you and we've got your back.
* Likewise any spamming, trolling, flaming, baiting or other attention-stealing behaviour is not welcome.
## Moderation
These are the policies for upholding our community's standards of conduct. If you feel that a thread needs moderation, please use the contact information above, or mention @tobz or @LucioFranco in the thread.
1. Remarks that violate this Code of Conduct, including hateful, hurtful, oppressive, or exclusionary remarks, are not allowed. (Cursing is allowed, but never targeting another user, and never in a hateful manner.)
2. Remarks that moderators find inappropriate, whether listed in the code of conduct or not, are also not allowed.
In the Rust community we strive to go the extra step to look out for each other. Don't just aim to be technically unimpeachable, try to be your best self. In particular, avoid flirting with offensive or sensitive issues, particularly if they're off-topic; this all too often leads to unnecessary fights, hurt feelings, and damaged trust; worse, it can drive people away from the community entirely.
And if someone takes issue with something you said or did, resist the urge to be defensive. Just stop doing what it was they complained about and apologize. Even if you feel you were misinterpreted or unfairly accused, chances are good there was something you could've communicated better — remember that it's your responsibility to make your fellow Rustaceans comfortable. Everyone wants to get along and we are all here first and foremost because we want to talk about cool technology. You will find that people will be eager to assume good intent and forgive as long as you earn their trust.

View File

@ -0,0 +1,18 @@
[package]
name = "metrics-recorder-prometheus"
version = "0.1.0"
authors = ["Toby Lawrence <toby@nuclearfurnace.com>"]
edition = "2018"
license = "MIT"
description = "metric recorder for Prometheus exposition output"
homepage = "https://github.com/metrics-rs/metrics"
repository = "https://github.com/metrics-rs/metrics-recorder-prometheus"
documentation = "https://docs.rs/metrics-recorder-prometheus"
[dependencies]
metrics-core = { path = "../metrics-core", version = "^0.2" }
metrics-util = { path = "../metrics-util", version = "^0.1" }
hdrhistogram = "^6.1"

View File

@ -0,0 +1,18 @@
# metrics-recorder-prometheus
[![conduct-badge][]][conduct] [![downloads-badge][] ![release-badge][]][crate] [![docs-badge][]][docs] [![license-badge][]](#license)
[conduct-badge]: https://img.shields.io/badge/%E2%9D%A4-code%20of%20conduct-blue.svg
[downloads-badge]: https://img.shields.io/crates/d/metrics-recorder-prometheus.svg
[release-badge]: https://img.shields.io/crates/v/metrics-recorder-prometheus.svg
[license-badge]: https://img.shields.io/crates/l/metrics-recorder-prometheus.svg
[docs-badge]: https://docs.rs/metrics-recorder-prometheus/badge.svg
[conduct]: https://github.com/metrics-rs/metrics-recorder-prometheus/blob/master/CODE_OF_CONDUCT.md
[crate]: https://crates.io/crates/metrics-recorder-prometheus
[docs]: https://docs.rs/metrics-recorder-prometheus
__metrics-recorder-prometheus__ is a metric recorder that outputs a Prometheus exposition format.
## code of conduct
**NOTE**: All conversations and contributions to this project shall adhere to the [Code of Conduct][conduct].

View File

@ -0,0 +1,110 @@
//! Records metrics in the Prometheus exposition format.
use std::time::SystemTime;
use hdrhistogram::Histogram;
use metrics_core::MetricsRecorder;
use metrics_util::{Quantile, parse_quantiles};
/// Records metrics in the Prometheus exposition format.
pub struct PrometheusRecorder {
quantiles: Vec<Quantile>,
output: String,
}
impl PrometheusRecorder {
/// Creates a new [`PrometheusRecorder`] with a default set of quantiles.
///
/// Configures the recorder with these default quantiles: 0.0, 0.5, 0.9, 0.95, 0.99, 0.999, and
/// 1.0. If you want to customize the quantiles used, you can call
/// [`PrometheusRecorder::with_quantiles`].
pub fn new() -> Self {
Self::with_quantiles(&[0.0, 0.5, 0.9, 0.95, 0.99, 0.999, 1.0])
}
/// Creates a new [`PrometheusRecorder`] with the given set of quantiles.
pub fn with_quantiles(quantiles: &[f64]) -> Self {
let actual_quantiles = parse_quantiles(quantiles);
Self {
quantiles: actual_quantiles,
output: get_prom_expo_header(),
}
}
}
impl MetricsRecorder for PrometheusRecorder {
fn record_counter<K: AsRef<str>>(&mut self, key: K, value: u64) {
let label = key.as_ref().replace('.', "_");
self.output.push_str("\n# TYPE ");
self.output.push_str(label.as_str());
self.output.push_str(" counter\n");
self.output.push_str(label.as_str());
self.output.push_str(" ");
self.output.push_str(value.to_string().as_str());
self.output.push_str("\n");
}
fn record_gauge<K: AsRef<str>>(&mut self, key: K, value: i64) {
let label = key.as_ref().replace('.', "_");
self.output.push_str("\n# TYPE ");
self.output.push_str(label.as_str());
self.output.push_str(" gauge\n");
self.output.push_str(label.as_str());
self.output.push_str(" ");
self.output.push_str(value.to_string().as_str());
self.output.push_str("\n");
}
fn record_histogram<K: AsRef<str>>(&mut self, key: K, values: &[u64]) {
let mut sum = 0;
let mut h = Histogram::<u64>::new(3).expect("failed to create histogram");
for value in values {
h.record(*value).expect("failed to record histogram value");
sum += *value;
}
let label = key.as_ref().replace('.', "_");
self.output.push_str("\n# TYPE ");
self.output.push_str(label.as_str());
self.output.push_str(" summary\n");
for quantile in &self.quantiles {
let value = h.value_at_quantile(quantile.value());
self.output.push_str(label.as_str());
self.output.push_str("{quantile=\"");
self.output.push_str(quantile.value().to_string().as_str());
self.output.push_str("\"} ");
self.output.push_str(value.to_string().as_str());
self.output.push_str("\n");
}
self.output.push_str(label.as_str());
self.output.push_str("_sum ");
self.output.push_str(sum.to_string().as_str());
self.output.push_str("\n");
self.output.push_str(label.as_str());
self.output.push_str("_count ");
self.output.push_str(values.len().to_string().as_str());
self.output.push_str("\n");
}
}
impl Clone for PrometheusRecorder {
fn clone(&self) -> Self {
Self {
output: get_prom_expo_header(),
quantiles: self.quantiles.clone(),
}
}
}
impl Into<String> for PrometheusRecorder {
fn into(self) -> String {
self.output
}
}
fn get_prom_expo_header() -> String {
let ts = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
format!("# metrics snapshot (ts={}) (prometheus exposition format)", ts)
}