Skip to content

Commit

Permalink
Move metrics logic inside of heroku/ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Aug 28, 2023
1 parent 802b9a6 commit 61adc80
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 1,018 deletions.
778 changes: 19 additions & 759 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
[workspace]
resolver = "2"
members = [
"buildpacks/metrics-agent",
"buildpacks/ruby",
"commons"
]
members = ["buildpacks/ruby", "commons"]

[workspace.package]
rust-version = "1.64"
Expand Down
4 changes: 0 additions & 4 deletions buildpacks/metrics-agent/CHANGELOG.md

This file was deleted.

29 changes: 0 additions & 29 deletions buildpacks/metrics-agent/Cargo.toml

This file was deleted.

23 changes: 0 additions & 23 deletions buildpacks/metrics-agent/buildpack.toml

This file was deleted.

1 change: 0 additions & 1 deletion buildpacks/metrics-agent/src/layers/mod.rs

This file was deleted.

101 changes: 0 additions & 101 deletions buildpacks/metrics-agent/src/main.rs

This file was deleted.

33 changes: 0 additions & 33 deletions buildpacks/metrics-agent/tests/integration_test.rs

This file was deleted.

1 change: 1 addition & 0 deletions buildpacks/ruby/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tempfile = "3"
thiserror = "1"
ureq = "2"
url = "2"
clap = { version = "4.4.1", features = ["derive"] }

[dev-dependencies]
libcnb-test = "=0.14.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const SLEEP_FOR: Duration = Duration::from_secs(1);
///
/// Example:
///
/// ```shell
/// $ cargo run --bin agentmon_loop -- --path <path/to/agentmon/binary>
/// ```

/// Turn CLI arguments into a Rust struct
#[derive(Parser, Debug)]
Expand All @@ -37,7 +39,7 @@ fn main() {
exit(1);
}

let agentmon_args = match build_args(std::env::vars().collect::<HashMap<String, String>>()) {
let agentmon_args = match build_args(&std::env::vars().collect::<HashMap<String, String>>()) {
Ok(args) => args,
Err(e) => {
eprintln!("Cannot start agentmon: {e}");
Expand Down Expand Up @@ -90,22 +92,21 @@ enum Error {
///
/// # Errors
///
/// - PORT is not set
/// - HEROKU_METRICS_URL is not set
/// - DYNO starts with `run.`
fn build_args(env: HashMap<String, String>) -> Result<Vec<String>, Error> {
/// - Environment variables: PORT or `HEROKU_METRICS_URL` are not set
/// - Environment variable DYNO starts with `run.`
fn build_args(env: &HashMap<String, String>) -> Result<Vec<String>, Error> {
let mut args = Vec::new();
if let Some(true) = env.get("DYNO").map(|value| value.starts_with("run.")) {
if env.get("DYNO").is_some_and(|value| value.starts_with("run.")) {
return Err(Error::RunDynoDetected);
}

if let Some(port) = env.get("PORT") {
args.push(format!("statsd-addr=:{port}"));
args.push(format!("-statsd-addr=:{port}"));
} else {
return Err(Error::MissingPort);
};

if let Some(true) = env.get("AGENTMON_DEBUG").map(|value| value == "true") {
if env.get("AGENTMON_DEBUG").is_some_and(|value| value == "true") {
args.push("-debug".to_string());
};

Expand All @@ -124,7 +125,7 @@ mod test {

#[test]
fn missing_port() {
let result = build_args(HashMap::new());
let result = build_args(&HashMap::new());

assert_eq!(result, Err(Error::MissingPort));
}
Expand All @@ -138,12 +139,12 @@ mod test {
"https://example.com".to_string(),
);

let result = build_args(env);
let result = build_args(&env);

assert_eq!(
result,
Ok(vec![
"statsd-addr=:90210".to_string(),
"-statsd-addr=:90210".to_string(),
"https://example.com".to_string()
])
);
Expand All @@ -159,12 +160,12 @@ mod test {
);
env.insert("AGENTMON_DEBUG".to_string(), "true".to_string());

let result = build_args(env);
let result = build_args(&env);

assert_eq!(
result,
Ok(vec![
"statsd-addr=:90210".to_string(),
"-statsd-addr=:90210".to_string(),
"-debug".to_string(),
"https://example.com".to_string()
])
Expand Down
77 changes: 77 additions & 0 deletions buildpacks/ruby/src/bin/launch_daemon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use clap::Parser;
use std::path::PathBuf;
use std::process::exit;
use std::process::Command;

/// Schedules agentmon to run as a background daemon

/// CLI argument parser
///
/// ```shell
/// $ cargo run --bin launch_daemon \
/// --log <path/to/log.txt> \
/// --agentmon <path/to/agentmon>
/// --loop-path <path/to/agentmon_loop>
/// ```
#[derive(Parser, Debug)]
struct Args {
#[arg(long, value_parser = absolute_path_exists)]
log: PathBuf,

#[arg(long, value_parser = absolute_path_exists)]
agentmon: PathBuf,

#[arg(long, value_parser = absolute_path_exists)]
loop_path: PathBuf,
}

fn absolute_path_exists(s: &str) -> Result<PathBuf, String> {
fs_err::canonicalize(PathBuf::from(s))
.map_err(|e| format!("{s} is not a valid path. Details: {e}"))
.and_then(|path| match path.try_exists() {
Ok(true) => Ok(path),
Ok(false) => Err(format!("path {} does not exist", path.display())),
Err(e) => Err(format!("problem verifying {} exists {e}", path.display())),
})
}

fn main() {
let Args {
log,
loop_path,
agentmon,
} = Args::parse();

let mut command = Command::new("start-stop-daemon");
if std::env::var_os("AGENTMON_DEBUG").is_some() {
command.args(["--output", &log.to_string_lossy()]);
} else {
match fs_err::write(&log, "To enable logging run with AGENTMON_DEBUG=1") {
Ok(_) => {}
Err(error) => eprintln!(
"Could not write to log file {}. Reason: {error}",
log.display()
),
};
}
command.args([
"--start",
"--background",
"--exec",
&loop_path.to_string_lossy(),
"--",
"--path",
&agentmon.to_string_lossy(),
]);

match command.spawn().map(|mut child| child.wait()) {
Ok(_) => {}
Err(error) => {
eprintln!(
"Command failed {}. Details: {error}",
commons::fun_run::display(&mut command)
);
exit(1)
}
};
}
1 change: 1 addition & 0 deletions buildpacks/ruby/src/layers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod bundle_download_layer;
mod bundle_install_layer;
pub(crate) mod metrics_agent_install;
mod ruby_install_layer;

pub(crate) use self::bundle_download_layer::BundleDownloadLayer;
Expand Down
Loading

0 comments on commit 61adc80

Please sign in to comment.