Skip to content

Commit

Permalink
Migrate metrics_agent log output to bullet_stream
Browse files Browse the repository at this point in the history
  • Loading branch information
runesoerensen committed Sep 17, 2024
1 parent e0733ed commit b4b1fcf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions buildpacks/ruby/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rust-version.workspace = true
workspace = true

[dependencies]
bullet_stream = "0.2.0"
clap = { version = "4", default-features = false, features = ["derive", "error-context", "help", "std", "usage"] }
commons = { path = "../../commons" }
flate2 = { version = "1", default-features = false, features = ["zlib"] }
Expand Down
28 changes: 14 additions & 14 deletions buildpacks/ruby/src/layers/metrics_agent_install.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{RubyBuildpack, RubyBuildpackError};
use commons::output::section_log::{log_step, log_step_timed, SectionLogger};
use bullet_stream::state::SubBullet;
use bullet_stream::Print;
use flate2::read::GzDecoder;
use libcnb::additional_buildpack_binary_path;
use libcnb::data::layer_name;
Expand All @@ -8,6 +9,7 @@ use libcnb::layer::{
};
use libherokubuildpack::digest::sha256;
use serde::{Deserialize, Serialize};
use std::io::Stdout;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use tar::Archive;
Expand Down Expand Up @@ -59,10 +61,8 @@ pub(crate) enum MetricsAgentInstallError {

pub(crate) fn handle_metrics_agent_layer(
context: &libcnb::build::BuildContext<RubyBuildpack>,
// TODO: Replace when implementing bullet_stream. Included with original comment:
// force the layer to be called within a Section logging context, not necessary but it's safer
_in_section_logger: &dyn SectionLogger,
) -> libcnb::Result<(), RubyBuildpackError> {
mut bullet: Print<SubBullet<Stdout>>,
) -> libcnb::Result<Print<SubBullet<Stdout>>, RubyBuildpackError> {
let layer_ref = context.cached_layer(
layer_name!("metrics_agent"),
CachedLayerDefinition {
Expand All @@ -87,26 +87,26 @@ pub(crate) fn handle_metrics_agent_layer(

match layer_ref.state.clone() {
LayerState::Restored { .. } => {
log_step("Using cached metrics agent");
bullet = bullet.sub_bullet("Using cached metrics agent");
}
LayerState::Empty { cause } => {
match cause {
EmptyLayerCause::NewlyCreated => {}
EmptyLayerCause::InvalidMetadataAction { .. } => {
log_step("Clearing cache (invalid metadata)");
bullet = bullet.sub_bullet("Clearing cache (invalid metadata)");
}
EmptyLayerCause::RestoredLayerAction { cause: url } => {
log_step(format!("Deleting cached metrics agent ({url})"));
bullet = bullet.sub_bullet(format!("Deleting cached metrics agent ({url})"));
}
}
let bin_dir = layer_ref.path().join("bin");

let agentmon = log_step_timed(
format!("Installing metrics agent from {DOWNLOAD_URL}"),
|| install_agentmon(&bin_dir).map_err(RubyBuildpackError::MetricsAgentError),
)?;
let timer = bullet.start_timer(format!("Installing metrics agent from {DOWNLOAD_URL}"));
let agentmon =
install_agentmon(&bin_dir).map_err(RubyBuildpackError::MetricsAgentError)?;
bullet = timer.done();

log_step("Writing scripts");
bullet = bullet.sub_bullet("Writing scripts");
let execd = write_execd_script(&agentmon, layer_ref.path().as_path())
.map_err(RubyBuildpackError::MetricsAgentError)?;

Expand All @@ -116,7 +116,7 @@ pub(crate) fn handle_metrics_agent_layer(
})?;
}
}
Ok(())
Ok(bullet)
}

fn write_execd_script(
Expand Down
20 changes: 9 additions & 11 deletions buildpacks/ruby/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bullet_stream::{style, Print};
use commons::cache::CacheError;
use commons::gemfile_lock::GemfileLock;
use commons::metadata_digest::MetadataDigest;
Expand Down Expand Up @@ -130,22 +131,19 @@ impl Buildpack for RubyBuildpack {
let bundler_version = gemfile_lock.resolve_bundler("2.4.5");
let ruby_version = gemfile_lock.resolve_ruby("3.1.3");

let mut build_output = Print::new(stdout()).without_header();
// ## Install metrics agent
logger = {
let section = logger.section("Metrics agent");
build_output = {
let bullet = build_output.bullet("Metrics agent");
if lockfile_contents.contains("barnes") {
layers::metrics_agent_install::handle_metrics_agent_layer(
&context,
section.as_ref(),
)?;
section.end_section()
layers::metrics_agent_install::handle_metrics_agent_layer(&context, bullet)?.done()
} else {
section
.step(&format!(
bullet
.sub_bullet(&format!(
"Skipping install ({barnes} gem not found)",
barnes = fmt::value("barnes")
barnes = style::value("barnes")
))
.end_section()
.done()
}
};

Expand Down

0 comments on commit b4b1fcf

Please sign in to comment.