Skip to content

Commit

Permalink
feat: Add support for using standard OTel configuration headers
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Sep 4, 2022
1 parent 3657285 commit 43b9151
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 175 deletions.
68 changes: 68 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lazy_static = "1.4"
nix = "0.25.0"
once_cell = "1.13"
opentelemetry = { version = "0.17", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.10", features = ["tonic", "tls"] }
opentelemetry-otlp = { version = "0.10", features = ["tonic", "tls-roots"] }
reqwest = { version = "0.11.11", default-features = false, features = ["rustls-tls", "json", "blocking"] }
rustls = "0.20"
serde = { version = "1.0", features = ["derive", "rc"] }
Expand Down
51 changes: 34 additions & 17 deletions src/commands/apply.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::path::PathBuf;

use clap::Arg;
use tracing::{info_span, instrument};
use opentelemetry::trace::SpanKind;
use tracing::{info_span, instrument};

use super::*;

Expand Down Expand Up @@ -30,21 +30,25 @@ impl Command for ApplyCommand {

impl CommandRunnable for ApplyCommand {
#[instrument(name = "command.apply", fields(otel.kind = %SpanKind::Client), skip(self, matches), err)]
fn run(
&self,
matches: &clap::ArgMatches,
) -> Result<i32, crate::errors::Error> {
let config_dir: PathBuf = matches.value_of("config")
.map(|p| p.into())
.ok_or_else(|| errors::user("No configuration directory provided.", "Provide the --config directory when running this command."))?;
fn run(&self, matches: &clap::ArgMatches) -> Result<i32, crate::errors::Error> {
let config_dir: PathBuf =
matches
.value_of("config")
.map(|p| p.into())
.ok_or_else(|| {
errors::user(
"No configuration directory provided.",
"Provide the --config directory when running this command.",
)
})?;

let mut output = crate::core::output::output();

let config = crate::core::config::load_all_config(&config_dir.join("config"))?;
for (key, val) in config.iter() {
writeln!(output, " = config {}={}", key, val)?;
}

let secrets = crate::core::config::load_all_config(&config_dir.join("secrets"))?;
for (key, _val) in secrets.iter() {
writeln!(output, " = secret {}=******", key)?;
Expand Down Expand Up @@ -73,8 +77,17 @@ impl CommandRunnable for ApplyCommand {
let root_path = PathBuf::from("/");
let files = package.get_files()?;
for file in files {
let target_path = package.files.get(&file.group).map(|f| f.as_path()).unwrap_or(&root_path);
writeln!(output, " + {} '{}'", if file.is_template { "template" } else { "file" }, target_path.join(&file.relative_path).display())?;
let target_path = package
.files
.get(&file.group)
.map(|f| f.as_path())
.unwrap_or(&root_path);
writeln!(
output,
" + {} '{}'",
if file.is_template { "template" } else { "file" },
target_path.join(&file.relative_path).display()
)?;

file.apply(target_path, &config, &secrets)?;
}
Expand All @@ -101,13 +114,17 @@ mod tests {
fn run() {
let _guard = test_tracing();
let temp = tempfile::tempdir().unwrap();

let cmd = ApplyCommand{};

let args = cmd.app().get_matches_from(vec!["apply", "--config", get_test_data().to_str().unwrap()]);


let cmd = ApplyCommand {};

let args = cmd.app().get_matches_from(vec![
"apply",
"--config",
get_test_data().to_str().unwrap(),
]);

let output = crate::core::output::mock();

let temp_path = temp.path().to_owned();
crate::core::file::File::apply.mock_safe(move |f, target, config, secrets| {
let target = Box::leak(Box::new(temp_path.join(target.strip_prefix("/").unwrap())));
Expand Down
49 changes: 32 additions & 17 deletions src/commands/plan.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::path::PathBuf;
use crate::errors;
use clap::Arg;
use tracing::{info_span, instrument};
use opentelemetry::trace::SpanKind;
use crate::errors;
use std::path::PathBuf;
use tracing::{info_span, instrument};

use super::*;

Expand Down Expand Up @@ -30,21 +30,25 @@ impl Command for PlanCommand {

impl CommandRunnable for PlanCommand {
#[instrument(name = "command.plan", fields(otel.kind = %SpanKind::Client), skip(self, matches), err)]
fn run(
&self,
matches: &clap::ArgMatches,
) -> Result<i32, crate::errors::Error> {
let config_dir: PathBuf = matches.value_of("config")
.map(|p| p.into())
.ok_or_else(|| errors::user("No configuration directory provided.", "Provide the --config directory when running this command."))?;
fn run(&self, matches: &clap::ArgMatches) -> Result<i32, crate::errors::Error> {
let config_dir: PathBuf =
matches
.value_of("config")
.map(|p| p.into())
.ok_or_else(|| {
errors::user(
"No configuration directory provided.",
"Provide the --config directory when running this command.",
)
})?;

let mut output = crate::core::output::output();

let config = crate::core::config::load_all_config(&config_dir.join("config"))?;
for (key, val) in config {
writeln!(output, " = config {}={}", key, val)?;
}

let secrets = crate::core::config::load_all_config(&config_dir.join("secrets"))?;
for (key, _val) in secrets {
writeln!(output, " = secret {}=******", key)?;
Expand All @@ -70,8 +74,17 @@ impl CommandRunnable for PlanCommand {
let root_path = PathBuf::from("/");
let files = package.get_files()?;
for file in files {
let group = package.files.get(&file.group).map(|f| f.as_path()).unwrap_or(&root_path);
writeln!(output, " + {} '{}'", if file.is_template { "template" } else { "file" }, group.join(file.relative_path).display())?;
let group = package
.files
.get(&file.group)
.map(|f| f.as_path())
.unwrap_or(&root_path);
writeln!(
output,
" + {} '{}'",
if file.is_template { "template" } else { "file" },
group.join(file.relative_path).display()
)?;
}

let tasks = package.get_tasks()?;
Expand All @@ -86,16 +99,18 @@ impl CommandRunnable for PlanCommand {

#[cfg(test)]
mod tests {
use super::*;
use crate::test::{get_test_data, test_tracing};
use mocktopus::mocking::*;
use super::*;

#[test]
fn run() {
let _guard = test_tracing();

let cmd = PlanCommand {};
let args = cmd.app().get_matches_from(vec!["plan", "--config", get_test_data().to_str().unwrap()]);
let args =
cmd.app()
.get_matches_from(vec!["plan", "--config", get_test_data().to_str().unwrap()]);

let output = crate::core::output::mock();

Expand Down
51 changes: 33 additions & 18 deletions src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, path::Path};
use std::fs::read_to_string;
use std::process;
use tracing::field::display;
use tracing::{Span, instrument};
use tracing::{instrument, Span};

use crate::errors;

Expand All @@ -13,33 +13,43 @@ use mocktopus::macros::*;
#[instrument(level = "debug", name = "config.load_all", err)]
pub fn load_all_config(dir: &Path) -> Result<HashMap<String, String>, errors::Error> {
if !dir.exists() {
return Ok(HashMap::new())
return Ok(HashMap::new());
}

dir.read_dir()
.map(|dirs| dirs.filter_map(|dir| match dir {
Ok(d) => match d.file_type() {
Ok(ft) if ft.is_file() => Some(d.path()),
_ => None
},
_ => None,
}))
.map_err(|err| errors::user_with_internal(
.map(|dirs| {
dirs.filter_map(|dir| match dir {
Ok(d) => match d.file_type() {
Ok(ft) if ft.is_file() => Some(d.path()),
_ => None,
},
_ => None,
})
})
.map_err(|err| {
errors::user_with_internal(
"Failed to read the list of configuration files.",
"Read the internal error message and take the appropriate steps to resolve the issue.",
err))
err)
})
.and_then(|files| {
let mut output = HashMap::new();

let mut errs: Vec<errors::Error> = files.map(|file| load_config(dunce::simplified(&file)).map(|config| {
for (key, val) in config {
output.insert(key,val);
}
})).filter(|r| r.is_err()).map(|r| r.unwrap_err()).collect();
let mut errs: Vec<errors::Error> = files
.map(|file| {
load_config(dunce::simplified(&file)).map(|config| {
for (key, val) in config {
output.insert(key, val);
}
})
})
.filter(|r| r.is_err())
.map(|r| r.unwrap_err())
.collect();

match errs.pop() {
Some(err) => Err(err),
None => Ok(output)
None => Ok(output),
}
})
}
Expand Down Expand Up @@ -86,7 +96,12 @@ fn load_env_config(file: &Path) -> Result<String, errors::Error> {
}

#[cfg_attr(test, mockable)]
#[instrument(level = "debug", name = "config.load.script", fields(stdout, stderr), err)]
#[instrument(
level = "debug",
name = "config.load.script",
fields(stdout, stderr),
err
)]
pub fn load_script_config(interpreter: &str, file: &Path) -> Result<String, errors::Error> {
process::Command::new(interpreter)
.arg(file)
Expand Down
2 changes: 1 addition & 1 deletion src/core/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, path::Path};
use walkdir::WalkDir;

use gtmpl::{template, Value};
use tracing::{instrument};
use tracing::instrument;

use crate::errors;

Expand Down
2 changes: 1 addition & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod config;
pub mod file;
pub mod output;
pub mod package;
pub mod script;
pub mod script;
Loading

0 comments on commit 43b9151

Please sign in to comment.