2019-08-29 14:46:54 -07:00
|
|
|
//! Zebrad Config
|
|
|
|
//!
|
|
|
|
//! See instructions in `commands.rs` to specify the path to your
|
|
|
|
//! application's configuration file and/or command-line options
|
|
|
|
//! for specifying it.
|
|
|
|
|
2020-08-05 16:35:56 -07:00
|
|
|
use std::{net::SocketAddr, path::PathBuf};
|
2020-02-14 13:38:33 -08:00
|
|
|
|
2019-08-29 14:46:54 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-08-05 16:35:56 -07:00
|
|
|
use tracing_subscriber::EnvFilter;
|
2019-10-08 13:57:24 -07:00
|
|
|
use zebra_network::Config as NetworkSection;
|
2020-07-20 22:31:11 -07:00
|
|
|
use zebra_state::Config as StateSection;
|
2019-10-08 13:57:24 -07:00
|
|
|
|
2020-06-17 14:44:24 -07:00
|
|
|
/// Configuration for `zebrad`.
|
|
|
|
///
|
|
|
|
/// The `zebrad` config is a TOML-encoded version of this structure. The meaning
|
|
|
|
/// of each field is described in the documentation, although it may be necessary
|
|
|
|
/// to click through to the sub-structures for each section.
|
2019-12-20 11:20:04 -08:00
|
|
|
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
|
2020-06-18 13:34:05 -07:00
|
|
|
#[serde(deny_unknown_fields, default)]
|
2019-08-29 14:46:54 -07:00
|
|
|
pub struct ZebradConfig {
|
2020-02-14 13:38:33 -08:00
|
|
|
/// Metrics configuration
|
|
|
|
pub metrics: MetricsSection,
|
2020-07-20 22:31:11 -07:00
|
|
|
|
|
|
|
/// Networking configuration
|
|
|
|
pub network: NetworkSection,
|
|
|
|
|
|
|
|
/// State configuration
|
|
|
|
pub state: StateSection,
|
|
|
|
|
|
|
|
/// Tracing configuration
|
|
|
|
pub tracing: TracingSection,
|
2019-08-29 14:46:54 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 13:05:42 -07:00
|
|
|
/// Tracing configuration section.
|
2020-07-27 21:35:07 -07:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2020-06-18 13:34:05 -07:00
|
|
|
#[serde(deny_unknown_fields, default)]
|
2019-09-09 13:05:42 -07:00
|
|
|
pub struct TracingSection {
|
|
|
|
/// The filter used for tracing events.
|
2020-08-05 11:48:08 -07:00
|
|
|
///
|
|
|
|
/// The filter is used to create a `tracing-subscriber`
|
|
|
|
/// [`EnvFilter`](https://docs.rs/tracing-subscriber/0.2.10/tracing_subscriber/filter/struct.EnvFilter.html#directives),
|
|
|
|
/// and more details on the syntax can be found there or in the examples
|
|
|
|
/// below.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// `warn,zebrad=info,zebra_network=debug` sets a global `warn` level, an
|
|
|
|
/// `info` level for the `zebrad` crate, and a `debug` level for the
|
|
|
|
/// `zebra_network` crate.
|
|
|
|
///
|
|
|
|
/// ```ascii,no_run
|
|
|
|
/// [block_verify{height=Some\(BlockHeight\(.*000\)\)}]=trace
|
|
|
|
/// ```
|
|
|
|
/// sets `trace` level for all events occurring in the context of a
|
|
|
|
/// `block_verify` span whose `height` field ends in `000`, i.e., traces the
|
|
|
|
/// verification of every 1000th block.
|
2020-06-04 19:34:06 -07:00
|
|
|
pub filter: Option<String>,
|
2020-07-27 21:35:07 -07:00
|
|
|
|
|
|
|
/// The endpoint address used for tracing.
|
|
|
|
pub endpoint_addr: SocketAddr,
|
|
|
|
|
2020-08-05 16:35:56 -07:00
|
|
|
/// The path to write a flamegraph of tracing spans too.
|
|
|
|
///
|
|
|
|
/// This path is not used verbatim when writing out the flamegraph. This is
|
|
|
|
/// because the flamegraph is written out as two parts. First the flamegraph
|
|
|
|
/// is constantly persisted to the disk in a "folded" representation that
|
|
|
|
/// records collapsed stack traces of the tracing spans that are active.
|
|
|
|
/// Then, when the application is finished running the destructor will flush
|
|
|
|
/// the flamegraph output to the folded file and then read that file and
|
|
|
|
/// generate the final flamegraph from it as an SVG.
|
|
|
|
///
|
|
|
|
/// The need to create two files means that we will slightly manipulate the
|
|
|
|
/// path given to us to create the two representations.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// Given `flamegraph = "flamegraph"` we will generate a `flamegraph.svg`
|
|
|
|
/// and a `flamegraph.folded` file in the current directory.
|
|
|
|
///
|
|
|
|
/// If you provide a path with an extension the extension will be ignored and
|
|
|
|
/// replaced with `.folded` and `.svg` for the respective files.
|
|
|
|
pub flamegraph: Option<PathBuf>,
|
2019-08-29 14:46:54 -07:00
|
|
|
}
|
|
|
|
|
2020-06-04 19:34:06 -07:00
|
|
|
impl TracingSection {
|
|
|
|
pub fn populated() -> Self {
|
2019-08-29 14:46:54 -07:00
|
|
|
Self {
|
2020-06-04 19:34:06 -07:00
|
|
|
filter: Some("info".to_owned()),
|
2020-07-27 21:35:07 -07:00
|
|
|
endpoint_addr: "0.0.0.0:3000".parse().unwrap(),
|
2020-08-05 16:35:56 -07:00
|
|
|
flamegraph: None,
|
2019-08-29 14:46:54 -07:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 16:35:56 -07:00
|
|
|
|
|
|
|
/// Constructs an EnvFilter for use in our tracing subscriber.
|
|
|
|
///
|
|
|
|
/// The env filter controls filtering of spans and events, but not how
|
|
|
|
/// they're emitted. Creating an env filter alone doesn't enable logging, it
|
|
|
|
/// needs to be used in conjunction with other layers like a fmt subscriber,
|
|
|
|
/// for logs, or an error layer, for SpanTraces.
|
|
|
|
pub fn env_filter(&self) -> EnvFilter {
|
|
|
|
self.filter.as_deref().unwrap_or("info").into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TracingSection {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::populated()
|
|
|
|
}
|
2019-08-29 14:46:54 -07:00
|
|
|
}
|
2020-02-14 13:38:33 -08:00
|
|
|
|
|
|
|
/// Metrics configuration section.
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2020-06-18 13:34:05 -07:00
|
|
|
#[serde(deny_unknown_fields, default)]
|
2020-02-14 13:38:33 -08:00
|
|
|
pub struct MetricsSection {
|
2020-07-27 21:35:07 -07:00
|
|
|
/// The endpoint address used for metrics.
|
2020-02-14 13:38:33 -08:00
|
|
|
pub endpoint_addr: SocketAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MetricsSection {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2020-06-18 22:44:59 -07:00
|
|
|
endpoint_addr: "0.0.0.0:9999".parse().unwrap(),
|
2020-02-14 13:38:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 12:24:28 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-06-22 15:36:23 -07:00
|
|
|
use color_eyre::eyre::Result;
|
|
|
|
|
2020-06-09 12:24:28 -07:00
|
|
|
#[test]
|
2020-06-22 15:36:23 -07:00
|
|
|
fn test_toml_ser() -> Result<()> {
|
2020-06-09 12:24:28 -07:00
|
|
|
let default_config = super::ZebradConfig::default();
|
|
|
|
println!("Default config: {:?}", default_config);
|
|
|
|
|
|
|
|
println!("Toml:\n{}", toml::Value::try_from(&default_config)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|