-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try(histogram): Local calculate for p99/pxx
- Loading branch information
Showing
7 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::metric::REGISTRY; | ||
use log::{error, info}; | ||
use once_cell::sync::Lazy; | ||
use parking_lot::Mutex; | ||
use prometheus::{register_int_gauge_vec, IntGaugeVec}; | ||
|
||
pub struct Histogram { | ||
name: String, | ||
recorder: Mutex<hdrhistogram::Histogram<u64>>, | ||
gauge: IntGaugeVec, | ||
} | ||
|
||
impl Histogram { | ||
pub fn new(name: &str) -> Histogram { | ||
info!("Registering metrics for {}", name); | ||
let gauge: IntGaugeVec = { register_int_gauge_vec!(name, name, &["quantile"]).unwrap() }; | ||
|
||
REGISTRY.register(Box::new(gauge.clone())).expect(""); | ||
|
||
Self { | ||
name: name.to_owned(), | ||
recorder: Mutex::new(hdrhistogram::Histogram::new(4).unwrap()), | ||
gauge, | ||
} | ||
} | ||
|
||
pub fn record(&self, value: u64) { | ||
let mut recorder = self.recorder.lock(); | ||
if let Err(e) = recorder.record(value) { | ||
error!("failed to record `{}`: {}", self.name, e); | ||
} | ||
} | ||
|
||
fn clear(&self) { | ||
let mut recorder = self.recorder.lock(); | ||
recorder.clear() | ||
} | ||
|
||
pub fn observe(&self) { | ||
let mut recorder = self.recorder.lock(); | ||
let p99 = recorder.value_at_quantile(0.99); | ||
let p95 = recorder.value_at_quantile(0.95); | ||
let p90 = recorder.value_at_quantile(0.90); | ||
let p80 = recorder.value_at_quantile(0.80); | ||
let p50 = recorder.value_at_quantile(0.50); | ||
|
||
self.gauge.with_label_values(&["p99"]).set(p99 as i64); | ||
self.gauge.with_label_values(&["p95"]).set(p95 as i64); | ||
self.gauge.with_label_values(&["p90"]).set(p90 as i64); | ||
self.gauge.with_label_values(&["p80"]).set(p80 as i64); | ||
self.gauge.with_label_values(&["p50"]).set(p50 as i64); | ||
|
||
recorder.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters