Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl BenchDb {
None,
None,
ExecutionExtensions::new(profile.into_execution_strategies(), None),
None,
).expect("Should not fail");

(client, backend)
Expand Down
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sp-blockchain = { version = "2.0.0-alpha.2", path = "../primitives/blockchain" }
sp-state-machine = { version = "0.8.0-alpha.2", path = "../primitives/state-machine" }
sc-telemetry = { version = "2.0.0-alpha.2", path = "telemetry" }
sp-trie = { version = "2.0.0-alpha.2", path = "../primitives/trie" }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0-alpha.2", path = "../utils/prometheus" }
tracing = "0.1.10"

[dev-dependencies]
Expand Down
12 changes: 7 additions & 5 deletions client/cli/src/commands/runcmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use regex::Regex;
use chrono::prelude::*;
use sc_service::{
AbstractService, Configuration, ChainSpecExtension, RuntimeGenesis, ChainSpec, Roles,
config::KeystoreConfig,
config::{KeystoreConfig, PrometheusConfig},
};
use sc_telemetry::TelemetryEndpoints;

Expand Down Expand Up @@ -423,11 +423,13 @@ impl RunCmd {

// Override prometheus
if self.no_prometheus {
config.prometheus_port = None;
} else if config.prometheus_port.is_none() {
config.prometheus_config = None;
} else if config.prometheus_config.is_none() {
let prometheus_interface: &str = if self.prometheus_external { "0.0.0.0" } else { "127.0.0.1" };
config.prometheus_port = Some(
parse_address(&format!("{}:{}", prometheus_interface, 9615), self.prometheus_port)?);
config.prometheus_config = Some(PrometheusConfig {
port: parse_address(&format!("{}:{}", prometheus_interface, 9615), self.prometheus_port)?,
registry: None
});
}

config.tracing_targets = self.import_params.tracing_targets.clone().into();
Expand Down
1 change: 1 addition & 0 deletions client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sc-state-db = { version = "0.8.0-alpha.2", path = "../state-db" }
sp-trie = { version = "2.0.0-alpha.2", path = "../../primitives/trie" }
sp-consensus = { version = "0.8.0-alpha.2", path = "../../primitives/consensus/common" }
sp-blockchain = { version = "2.0.0-alpha.2", path = "../../primitives/blockchain" }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0-alpha.2", path = "../../utils/prometheus" }

[dev-dependencies]
sp-keyring = { version = "2.0.0-alpha.2", path = "../../primitives/keyring" }
Expand Down
3 changes: 3 additions & 0 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ use crate::storage_cache::{CachingState, SharedCache, new_shared_cache};
use crate::stats::StateUsageStats;
use log::{trace, debug, warn};
pub use sc_state_db::PruningMode;
use prometheus_endpoint::Registry;

#[cfg(any(feature = "kvdb-rocksdb", test))]
pub use bench::BenchmarkingState;
Expand Down Expand Up @@ -291,6 +292,7 @@ pub fn new_client<E, S, Block, RA>(
fork_blocks: ForkBlocks<Block>,
bad_blocks: BadBlocks<Block>,
execution_extensions: ExecutionExtensions<Block>,
prometheus_registry: Option<Registry>,
) -> Result<(
sc_client::Client<
Backend<Block>,
Expand All @@ -317,6 +319,7 @@ pub fn new_client<E, S, Block, RA>(
fork_blocks,
bad_blocks,
execution_extensions,
prometheus_registry,
)?,
backend,
))
Expand Down
79 changes: 34 additions & 45 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use crate::{Service, NetworkStatus, NetworkState, error::Error, DEFAULT_PROTOCOL_ID, MallocSizeOfWasm};
use crate::{SpawnTaskHandle, start_rpc_servers, build_network_future, TransactionPoolAdapter};
use crate::status_sinks;
use crate::config::{Configuration, DatabaseConfig, KeystoreConfig};
use crate::config::{Configuration, DatabaseConfig, KeystoreConfig, PrometheusConfig};
use sc_client_api::{
self,
BlockchainEvents,
Expand Down Expand Up @@ -46,7 +46,7 @@ use sc_executor::{NativeExecutor, NativeExecutionDispatch};
use std::{
borrow::Cow,
io::{Read, Write, Seek},
marker::PhantomData, sync::Arc, pin::Pin
marker::PhantomData, sync::Arc, pin::Pin, net::SocketAddr,
};
use wasm_timer::SystemTime;
use sysinfo::{get_current_pid, ProcessExt, System, SystemExt};
Expand Down Expand Up @@ -92,6 +92,16 @@ impl ServiceMetrics {
}
}

fn get_registry_and_port(config: Option<&PrometheusConfig>) -> Result<Option<(Registry, SocketAddr)>, PrometheusError> {
Ok(match config {
Some(config) => match config.registry.as_ref() {
Some(registry) => Some((registry.clone(), config.port)),
None => Some((Registry::new_custom(Some("substrate".into()), None)?, config.port))
Comment thread
expenses marked this conversation as resolved.
Outdated
},
None => None
})
}

pub type BackgroundTask = Pin<Box<dyn Future<Output=()> + Send>>;

/// Aggregator for the components required to build a service.
Expand Down Expand Up @@ -128,7 +138,7 @@ pub struct ServiceBuilder<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TF
remote_backend: Option<Arc<dyn RemoteBlockchain<TBl>>>,
marker: PhantomData<(TBl, TRtApi)>,
background_tasks: Vec<(&'static str, BackgroundTask)>,
prometheus_registry: Option<Registry>
prometheus_registry_and_port: Option<(Registry, SocketAddr)>,
Comment thread
expenses marked this conversation as resolved.
Outdated
}

/// Full client type.
Expand Down Expand Up @@ -181,6 +191,7 @@ type TFullParts<TBl, TRtApi, TExecDisp> = (
TFullClient<TBl, TRtApi, TExecDisp>,
Arc<TFullBackend<TBl>>,
Arc<RwLock<sc_keystore::Store>>,
Option<(Registry, SocketAddr)>,
);

/// Creates a new full client for the given config.
Expand Down Expand Up @@ -230,6 +241,8 @@ fn new_full_parts<TBl, TRtApi, TExecDisp, TGen, TCSExt>(
.cloned()
.unwrap_or_default();

let prometheus_registry_and_port = get_registry_and_port(config.prometheus_config.as_ref())?;

let (client, backend) = {
let db_config = sc_client_db::DatabaseSettings {
state_cache_size: config.state_cache_size,
Expand Down Expand Up @@ -259,10 +272,11 @@ fn new_full_parts<TBl, TRtApi, TExecDisp, TGen, TCSExt>(
fork_blocks,
bad_blocks,
extensions,
prometheus_registry_and_port.as_ref().map(|(r, _)| r.clone()),
)?
};

Ok((client, backend, keystore))
Ok((client, backend, keystore, prometheus_registry_and_port))
}

impl<TGen, TCSExt> ServiceBuilder<(), (), TGen, TCSExt, (), (), (), (), (), (), (), (), ()>
Expand All @@ -285,12 +299,11 @@ where TGen: RuntimeGenesis, TCSExt: Extension {
(),
TFullBackend<TBl>,
>, Error> {
let (client, backend, keystore) = new_full_parts(&config)?;
let (client, backend, keystore, prometheus_registry_and_port) = new_full_parts(&config)?;

let client = Arc::new(client);

Ok(ServiceBuilder {
config,
client,
backend,
keystore,
Expand All @@ -304,7 +317,8 @@ where TGen: RuntimeGenesis, TCSExt: Extension {
remote_backend: None,
background_tasks: Default::default(),
marker: PhantomData,
prometheus_registry: None,
prometheus_registry_and_port,
config,
})
}

Expand Down Expand Up @@ -368,14 +382,17 @@ where TGen: RuntimeGenesis, TCSExt: Extension {
let fetcher = Arc::new(sc_network::config::OnDemand::new(fetch_checker));
let backend = sc_client::light::new_light_backend(light_blockchain);
let remote_blockchain = backend.remote_blockchain();

let prometheus_registry_and_port = get_registry_and_port(config.prometheus_config.as_ref())?;

let client = Arc::new(sc_client::light::new_light(
backend.clone(),
config.expect_chain_spec(),
executor,
prometheus_registry_and_port.as_ref().map(|(r, _)| r.clone()),
)?);

Ok(ServiceBuilder {
config,
client,
backend,
keystore,
Expand All @@ -389,7 +406,8 @@ where TGen: RuntimeGenesis, TCSExt: Extension {
remote_backend: Some(remote_blockchain),
background_tasks: Default::default(),
marker: PhantomData,
prometheus_registry: None,
prometheus_registry_and_port,
config,
})
}
}
Expand Down Expand Up @@ -462,7 +480,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, T
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
prometheus_registry: self.prometheus_registry,
prometheus_registry_and_port: self.prometheus_registry_and_port,
})
}

Expand Down Expand Up @@ -505,7 +523,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, T
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
prometheus_registry: self.prometheus_registry,
prometheus_registry_and_port: self.prometheus_registry_and_port,
})
}

