mirror of
https://github.com/romanz/electrs.git
synced 2024-11-19 01:43:29 +01:00
Export current process RSS size to Prometheus
This commit is contained in:
parent
eab81c2936
commit
a93284cd96
@ -24,6 +24,7 @@ glob = "0.2"
|
||||
hex = "0.3"
|
||||
libc = "0.2"
|
||||
log = "0.4"
|
||||
page_size = "0.4"
|
||||
prometheus = "0.4"
|
||||
rocksdb = "0.10.1"
|
||||
rust-crypto = "0.2"
|
||||
|
@ -9,6 +9,7 @@ extern crate dirs;
|
||||
extern crate glob;
|
||||
extern crate hex;
|
||||
extern crate libc;
|
||||
extern crate page_size;
|
||||
extern crate prometheus;
|
||||
extern crate rocksdb;
|
||||
extern crate serde;
|
||||
|
@ -1,6 +1,11 @@
|
||||
use page_size;
|
||||
use prometheus::{self, Encoder};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::Path;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use tiny_http;
|
||||
|
||||
pub use prometheus::{
|
||||
@ -10,6 +15,8 @@ pub use prometheus::{
|
||||
|
||||
use util::spawn_thread;
|
||||
|
||||
use errors::*;
|
||||
|
||||
pub struct Metrics {
|
||||
reg: prometheus::Registry,
|
||||
addr: SocketAddr,
|
||||
@ -64,6 +71,7 @@ impl Metrics {
|
||||
"failed to start monitoring HTTP server at {}",
|
||||
self.addr
|
||||
));
|
||||
start_process_exporter(&self);
|
||||
let reg = self.reg.clone();
|
||||
spawn_thread("metrics", move || loop {
|
||||
if let Err(e) = handle_request(®, server.recv()) {
|
||||
@ -85,3 +93,38 @@ fn handle_request(
|
||||
let response = tiny_http::Response::from_data(buffer);
|
||||
request.respond(response)
|
||||
}
|
||||
|
||||
struct Stats {
|
||||
rss: usize,
|
||||
}
|
||||
|
||||
fn parse_stats(path: &Path) -> Result<Stats> {
|
||||
let value = fs::read_to_string(path).chain_err(|| "failed to read stats")?;
|
||||
let parts: Vec<&str> = value.split_whitespace().collect();
|
||||
let page_size = page_size::get();
|
||||
|
||||
let rss_in_pages: usize = parts
|
||||
.get(23)
|
||||
.chain_err(|| "missing value")?
|
||||
.parse::<usize>()
|
||||
.chain_err(|| "invalid value")?;
|
||||
|
||||
Ok(Stats {
|
||||
rss: rss_in_pages * page_size,
|
||||
})
|
||||
}
|
||||
|
||||
fn start_process_exporter(metrics: &Metrics) {
|
||||
let rss = metrics.gauge(MetricOpts::new(
|
||||
"process_memory_rss",
|
||||
"Resident memory size [bytes]",
|
||||
));
|
||||
let path = Path::new("/proc/self/stat");
|
||||
spawn_thread("exporter", move || loop {
|
||||
match parse_stats(path) {
|
||||
Ok(stats) => rss.set(stats.rss as i64),
|
||||
Err(e) => warn!("failed to parse {:?}: {}", path, e),
|
||||
}
|
||||
thread::sleep(Duration::from_secs(5));
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user