Browse Source

Add support for Prometheus monitoring

refactor-mempool
Roman Zeyde 6 years ago
parent
commit
c3682ade1f
No known key found for this signature in database GPG Key ID: 87CAE5FA46917CBB
  1. 2
      Cargo.toml
  2. 8
      src/config.rs
  3. 3
      src/lib.rs
  4. 44
      src/metrics.rs

2
Cargo.toml

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

8
src/config.rs

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

3
src/lib.rs

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

44
src/metrics.rs

@ -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)
}
}
Loading…
Cancel
Save