Skip to content

Commit

Permalink
Sample config
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Jan 26, 2024
1 parent 2874464 commit 4f907b3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 22 deletions.
35 changes: 18 additions & 17 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "service-rs"
name = "mpc"
version = "0.1.0"
edition = "2021"

Expand All @@ -20,6 +20,7 @@ tokio = { version = "1.35.1", features = ["macros"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
metrics = "0.21.1"
serde = { version = "1.0.195", features = ["derive"] }

[[bin]]
name = "mpc-node"
Expand Down
26 changes: 22 additions & 4 deletions bin/mpc_node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::PathBuf;

use clap::Parser;
use mpc::config::Config;
use telemetry_batteries::metrics::batteries::StatsdBattery;
use telemetry_batteries::tracing::batteries::DatadogBattery;
use tracing_subscriber::layer::SubscriberExt;
Expand All @@ -17,6 +20,9 @@ pub const METRICS_PREFIX: &str = "mpc-node";
pub struct Args {
#[clap(short, long, env)]
local: bool,

#[clap(short, long, env)]
config: Option<PathBuf>,
}

#[tokio::main]
Expand Down Expand Up @@ -44,20 +50,32 @@ async fn main() -> eyre::Result<()> {
Some(METRICS_PREFIX),
)?;

let mut settings = config::Config::builder();

if let Some(path) = args.config {
settings = settings.add_source(config::File::from(path).required(true));
}

let settings = settings
.add_source(config::Environment::with_prefix("MPC").separator("__"))
.build()?;

let config = settings.try_deserialize::<Config>()?;

let mut n = 0;

loop {
foo(n).await;
foo(&config, n).await;

n += 1;

tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
}
}

#[tracing::instrument]
async fn foo(n: usize) {
tracing::info!(n, "Foo");
#[tracing::instrument(skip(config))]
async fn foo(config: &Config, n: usize) {
tracing::info!(n, test = config.test.test, "Foo");

metrics::gauge!("foo", n as f64);
}
28 changes: 28 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub test: TestConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestConfig {
#[serde(default = "default::test")]
pub test: String,
}

impl Default for TestConfig {
fn default() -> Self {
Self {
test: default::test(),
}
}

}

pub mod default {
pub fn test() -> String {
"default".to_string()
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod config;

0 comments on commit 4f907b3

Please sign in to comment.