Expand Down Expand Up @@ -545,7 +563,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, T
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
prometheus_registry: self.prometheus_registry,
prometheus_registry_and_port: self.prometheus_registry_and_port,
})
}

Expand Down Expand Up @@ -609,7 +627,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, T
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
prometheus_registry: self.prometheus_registry,
prometheus_registry_and_port: self.prometheus_registry_and_port,
})
}

Expand Down Expand Up @@ -669,7 +687,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, T
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
prometheus_registry: self.prometheus_registry,
prometheus_registry_and_port: self.prometheus_registry_and_port,
})
}

Expand Down Expand Up @@ -697,30 +715,9 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, T
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
prometheus_registry: self.prometheus_registry,
prometheus_registry_and_port: self.prometheus_registry_and_port,
})
}

/// Use an existing prometheus `Registry` to record metrics into.
pub fn with_prometheus_registry(self, registry: Registry) -> Self {
Self {
config: self.config,
client: self.client,
backend: self.backend,
keystore: self.keystore,
fetcher: self.fetcher,
select_chain: self.select_chain,
import_queue: self.import_queue,
finality_proof_request_builder: self.finality_proof_request_builder,
finality_proof_provider: self.finality_proof_provider,
transaction_pool: self.transaction_pool,
rpc_extensions: self.rpc_extensions,
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
prometheus_registry: Some(registry),
}
}
}

/// Implemented on `ServiceBuilder`. Allows running block commands, such as import/export/validate
Expand Down Expand Up @@ -830,7 +827,7 @@ ServiceBuilder<
rpc_extensions,
remote_backend,
background_tasks,
prometheus_registry,
prometheus_registry_and_port,
} = self;

sp_session::generate_initial_session_keys(
Expand Down Expand Up @@ -888,14 +885,6 @@ ServiceBuilder<
let block_announce_validator =
Box::new(sp_consensus::block_validation::DefaultBlockAnnounceValidator::new(client.clone()));

let prometheus_registry_and_port = match config.prometheus_port {
Some(port) => match prometheus_registry {
Some(registry) => Some((registry, port)),
None => Some((Registry::new_custom(Some("substrate".into()), None)?, port))
},
None => None
};

let network_params = sc_network::config::Params {
roles: config.roles,
executor: {
Expand Down
15 changes: 12 additions & 3 deletions client/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use sc_chain_spec::{ChainSpec, NoExtension};
use sp_core::crypto::Protected;
use target_info::Target;
use sc_telemetry::TelemetryEndpoints;
use prometheus_endpoint::Registry;

/// Executable version. Used to pass version information from the root crate.
#[derive(Clone)]
Expand Down Expand Up @@ -93,8 +94,8 @@ pub struct Configuration<G, E = NoExtension> {
pub rpc_ws_max_connections: Option<usize>,
/// CORS settings for HTTP & WS servers. `None` if all origins are allowed.
pub rpc_cors: Option<Vec<String>>,
/// Prometheus endpoint Port. `None` if disabled.
pub prometheus_port: Option<SocketAddr>,
/// Prometheus endpoint configuration. `None` if disabled.
pub prometheus_config: Option<PrometheusConfig>,
/// Telemetry service URL. `None` if disabled.
pub telemetry_endpoints: Option<TelemetryEndpoints>,
/// External WASM transport for the telemetry. If `Some`, when connection to a telemetry
Expand Down Expand Up @@ -165,6 +166,14 @@ pub enum DatabaseConfig {
Custom(Arc<dyn KeyValueDB>),
}

/// Configuration of the Prometheus endpoint.
pub struct PrometheusConfig {
/// Port to use.
pub port: SocketAddr,
/// A metrics registry to use. One will be created if `None`.
pub registry: Option<Registry>,
}

impl<G, E> Default for Configuration<G, E> {
/// Create a default config
fn default() -> Self {
Expand All @@ -190,7 +199,7 @@ impl<G, E> Default for Configuration<G, E> {
rpc_ws: None,
rpc_ws_max_connections: None,
rpc_cors: Some(vec![]),
prometheus_port: None,
prometheus_config: None,
telemetry_endpoints: None,
telemetry_external_transport: None,
default_heap_pages: None,
Expand Down
2 changes: 1 addition & 1 deletion client/service/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn node_config<G, E: Clone> (
rpc_ws: None,
rpc_ws_max_connections: None,
rpc_cors: None,
prometheus_port: None,
prometheus_config: None,
telemetry_endpoints: None,
telemetry_external_transport: None,
default_heap_pages: None,
Expand Down
Loading