Skip to content

Commit

Permalink
Configure all lading programs through files (#99)
Browse files Browse the repository at this point in the history
Previously we allowed for some lading programs to be configured through
command-line flags. This makes lading challenging to use in contexts where
file-based configuration is more convenient, so we now have every lading binary
read a config file from under `/etc/lading`.

Signed-off-by: Brian L. Troutwine <[email protected]>
  • Loading branch information
blt authored Oct 14, 2021
1 parent 4dd9503 commit 8693fce
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 28 deletions.
7 changes: 4 additions & 3 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 lading_blackholes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lading_blackholes"
version = "0.4.8"
version = "0.5.0"
authors = ["Brian L. Troutwine <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -13,6 +13,7 @@ description = "Blackhole programs with some instrumentation"
tower = { version = "0.4", default-features = false, features = ["timeout", "limit", "load-shed"] }
metrics = { version = "0.17", default-features = false, features = ["std"] }
metrics-exporter-prometheus = { version = "0.6", default-features = false, features = ["tokio-exporter"] }
toml = "0.5"

[dependencies.argh]
version = "0.1"
Expand Down
43 changes: 32 additions & 11 deletions lading_blackholes/src/bin/http_blackhole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use hyper::{body, header};
use hyper::{Body, Request, Response, Server, StatusCode};
use metrics_exporter_prometheus::PrometheusBuilder;
use once_cell::unsync::OnceCell;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::io::Read;
use std::net::SocketAddr;
use std::str::FromStr;
use std::time::Duration;
Expand All @@ -20,7 +21,11 @@ fn default_concurrent_requests_max() -> usize {
100
}

#[derive(Debug, Copy, Clone)]
fn default_config_path() -> String {
"/etc/lading/http_blackhole.toml".to_string()
}

#[derive(Debug, Copy, Clone, Deserialize)]
enum BodyVariant {
Nothing,
AwsKinesis,
Expand All @@ -44,20 +49,25 @@ fn default_body_variant() -> BodyVariant {
#[derive(FromArgs)]
/// `http_blackhole` options
struct Opts {
/// path on disk to the configuration file
#[argh(option, default = "default_config_path()")]
config_path: String,
}

#[derive(Debug, Deserialize)]
/// Main configuration struct for this program
struct Config {
/// number of worker threads to use in this program
#[argh(option)]
pub worker_threads: u16,
/// number of concurrent HTTP connections to allow
#[argh(option, default = "default_concurrent_requests_max()")]
#[serde(default = "default_concurrent_requests_max")]
pub concurrent_requests_max: usize,
/// address -- IP plus port -- to bind to
#[argh(option)]
pub binding_addr: SocketAddr,
/// address -- IP plus port -- for prometheus exporting to bind to
#[argh(option)]
pub prometheus_addr: SocketAddr,
/// the body variant to respond with, default nothing
#[argh(option, default = "default_body_variant()")]
#[serde(default = "default_body_variant")]
pub body_variant: BodyVariant,
}

Expand Down Expand Up @@ -159,16 +169,27 @@ impl HttpServer {
}
}

fn main() {
fn get_config() -> Config {
let ops: Opts = argh::from_env();
let httpd = HttpServer::new(ops.binding_addr, ops.prometheus_addr);
let mut file: std::fs::File = std::fs::OpenOptions::new()
.read(true)
.open(ops.config_path)
.unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
toml::from_str(&contents).unwrap()
}

fn main() {
let config: Config = get_config();
let httpd = HttpServer::new(config.binding_addr, config.prometheus_addr);
let runtime = Builder::new_multi_thread()
.worker_threads(ops.worker_threads as usize)
.worker_threads(config.worker_threads as usize)
.enable_io()
.enable_time()
.build()
.unwrap();
runtime
.block_on(httpd.run(ops.body_variant, ops.concurrent_requests_max))
.block_on(httpd.run(config.body_variant, config.concurrent_requests_max))
.unwrap();
}
46 changes: 39 additions & 7 deletions lading_blackholes/src/bin/udp_blackhole.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
use argh::FromArgs;
use metrics::counter;
use metrics_exporter_prometheus::PrometheusBuilder;
use serde::Deserialize;
use std::io;
use std::io::Read;
use std::net::SocketAddr;
use tokio::net::UdpSocket;
use tokio::runtime::Builder;

fn default_config_path() -> String {
"/etc/lading/udp_blackhole.toml".to_string()
}

#[derive(FromArgs)]
/// `udp_blackhole` options
struct Opts {
/// path on disk to the configuration file
#[argh(option, default = "default_config_path()")]
config_path: String,
}

/// Main configuration struct for this program
#[derive(Debug, Deserialize)]
pub struct Config {
/// number of worker threads to use in this program
#[argh(option)]
pub worker_threads: u16,
/// address -- IP plus port -- to bind to
#[argh(option)]
pub binding_addr: SocketAddr,
/// Address and port for prometheus exporter
pub prometheus_addr: SocketAddr,
}

struct Server {
addr: SocketAddr,
prom_addr: SocketAddr,
}

impl Server {
fn new(addr: SocketAddr) -> Self {
Self { addr }
fn new(addr: SocketAddr, prom_addr: SocketAddr) -> Self {
Self { addr, prom_addr }
}

async fn run(self) -> Result<(), io::Error> {
let _: () = PrometheusBuilder::new()
.listen_address(self.prom_addr)
.install()
.unwrap();

let socket = UdpSocket::bind(&self.addr).await?;
let mut buf: Vec<u8> = vec![0; 4096];

Expand All @@ -36,11 +57,22 @@ impl Server {
}
}

fn main() {
fn get_config() -> Config {
let ops: Opts = argh::from_env();
let server = Server::new(ops.binding_addr);
let mut file: std::fs::File = std::fs::OpenOptions::new()
.read(true)
.open(ops.config_path)
.unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
toml::from_str(&contents).unwrap()
}

fn main() {
let config: Config = get_config();
let server = Server::new(config.binding_addr, config.prometheus_addr);
let runtime = Builder::new_multi_thread()
.worker_threads(ops.worker_threads as usize)
.worker_threads(config.worker_threads as usize)
.enable_io()
.build()
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion lading_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lading_common"
version = "0.4.8"
version = "0.5.0"
authors = ["Brian L. Troutwine <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion lading_generators/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "lading_generators"
readme = "README.md"
version = "0.4.8"
version = "0.5.0"
authors = ["Brian L. Troutwine <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
6 changes: 5 additions & 1 deletion lading_generators/src/bin/file_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ use std::net::SocketAddr;
use std::{fs, mem};
use tokio::runtime::Builder;

fn default_config_path() -> String {
"/etc/lading/file_gen.toml".to_string()
}

#[derive(FromArgs)]
/// `file_gen` options
struct Opts {
/// path on disk to the configuration file
#[argh(option)]
#[argh(option, default = "default_config_path()")]
config_path: String,
}

Expand Down
6 changes: 5 additions & 1 deletion lading_generators/src/bin/http_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ use std::io::Read;
use std::net::SocketAddr;
use tokio::runtime::Builder;

fn default_config_path() -> String {
"/etc/lading/http_gen.toml".to_string()
}

#[derive(FromArgs)]
/// `http_gen` options
struct Opts {
/// path on disk to the configuration file
#[argh(option)]
#[argh(option, default = "default_config_path()")]
config_path: String,
}

Expand Down
6 changes: 5 additions & 1 deletion lading_generators/src/bin/kafka_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ use std::io::Read;
use std::net::SocketAddr;
use tokio::runtime::Builder;

fn default_config_path() -> String {
"/etc/lading/kafka_gen.toml".to_string()
}

#[derive(FromArgs)]
/// `kafka_gen` options
struct Opts {
/// path on disk to the configuration file
#[argh(option)]
#[argh(option, default = "default_config_path()")]
config_path: String,
}

Expand Down
6 changes: 5 additions & 1 deletion lading_generators/src/bin/tcp_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ use std::io::Read;
use std::net::SocketAddr;
use tokio::runtime::Builder;

fn default_config_path() -> String {
"/etc/lading/tcp_gen.toml".to_string()
}

#[derive(FromArgs)]
/// `tcp_gen` options
struct Opts {
/// path on disk to the configuration file
#[argh(option)]
#[argh(option, default = "default_config_path()")]
config_path: String,
}

Expand Down

0 comments on commit 8693fce

Please sign in to comment.