Skip to content

Commit

Permalink
Use the tracing features provided by libcnb
Browse files Browse the repository at this point in the history
Removes the otel tracing added in [#652](#652) in favor of the `libcnb` tracing support added in [heroku/libcnb.rs#723](heroku/libcnb.rs#723).
  • Loading branch information
colincasey committed Nov 5, 2024
1 parent ddbd28b commit 7f4507e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 187 deletions.
45 changes: 3 additions & 42 deletions Cargo.lock

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

117 changes: 58 additions & 59 deletions buildpacks/nodejs-corepack/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use heroku_nodejs_utils::package_json::{PackageJson, PackageJsonError};
use heroku_nodejs_utils::telemetry::init_tracer;
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
use libcnb::data::build_plan::BuildPlanBuilder;
use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder};
Expand Down Expand Up @@ -35,67 +34,67 @@ impl Buildpack for CorepackBuildpack {
type Error = CorepackBuildpackError;

fn detect(&self, context: DetectContext<Self>) -> libcnb::Result<DetectResult, Self::Error> {
let tracer = init_tracer(context.buildpack_descriptor.buildpack.id.to_string());
tracer.in_span("nodejs-corepack-detect", |_cx| {
// Corepack requires the `packageManager` key from `package.json`.
// This buildpack won't be detected without it.
let pkg_json_path = context.app_dir.join("package.json");
if pkg_json_path.exists() {
let pkg_json = PackageJson::read(pkg_json_path)
.map_err(CorepackBuildpackError::PackageJson)?;
cfg::get_supported_package_manager(&pkg_json).map_or_else(
|| DetectResultBuilder::fail().build(),
|pkg_mgr| {
DetectResultBuilder::pass()
.build_plan(
BuildPlanBuilder::new()
.requires("node")
.requires(&pkg_mgr)
.provides(pkg_mgr)
.build(),
)
.build()
},
)
} else {
DetectResultBuilder::fail().build()
}
})
opentelemetry::global::tracer(context.buildpack_descriptor.buildpack.id.to_string())
.in_span("detect", |_cx| {
// Corepack requires the `packageManager` key from `package.json`.
// This buildpack won't be detected without it.
let pkg_json_path = context.app_dir.join("package.json");
if pkg_json_path.exists() {
let pkg_json = PackageJson::read(pkg_json_path)
.map_err(CorepackBuildpackError::PackageJson)?;
cfg::get_supported_package_manager(&pkg_json).map_or_else(
|| DetectResultBuilder::fail().build(),
|pkg_mgr| {
DetectResultBuilder::pass()
.build_plan(
BuildPlanBuilder::new()
.requires("node")
.requires(&pkg_mgr)
.provides(pkg_mgr)
.build(),
)
.build()
},
)
} else {
DetectResultBuilder::fail().build()
}
})
}

fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
let tracer = init_tracer(context.buildpack_descriptor.buildpack.id.to_string());
tracer.in_span("nodejs-corepack-build", |cx| {
let pkg_mgr = PackageJson::read(context.app_dir.join("package.json"))
.map_err(CorepackBuildpackError::PackageJson)?
.package_manager
.ok_or(CorepackBuildpackError::PackageManagerMissing)?;

cx.span().set_attributes([
KeyValue::new("package_manager.name", pkg_mgr.name.clone()),
KeyValue::new("package_manager.version", pkg_mgr.version.to_string()),
]);

let env = &Env::from_current();

let corepack_version =
cmd::corepack_version(env).map_err(CorepackBuildpackError::CorepackVersion)?;

cx.span().set_attribute(KeyValue::new(
"corepack.version",
corepack_version.to_string(),
));

log_header(format!(
"Installing {} {} via corepack {corepack_version}",
pkg_mgr.name, pkg_mgr.version
));

enable_corepack(&context, &corepack_version, &pkg_mgr, env)?;
prepare_corepack(&context, &pkg_mgr, env)?;

BuildResultBuilder::new().build()
})
opentelemetry::global::tracer(context.buildpack_descriptor.buildpack.id.to_string())
.in_span("build", |cx| {
let pkg_mgr = PackageJson::read(context.app_dir.join("package.json"))
.map_err(CorepackBuildpackError::PackageJson)?
.package_manager
.ok_or(CorepackBuildpackError::PackageManagerMissing)?;

cx.span().set_attributes([
KeyValue::new("package_manager.name", pkg_mgr.name.clone()),
KeyValue::new("package_manager.version", pkg_mgr.version.to_string()),
]);

let env = &Env::from_current();

let corepack_version =
cmd::corepack_version(env).map_err(CorepackBuildpackError::CorepackVersion)?;

cx.span().set_attribute(KeyValue::new(
"corepack.version",
corepack_version.to_string(),
));

log_header(format!(
"Installing {} {} via corepack {corepack_version}",
pkg_mgr.name, pkg_mgr.version
));

enable_corepack(&context, &corepack_version, &pkg_mgr, env)?;
prepare_corepack(&context, &pkg_mgr, env)?;

BuildResultBuilder::new().build()
})
}

fn on_error(&self, err: libcnb::Error<Self::Error>) {
Expand Down
3 changes: 0 additions & 3 deletions common/nodejs-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ keep_a_changelog_file = "0.1.0"
libcnb-data = "=0.23.0"
libherokubuildpack = { version = "=0.24.0", default-features = false, features = ["inventory", "inventory-sha2"] }
node-semver = "2"
opentelemetry = "0.24"
opentelemetry_sdk = { version = "0.24", features = ["trace"] }
opentelemetry-stdout = { version = "0.5", features = ["trace"] }
regex = "1"
serde = { version = "1", features = ['derive'] }
serde_json = "1"
Expand Down
1 change: 0 additions & 1 deletion common/nodejs-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ mod npmjs_org;
pub mod package_json;
pub mod package_manager;
mod s3;
pub mod telemetry;
pub mod vrs;
82 changes: 0 additions & 82 deletions common/nodejs-utils/src/telemetry.rs

This file was deleted.

0 comments on commit 7f4507e

Please sign in to comment.