From 86a989517eda501c75a02bbea7f7dae08423dec2 Mon Sep 17 00:00:00 2001 From: LFC <990479+MichaelScofield@users.noreply.github.com> Date: Tue, 23 Apr 2024 22:21:34 +0800 Subject: [PATCH] refactor: move the version string to common (#3783) --- src/cmd/Cargo.toml | 1 + src/cmd/src/bin/greptime.rs | 6 +++-- src/cmd/src/lib.rs | 46 ++++++----------------------------- src/common/version/src/lib.rs | 25 +++++++++++++++++++ 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/cmd/Cargo.toml b/src/cmd/Cargo.toml index 8b2db4a40a70..24bbe69df18c 100644 --- a/src/cmd/Cargo.toml +++ b/src/cmd/Cargo.toml @@ -36,6 +36,7 @@ common-telemetry = { workspace = true, features = [ "deadlock_detection", ] } common-time.workspace = true +common-version.workspace = true common-wal.workspace = true config = "0.13" datanode.workspace = true diff --git a/src/cmd/src/bin/greptime.rs b/src/cmd/src/bin/greptime.rs index 070a79868abb..0bbfb6bc0407 100644 --- a/src/cmd/src/bin/greptime.rs +++ b/src/cmd/src/bin/greptime.rs @@ -22,6 +22,7 @@ use cmd::options::{CliOptions, Options}; use cmd::{ cli, datanode, frontend, greptimedb_cli, log_versions, metasrv, standalone, start_app, App, }; +use common_version::{short_version, version}; #[derive(Parser)] enum SubCommand { @@ -105,7 +106,8 @@ async fn main() -> Result<()> { common_telemetry::set_panic_hook(); - let cli = greptimedb_cli(); + let version = version!(); + let cli = greptimedb_cli().version(version); let cli = SubCommand::augment_subcommands(cli); @@ -129,7 +131,7 @@ async fn main() -> Result<()> { opts.node_id(), ); - log_versions(); + log_versions(version, short_version!()); let app = subcmd.build(opts).await?; diff --git a/src/cmd/src/lib.rs b/src/cmd/src/lib.rs index 17f02cbc148d..9b4ba43bd330 100644 --- a/src/cmd/src/lib.rs +++ b/src/cmd/src/lib.rs @@ -64,26 +64,23 @@ pub async fn start_app(mut app: Box) -> error::Result<()> { Ok(()) } -pub fn log_versions() { +/// Log the versions of the application, and the arguments passed to the cli. +/// `version_string` should be the same as the output of cli "--version"; +/// and the `app_version` is the short version of the codes, often consist of git branch and commit. +pub fn log_versions(version_string: &str, app_version: &str) { // Report app version as gauge. APP_VERSION - .with_label_values(&[short_version(), full_version()]) + .with_label_values(&[env!("CARGO_PKG_VERSION"), app_version]) .inc(); // Log version and argument flags. - info!( - "short_version: {}, full_version: {}", - short_version(), - full_version() - ); + info!("GreptimeDB version: {}", version_string); log_env_flags(); } pub fn greptimedb_cli() -> clap::Command { - let cmd = clap::Command::new("greptimedb") - .version(print_version()) - .subcommand_required(true); + let cmd = clap::Command::new("greptimedb").subcommand_required(true); #[cfg(feature = "tokio-console")] let cmd = cmd.arg(arg!(--"tokio-console-addr"[TOKIO_CONSOLE_ADDR])); @@ -91,35 +88,6 @@ pub fn greptimedb_cli() -> clap::Command { cmd.args([arg!(--"log-dir"[LOG_DIR]), arg!(--"log-level"[LOG_LEVEL])]) } -fn print_version() -> &'static str { - concat!( - "\nbranch: ", - env!("GIT_BRANCH"), - "\ncommit: ", - env!("GIT_COMMIT"), - "\ndirty: ", - env!("GIT_DIRTY"), - "\nversion: ", - env!("CARGO_PKG_VERSION") - ) -} - -fn short_version() -> &'static str { - env!("CARGO_PKG_VERSION") -} - -// {app_name}-{branch_name}-{commit_short} -// The branch name (tag) of a release build should already contain the short -// version so the full version doesn't concat the short version explicitly. -fn full_version() -> &'static str { - concat!( - "greptimedb-", - env!("GIT_BRANCH"), - "-", - env!("GIT_COMMIT_SHORT") - ) -} - fn log_env_flags() { info!("command line arguments"); for argument in std::env::args() { diff --git a/src/common/version/src/lib.rs b/src/common/version/src/lib.rs index 42bbeeb1e353..fd179769cd2b 100644 --- a/src/common/version/src/lib.rs +++ b/src/common/version/src/lib.rs @@ -103,3 +103,28 @@ pub fn setup_build_info() { println!("cargo:rustc-env=RUSTC_VERSION={}", build_info.rustc); println!("cargo:rustc-env=SOURCE_TIMESTAMP={}", build_info.timestamp); } + +/// Get the string for the output of cli "--version". +#[macro_export] +macro_rules! version { + () => { + concat!( + "\nbranch: ", + env!("GIT_BRANCH"), + "\ncommit: ", + env!("GIT_COMMIT"), + "\ndirty: ", + env!("GIT_DIRTY"), + "\nversion: ", + env!("CARGO_PKG_VERSION") + ) + }; +} + +/// Short version for reporting metrics. +#[macro_export] +macro_rules! short_version { + () => { + concat!(env!("GIT_BRANCH"), "-", env!("GIT_COMMIT_SHORT")) + }; +}