Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move the version string to common #3783

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cmd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/cmd/src/bin/greptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand All @@ -129,7 +131,7 @@ async fn main() -> Result<()> {
opts.node_id(),
);

log_versions();
log_versions(version, short_version!());

let app = subcmd.build(opts).await?;

Expand Down
46 changes: 7 additions & 39 deletions src/cmd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,62 +64,30 @@ pub async fn start_app(mut app: Box<dyn App>) -> 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]));

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() {
Expand Down
25 changes: 25 additions & 0 deletions src/common/version/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
};
}
Loading