forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[aptos-telemetry] Lite build information collection (aptos-labs#3309)
This commit introduces aptos-build-info, a library crate that collect certain build information and exposes them for consumption. The library is used in aptos-node and aptos crates to feed the aptos-telemetry crate and provide CLI version information. aptos-build-info uses shadow_rs so it is only introduced in the binary crates so as to not introduce overhead during build times. It also gets PKG_VERSION of calling crate via macros.
- Loading branch information
1 parent
af05a87
commit 1ef2cbc
Showing
16 changed files
with
203 additions
and
169 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "aptos-build-info" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Aptos Labs <[email protected]>"] | ||
description = "Aptos build information provider" | ||
repository = "https://github.com/aptos-labs/aptos-core" | ||
homepage = "https://aptoslabs.com" | ||
license = "Apache-2.0" | ||
publish = false | ||
build = "build.rs" | ||
|
||
[dependencies] | ||
shadow-rs = "0.16.2" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[build-dependencies] | ||
shadow-rs = "0.16.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
fn main() -> shadow_rs::SdResult<()> { | ||
shadow_rs::new() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::collections::BTreeMap; | ||
|
||
use shadow_rs::shadow; | ||
|
||
/// Build information keys | ||
pub const BUILD_BRANCH: &str = "build_branch"; | ||
pub const BUILD_CARGO_VERSION: &str = "build_cargo_version"; | ||
pub const BUILD_COMMIT_HASH: &str = "build_commit_hash"; | ||
pub const BUILD_TAG: &str = "build_tag"; | ||
pub const BUILD_TIME: &str = "build_time"; | ||
pub const BUILD_OS: &str = "build_os"; | ||
pub const BUILD_PKG_VERSION: &str = "build_pkg_version"; | ||
pub const BUILD_RUST_CHANNEL: &str = "build_rust_channel"; | ||
pub const BUILD_RUST_VERSION: &str = "build_rust_version"; | ||
|
||
/// This macro returns the build information as visible during build-time. | ||
/// Use of this macro is recommended over the `get_build_information` | ||
/// function because this macro includes the caller crate package version | ||
/// in the returned build information map. | ||
#[macro_export] | ||
macro_rules! build_information { | ||
() => {{ | ||
let mut build_information = aptos_build_info::get_build_information(); | ||
|
||
build_information.insert( | ||
aptos_build_info::BUILD_PKG_VERSION.into(), | ||
env!("CARGO_PKG_VERSION").into(), | ||
); | ||
|
||
build_information | ||
}}; | ||
} | ||
|
||
/// This method returns the build information as visible during build-time. | ||
/// Note that it is recommended to use the the `build_information` macro since | ||
/// this method does not return the build package version. | ||
pub fn get_build_information() -> BTreeMap<String, String> { | ||
shadow!(build); | ||
|
||
let mut build_information = BTreeMap::new(); | ||
|
||
// Get Git metadata from shadow_rs crate. | ||
// This is applicable for native builds where the cargo has | ||
// access to the .git directory. | ||
build_information.insert(BUILD_BRANCH.into(), build::BRANCH.into()); | ||
build_information.insert(BUILD_CARGO_VERSION.into(), build::CARGO_VERSION.into()); | ||
build_information.insert(BUILD_COMMIT_HASH.into(), build::COMMIT_HASH.into()); | ||
build_information.insert(BUILD_TAG.into(), build::TAG.into()); | ||
build_information.insert(BUILD_TIME.into(), build::BUILD_TIME.into()); | ||
build_information.insert(BUILD_OS.into(), build::BUILD_OS.into()); | ||
build_information.insert(BUILD_RUST_CHANNEL.into(), build::RUST_CHANNEL.into()); | ||
build_information.insert(BUILD_RUST_VERSION.into(), build::RUST_VERSION.into()); | ||
|
||
// Get Git metadata from environment variables set during build-time. | ||
// This is applicable for docker based builds where the cargo cannot | ||
// access the .git directory, or to override shadow_rs provided info. | ||
if let Ok(git_sha) = std::env::var("GIT_SHA") { | ||
build_information.insert(BUILD_COMMIT_HASH.into(), git_sha); | ||
} | ||
|
||
if let Ok(git_branch) = std::env::var("GIT_BRANCH") { | ||
build_information.insert(BUILD_BRANCH.into(), git_branch); | ||
} | ||
|
||
if let Ok(git_tag) = std::env::var("GIT_TAG") { | ||
build_information.insert(BUILD_TAG.into(), git_tag); | ||
} | ||
|
||
if let Ok(build_date) = std::env::var("BUILD_DATE") { | ||
build_information.insert(BUILD_TIME.into(), build_date); | ||
} | ||
|
||
build_information | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::*; | ||
|
||
#[test] | ||
fn test_get_build_information_env_override() { | ||
let commit_hash = String::from("COMMIT-HASH-1"); | ||
let build_branch = String::from("branch-1"); | ||
let build_tag = String::from("release-1"); | ||
|
||
std::env::set_var("GIT_SHA", commit_hash.clone()); | ||
std::env::set_var("GIT_BRANCH", build_branch.clone()); | ||
std::env::set_var("GIT_TAG", build_tag.clone()); | ||
|
||
let info = get_build_information(); | ||
|
||
assert!(info.contains_key(BUILD_COMMIT_HASH)); | ||
assert_eq!(info.get(BUILD_COMMIT_HASH), Some(&commit_hash)); | ||
|
||
assert!(info.contains_key(BUILD_BRANCH)); | ||
assert_eq!(info.get(BUILD_BRANCH), Some(&build_branch)); | ||
|
||
assert!(info.contains_key(BUILD_TAG)); | ||
assert_eq!(info.get(BUILD_TAG), Some(&build_tag)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.