Skip to content

Commit

Permalink
Improved metrics (openethereum#240)
Browse files Browse the repository at this point in the history
Added db metrics (kvdb_bytes_read, kvdb_bytes_written, kvdb_reads, kvdb_writes)
Added --metrics-prefix=[prefix]
  • Loading branch information
adria0 committed Mar 3, 2021
1 parent ba011eb commit 0fcb102
Show file tree
Hide file tree
Showing 34 changed files with 437 additions and 186 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions bin/oe/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ usage! {
"--metrics",
"Enable prometheus metrics (only full client).",

ARG arg_metrics_prefix: (String) = "", or |c: &Config| c.metrics.as_ref()?.prefix.clone(),
"--metrics-prefix=[prefix]",
"Prepend the specified prefix to the exported metrics names.",

ARG arg_metrics_port: (u16) = 3000u16, or |c: &Config| c.metrics.as_ref()?.port.clone(),
"--metrics-port=[PORT]",
"Specify the port portion of the metrics server.",
Expand Down Expand Up @@ -922,6 +926,7 @@ struct Ipc {
#[serde(deny_unknown_fields)]
struct Metrics {
enable: Option<bool>,
prefix: Option<String>,
port: Option<u16>,
interface: Option<String>,
}
Expand Down Expand Up @@ -1338,6 +1343,7 @@ mod tests {

// METRICS
flag_metrics: false,
arg_metrics_prefix: "".into(),
arg_metrics_port: 3000u16,
arg_metrics_interface: "local".into(),

Expand Down Expand Up @@ -1542,6 +1548,7 @@ mod tests {
}),
metrics: Some(Metrics {
enable: Some(true),
prefix: Some("oe".to_string()),
interface: Some("local".to_string()),
port: Some(4000),
}),
Expand Down
2 changes: 1 addition & 1 deletion bin/oe/cli/tests/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ apis = ["rpc", "eth"]
enable = true
interface = "local"
port = 4000

prefix = "oe"

[secretstore]
http_port = 8082
Expand Down
5 changes: 5 additions & 0 deletions bin/oe/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ impl Configuration {
fn metrics_config(&self) -> Result<MetricsConfiguration, String> {
let conf = MetricsConfiguration {
enabled: self.metrics_enabled(),
prefix: self.metrics_prefix(),
interface: self.metrics_interface(),
port: self.args.arg_ports_shift + self.args.arg_metrics_port,
};
Expand Down Expand Up @@ -1147,6 +1148,10 @@ impl Configuration {
self.args.flag_metrics
}

fn metrics_prefix(&self) -> String {
self.args.arg_metrics_prefix.clone()
}

fn secretstore_enabled(&self) -> bool {
!self.args.flag_no_secretstore && cfg!(feature = "secretstore")
}
Expand Down
12 changes: 10 additions & 2 deletions bin/oe/db/rocksdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use self::{
};
use blooms_db;
use ethcore::client::ClientConfig;
use kvdb::KeyValueDB;
use ethcore_db::KeyValueDB;
use stats::PrometheusMetrics;
use std::{fs, io, path::Path, sync::Arc};

mod blooms;
Expand Down Expand Up @@ -53,6 +54,10 @@ impl BlockChainDB for AppDB {
}
}

impl PrometheusMetrics for AppDB {
fn prometheus_metrics(&self, _: &mut stats::PrometheusRegistry) {}
}

/// Open a secret store DB using the given secret store data path. The DB path is one level beneath the data path.
#[cfg(feature = "secretstore")]
pub fn open_secretstore_db(data_path: &str) -> Result<Arc<dyn KeyValueDB>, String> {
Expand Down Expand Up @@ -101,8 +106,11 @@ pub fn open_database(
fs::create_dir_all(&blooms_path)?;
fs::create_dir_all(&trace_blooms_path)?;

let db = Database::open(&config, client_path)?;
let db_with_metrics = ethcore_db::DatabaseWithMetrics::new(db);

let db = AppDB {
key_value: Arc::new(Database::open(&config, client_path)?),
key_value: Arc::new(db_with_metrics),
blooms: blooms_db::Database::open(blooms_path)?,
trace_blooms: blooms_db::Database::open(trace_blooms_path)?,
};
Expand Down
27 changes: 18 additions & 9 deletions bin/oe/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use hyper::{service::service_fn_ok, Body, Method, Request, Response, Server, Sta

use stats::{
prometheus::{self, Encoder},
prometheus_gauge, PrometheusMetrics,
PrometheusMetrics, PrometheusRegistry,
};

#[derive(Debug, Clone, PartialEq)]
pub struct MetricsConfiguration {
/// Are metrics enabled (default is false)?
pub enabled: bool,
/// Prefix
pub prefix: String,
/// The IP of the network interface used (default is 127.0.0.1).
pub interface: String,
/// The network port (default is 3000).
Expand All @@ -25,6 +27,7 @@ impl Default for MetricsConfiguration {
fn default() -> Self {
MetricsConfiguration {
enabled: false,
prefix: "".into(),
interface: "127.0.0.1".into(),
port: 3000,
}
Expand All @@ -35,27 +38,30 @@ struct State {
rpc_apis: Arc<rpc_apis::FullDependencies>,
}

fn handle_request(req: Request<Body>, state: Arc<Mutex<State>>) -> Response<Body> {
fn handle_request(
req: Request<Body>,
conf: Arc<MetricsConfiguration>,
state: Arc<Mutex<State>>,
) -> Response<Body> {
let (parts, _body) = req.into_parts();
match (parts.method, parts.uri.path()) {
(Method::GET, "/metrics") => {
let start = Instant::now();

let mut reg = prometheus::Registry::new();
let mut reg = PrometheusRegistry::new(conf.prefix.clone());
let state = state.lock();
state.rpc_apis.client.prometheus_metrics(&mut reg);
state.rpc_apis.sync.prometheus_metrics(&mut reg);
let elapsed = start.elapsed();
prometheus_gauge(
&mut reg,
reg.register_gauge(
"metrics_time",
"Time to perform rpc metrics",
elapsed.as_millis() as i64,
);

let mut buffer = vec![];
let encoder = prometheus::TextEncoder::new();
let metric_families = reg.gather();
let metric_families = reg.registry().gather();

encoder
.encode(&metric_families, &mut buffer)
Expand Down Expand Up @@ -90,17 +96,20 @@ pub fn start_prometheus_metrics(
rpc_apis: deps.apis.clone(),
};
let state = Arc::new(Mutex::new(state));

let conf = Arc::new(conf.to_owned());
let server = Server::bind(&addr)
.serve(move || {
// This is the `Service` that will handle the connection.
// `service_fn_ok` is a helper to convert a function that
// returns a Response into a `Service`.
let state = state.clone();
service_fn_ok(move |req: Request<Body>| handle_request(req, state.clone()))
let conf = conf.clone();
service_fn_ok(move |req: Request<Body>| {
handle_request(req, conf.clone(), state.clone())
})
})
.map_err(|e| eprintln!("server error: {}", e));
println!("Listening on http://{}", addr);
info!("Started prometeus metrics at http://{}/metrics", addr);

deps.executor.spawn(server);

Expand Down
1 change: 1 addition & 0 deletions crates/concensus/miner/local-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
common-types = { path = "../../../ethcore/types" }
ethcore-io = { path = "../../../runtime/io" }
ethcore-db = { path = "../../../db/db"}
kvdb = "0.1"
log = "0.4"
rlp = { version = "0.3.0", features = ["ethereum"] }
Expand Down
9 changes: 5 additions & 4 deletions crates/concensus/miner/local-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

use std::{fmt, sync::Arc, time::Duration};

use ethcore_db::KeyValueDB;
use io::IoHandler;
use kvdb::KeyValueDB;
use types::transaction::{
Condition as TransactionCondition, PendingTransaction, SignedTransaction, TypedTransaction,
UnverifiedTransaction,
};

extern crate common_types as types;
extern crate ethcore_db;
extern crate ethcore_io as io;
extern crate kvdb;
extern crate rlp;
Expand Down Expand Up @@ -253,7 +254,7 @@ mod tests {

#[test]
fn twice_empty() {
let db = Arc::new(::kvdb_memorydb::create(0));
let db = Arc::new(ethcore_db::InMemoryWithMetrics::create(0));

{
let store = super::create(db.clone(), None, Dummy(vec![]));
Expand Down Expand Up @@ -284,7 +285,7 @@ mod tests {
})
.collect();

let db = Arc::new(::kvdb_memorydb::create(0));
let db = Arc::new(ethcore_db::InMemoryWithMetrics::create(0));

{
// nothing written yet, will write pending.
Expand Down Expand Up @@ -325,7 +326,7 @@ mod tests {
PendingTransaction::new(signed, None)
});

let db = Arc::new(::kvdb_memorydb::create(0));
let db = Arc::new(ethcore_db::InMemoryWithMetrics::create(0));
{
// nothing written, will write bad.
let store = super::create(db.clone(), None, Dummy(transactions.clone()));
Expand Down
3 changes: 3 additions & 0 deletions crates/db/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ common-types = { path = "../../ethcore/types" }
ethereum-types = "0.4"
heapsize = "0.4"
kvdb = "0.1"
kvdb-rocksdb = "0.1.3"
kvdb-memorydb = "0.1"
parking_lot = "0.7"
rlp = { version = "0.3.0", features = ["ethereum"] }
rlp_derive = { path = "../../util/rlp-derive" }
stats = { path = "../../util/stats" }
Loading

0 comments on commit 0fcb102

Please sign in to comment.