Skip to content

Commit

Permalink
[aptos-telemetry] Lite build information collection (aptos-labs#3309)
Browse files Browse the repository at this point in the history
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
ibalajiarun authored Aug 23, 2022
1 parent af05a87 commit 1ef2cbc
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 169 deletions.
10 changes: 10 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ members = [
"consensus/safety-rules",
"crates/aptos",
"crates/aptos-bitvec",
"crates/aptos-build-info",
"crates/aptos-compression",
"crates/aptos-crypto",
"crates/aptos-crypto-derive",
Expand Down
1 change: 1 addition & 0 deletions aptos-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tokio = { version = "1.18.2", features = ["full"] }
tokio-stream = "0.1.8"

aptos-api = { path = "../api" }
aptos-build-info = { path = "../crates/aptos-build-info" }
aptos-config = { path = "../config" }
aptos-crypto = { path = "../crates/aptos-crypto" }
aptos-data-client = { path = "../state-sync/aptos-data-client" }
Expand Down
9 changes: 7 additions & 2 deletions aptos-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use anyhow::anyhow;
use aptos_api::bootstrap as bootstrap_api;
use aptos_build_info::build_information;
use aptos_config::{
config::{
AptosDataClientConfig, BaseConfig, DataStreamingServiceConfig, NetworkConfig, NodeConfig,
Expand Down Expand Up @@ -725,9 +726,13 @@ pub fn setup_environment(node_config: NodeConfig) -> anyhow::Result<AptosHandle>
debug!("Consensus started in {} ms", instant.elapsed().as_millis());
}

let build_info = build_information!();
// Create the telemetry service
let telemetry_runtime =
aptos_telemetry::service::start_telemetry_service(node_config.clone(), chain_id);
let telemetry_runtime = aptos_telemetry::service::start_telemetry_service(
node_config.clone(),
chain_id,
build_info,
);

Ok(AptosHandle {
_api: api_runtime,
Expand Down
18 changes: 18 additions & 0 deletions crates/aptos-build-info/Cargo.toml
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"
6 changes: 6 additions & 0 deletions crates/aptos-build-info/build.rs
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()
}
104 changes: 104 additions & 0 deletions crates/aptos-build-info/src/lib.rs
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));
}
}
110 changes: 0 additions & 110 deletions crates/aptos-telemetry/src/build_information.rs

This file was deleted.

3 changes: 1 addition & 2 deletions crates/aptos-telemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

#![forbid(unsafe_code)]

pub mod build_information;
pub mod cli_metrics;
mod constants;
mod core_metrics;
mod metrics;
mod network_metrics;
mod sender;
pub mod service;
mod system_information;
pub mod system_information;
pub mod utils;
Loading

0 comments on commit 1ef2cbc

Please sign in to comment.