Add support for Prometheus monitoring

This commit is contained in:
Roman Zeyde 2018-06-12 18:47:28 +03:00
parent 61fbd4d3ca
commit c3682ade1f
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
4 changed files with 54 additions and 3 deletions

View File

@ -15,6 +15,7 @@ error-chain = "0.11"
hex = "0.3"
log = "0.4"
pbr = "1.0"
prometheus = "0.4"
rocksdb = "0.10"
rust-crypto = "0.2"
serde = "1.0"
@ -22,3 +23,4 @@ serde_derive = "1.0"
serde_json = "1.0"
simplelog = "0.5"
time = "0.1"
tiny_http = "0.6"

View File

@ -9,9 +9,10 @@ pub struct Config {
pub log_file: String,
pub log_level: simplelog::LevelFilter,
pub restart: bool,
pub network_type: Network, // bitcoind JSONRPC endpoint
pub db_path: String, // RocksDB directory path
pub rpc_addr: SocketAddr, // for serving Electrum clients
pub network_type: Network, // bitcoind JSONRPC endpoint
pub db_path: String, // RocksDB directory path
pub rpc_addr: SocketAddr, // for serving Electrum clients
pub monitoring_addr: SocketAddr, // for Prometheus monitoring
}
impl Config {
@ -73,6 +74,7 @@ impl Config {
Network::Testnet => "127.0.0.1:60001",
}.parse()
.unwrap(),
monitoring_addr: "127.0.0.1:42024".parse().unwrap(),
};
setup_logging(&config);
config

View File

@ -8,10 +8,12 @@ extern crate chan_signal;
extern crate crypto;
extern crate hex;
extern crate pbr;
extern crate prometheus;
extern crate rocksdb;
extern crate serde;
extern crate simplelog;
extern crate time;
extern crate tiny_http;
#[macro_use]
extern crate chan;
@ -32,6 +34,7 @@ pub mod daemon;
pub mod errors;
pub mod index;
pub mod mempool;
pub mod metrics;
pub mod query;
pub mod rpc;
pub mod signal;

44
src/metrics.rs Normal file
View File

@ -0,0 +1,44 @@
use prometheus::{self, Encoder};
use std::io;
use std::net::SocketAddr;
use tiny_http;
pub use prometheus::{IntCounter as Counter, Opts as MetricOpts};
pub struct Metrics {
reg: prometheus::Registry,
addr: SocketAddr,
}
impl Metrics {
pub fn new(addr: SocketAddr) -> Metrics {
Metrics {
reg: prometheus::Registry::new(),
addr,
}
}
pub fn counter(&self, opts: prometheus::Opts) -> Counter {
let cnt = Counter::with_opts(opts).unwrap();
self.reg.register(Box::new(cnt.clone())).unwrap();
cnt
}
pub fn serve(&self) {
let server = tiny_http::Server::http(self.addr).unwrap();
loop {
if let Err(e) = self.handle(server.recv()) {
error!("http error: {}", e);
}
}
}
fn handle(&self, req: io::Result<tiny_http::Request>) -> io::Result<()> {
let request = req?;
let mut buffer = vec![];
prometheus::TextEncoder::new()
.encode(&self.reg.gather(), &mut buffer)
.unwrap();
let response = tiny_http::Response::from_data(buffer);
request.respond(response)
}
}