From 19d654eb7d5491b1f4deb5ea2f4bcfdbfc91acb1 Mon Sep 17 00:00:00 2001 From: Hengfei Yang Date: Mon, 10 Mar 2025 16:46:38 +0800 Subject: [PATCH] feat: add node version (#6224) Add node version to node list via heartbeat. --- Cargo.toml | 3 --- src/cli/basic/cli.rs | 2 +- src/cli/basic/http.rs | 8 ++++++-- src/common/infra/cluster/etcd.rs | 2 ++ src/common/infra/cluster/nats.rs | 2 ++ src/common/infra/cluster/scheduler.rs | 1 + src/common/infra/config.rs | 5 ----- src/common/meta/telemetry.rs | 2 +- src/common/migration/schema.rs | 7 ++----- src/config/Cargo.toml | 3 +++ build.rs => src/config/build.rs | 0 src/config/src/cluster.rs | 1 + src/config/src/config.rs | 5 +++++ src/config/src/meta/cluster.rs | 3 +++ src/handler/http/request/status/mod.rs | 6 +++--- src/main.rs | 10 +++++----- src/service/db/version.rs | 2 +- 17 files changed, 36 insertions(+), 26 deletions(-) rename build.rs => src/config/build.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 0954996c03..cbfdc33ccf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -180,9 +180,6 @@ pprof = { version = "0.14", features = [ "prost-codec", ], optional = true } -[build-dependencies] -chrono = { version = "0.4", default-features = false, features = ["clock"] } - [dev-dependencies] async-walkdir.workspace = true expect-test.workspace = true diff --git a/src/cli/basic/cli.rs b/src/cli/basic/cli.rs index 8889d6fd5e..d0b8679630 100644 --- a/src/cli/basic/cli.rs +++ b/src/cli/basic/cli.rs @@ -28,7 +28,7 @@ use crate::{ pub async fn cli() -> Result { let app = clap::Command::new("openobserve") - .version(env!("GIT_VERSION")) + .version(config::VERSION) .about(clap::crate_description!()) .subcommands(&[ clap::Command::new("reset") diff --git a/src/cli/basic/http.rs b/src/cli/basic/http.rs index 9fde292808..8d59bce0e9 100644 --- a/src/cli/basic/http.rs +++ b/src/cli/basic/http.rs @@ -126,7 +126,8 @@ pub async fn node_list() -> Result<(), anyhow::Error> { Cell::new("ID"), Cell::new("UUID"), Cell::new("Name"), - Cell::new("HTTP Address"), + Cell::new("Endpoint"), + Cell::new("Version"), Cell::new("Status"), Cell::new("Scheduled"), Cell::new("CPU"), @@ -139,6 +140,7 @@ pub async fn node_list() -> Result<(), anyhow::Error> { Cell::new(&node.uuid), Cell::new(&node.name), Cell::new(&node.http_addr), + Cell::new(&node.version), Cell::new(&format!("{:?}", node.status)), Cell::new(&node.scheduled.to_string()), Cell::new(&node.metrics.cpu_total.to_string()), @@ -167,7 +169,8 @@ pub async fn node_list_with_metrics() -> Result<(), anyhow::Error> { Cell::new("ID"), Cell::new("UUID"), Cell::new("Name"), - Cell::new("HTTP Address"), + Cell::new("Endpoint"), + Cell::new("Version"), Cell::new("Status"), Cell::new("Scheduled"), Cell::new("CPU"), @@ -186,6 +189,7 @@ pub async fn node_list_with_metrics() -> Result<(), anyhow::Error> { Cell::new(&node.uuid), Cell::new(&node.name), Cell::new(&node.http_addr), + Cell::new(&node.version), Cell::new(&format!("{:?}", node.status)), Cell::new(&node.scheduled.to_string()), Cell::new(&node.metrics.cpu_total.to_string()), diff --git a/src/common/infra/cluster/etcd.rs b/src/common/infra/cluster/etcd.rs index 3861121466..92317839bd 100644 --- a/src/common/infra/cluster/etcd.rs +++ b/src/common/infra/cluster/etcd.rs @@ -153,6 +153,7 @@ async fn register() -> Result<()> { scheduled: true, broadcasted: false, metrics: Default::default(), + version: config::VERSION.to_string(), }; let val = json::to_string(&node).unwrap(); @@ -244,6 +245,7 @@ pub(crate) async fn set_status(status: NodeStatus, new_lease_id: bool) -> Result scheduled: true, broadcasted: false, metrics: Default::default(), + version: config::VERSION.to_string(), }, }; diff --git a/src/common/infra/cluster/nats.rs b/src/common/infra/cluster/nats.rs index 4c6ccf3be2..e5fe81290b 100644 --- a/src/common/infra/cluster/nats.rs +++ b/src/common/infra/cluster/nats.rs @@ -154,6 +154,7 @@ async fn register() -> Result<()> { scheduled: true, broadcasted: false, metrics: Default::default(), + version: config::VERSION.to_string(), }; let val = json::to_vec(&node).unwrap(); @@ -232,6 +233,7 @@ pub(crate) async fn set_status(status: NodeStatus) -> Result<()> { scheduled: true, broadcasted: false, metrics: Default::default(), + version: config::VERSION.to_string(), }, }; diff --git a/src/common/infra/cluster/scheduler.rs b/src/common/infra/cluster/scheduler.rs index 2815a01c07..f6e23e6a79 100644 --- a/src/common/infra/cluster/scheduler.rs +++ b/src/common/infra/cluster/scheduler.rs @@ -107,6 +107,7 @@ mod tests { tcp_conns_close_wait: 3, tcp_conns_time_wait: 5, }, + version: config::VERSION.to_string(), } } diff --git a/src/common/infra/config.rs b/src/common/infra/config.rs index 5b011924c5..bd8455b034 100644 --- a/src/common/infra/config.rs +++ b/src/common/infra/config.rs @@ -44,11 +44,6 @@ use crate::{ }, }; -// global version variables -pub static VERSION: &str = env!("GIT_VERSION"); -pub static COMMIT_HASH: &str = env!("GIT_COMMIT_HASH"); -pub static BUILD_DATE: &str = env!("GIT_BUILD_DATE"); - // global cache variables pub static KVS: Lazy> = Lazy::new(Default::default); pub static QUERY_FUNCTIONS: Lazy> = Lazy::new(DashMap::default); diff --git a/src/common/meta/telemetry.rs b/src/common/meta/telemetry.rs index a03711f745..d82c940e02 100644 --- a/src/common/meta/telemetry.rs +++ b/src/common/meta/telemetry.rs @@ -112,7 +112,7 @@ pub fn get_base_info(data: &mut HashMap) -> HashMap Result<(), anyhow::Error> { if let Err(e) = db .put( META_MIGRATION_VERSION_KEY, - VERSION.to_string().into(), + config::VERSION.to_string().into(), NO_NEED_WATCH, None, ) diff --git a/src/config/Cargo.toml b/src/config/Cargo.toml index d918e4df0b..6500e61849 100644 --- a/src/config/Cargo.toml +++ b/src/config/Cargo.toml @@ -76,3 +76,6 @@ vrl.workspace = true [dev-dependencies] expect-test.workspace = true + +[build-dependencies] +chrono.workspace = true diff --git a/build.rs b/src/config/build.rs similarity index 100% rename from build.rs rename to src/config/build.rs diff --git a/src/config/src/cluster.rs b/src/config/src/cluster.rs index 48bbe8cfc2..e85a02e96a 100644 --- a/src/config/src/cluster.rs +++ b/src/config/src/cluster.rs @@ -55,6 +55,7 @@ pub fn load_local_node() -> Node { broadcasted: false, status: NodeStatus::Online, metrics: Default::default(), + version: crate::VERSION.to_string(), } } diff --git a/src/config/src/config.rs b/src/config/src/config.rs index 5da0b6ebdb..d4b21b38cc 100644 --- a/src/config/src/config.rs +++ b/src/config/src/config.rs @@ -45,6 +45,11 @@ pub type RwAHashMap = tokio::sync::RwLock>; pub type RwAHashSet = tokio::sync::RwLock>; pub type RwBTreeMap = tokio::sync::RwLock>; +// global version variables +pub static VERSION: &str = env!("GIT_VERSION"); +pub static COMMIT_HASH: &str = env!("GIT_COMMIT_HASH"); +pub static BUILD_DATE: &str = env!("GIT_BUILD_DATE"); + pub const META_ORG_ID: &str = "_meta"; pub const MMDB_CITY_FILE_NAME: &str = "GeoLite2-City.mmdb"; diff --git a/src/config/src/meta/cluster.rs b/src/config/src/meta/cluster.rs index ff2c8ee1a6..7a6d2aade4 100644 --- a/src/config/src/meta/cluster.rs +++ b/src/config/src/meta/cluster.rs @@ -50,6 +50,8 @@ pub struct Node { pub status: NodeStatus, #[serde(default)] pub metrics: NodeMetrics, + #[serde(default)] + pub version: String, } impl Node { @@ -67,6 +69,7 @@ impl Node { broadcasted: false, status: NodeStatus::Prepare, metrics: Default::default(), + version: crate::VERSION.to_string(), } } diff --git a/src/handler/http/request/status/mod.rs b/src/handler/http/request/status/mod.rs index 2b9d9a2b32..1bb23c27b1 100644 --- a/src/handler/http/request/status/mod.rs +++ b/src/handler/http/request/status/mod.rs @@ -256,10 +256,10 @@ pub async fn zo_config() -> Result { let build_type = "opensource"; let cfg = get_config(); Ok(HttpResponse::Ok().json(ConfigResponse { - version: VERSION.to_string(), + version: config::VERSION.to_string(), instance: get_instance_id(), - commit_hash: COMMIT_HASH.to_string(), - build_date: BUILD_DATE.to_string(), + commit_hash: config::COMMIT_HASH.to_string(), + build_date: config::BUILD_DATE.to_string(), build_type: build_type.to_string(), telemetry_enabled: cfg.common.telemetry_enabled, default_fts_keys: SQL_FULL_TEXT_SEARCH_FIELDS diff --git a/src/main.rs b/src/main.rs index ab8e6799e0..db1a6dc105 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,7 @@ use openobserve::handler::http::{ use openobserve::{ cli::basic::cli, common::{ - infra::{self as common_infra, cluster, config::VERSION}, + infra::{self as common_infra, cluster}, meta, migration, utils::zo_logger, }, @@ -147,7 +147,7 @@ async fn main() -> Result<(), anyhow::Error> { [ ("role", cfg.common.node_role.as_str()), ("instance", cfg.common.instance_name.as_str()), - ("version", VERSION), + ("version", config::VERSION), ] .to_vec(), ) @@ -187,7 +187,7 @@ async fn main() -> Result<(), anyhow::Error> { Some(setup_logs()) }; - log::info!("Starting OpenObserve {}", VERSION); + log::info!("Starting OpenObserve {}", config::VERSION); log::info!( "System info: CPU cores {}, MEM total {}, Disk total {}, free {}", cfg.limit.real_cpu_num, @@ -257,7 +257,7 @@ async fn main() -> Result<(), anyhow::Error> { // check version upgrade let old_version = db::version::get().await.unwrap_or("v0.0.0".to_string()); - if let Err(e) = migration::check_upgrade(&old_version, VERSION).await { + if let Err(e) = migration::check_upgrade(&old_version, config::VERSION).await { job_init_tx.send(false).ok(); panic!("check upgrade failed: {}", e); } @@ -982,7 +982,7 @@ fn enable_tracing() -> Result<(), anyhow::Error> { Resource::new(vec![ KeyValue::new("service.name", cfg.common.node_role.to_string()), KeyValue::new("service.instance", cfg.common.instance_name.to_string()), - KeyValue::new("service.version", VERSION), + KeyValue::new("service.version", config::VERSION), ]), )) .install_batch(opentelemetry_sdk::runtime::Tokio)?; diff --git a/src/service/db/version.rs b/src/service/db/version.rs index c792fc53c6..7c0ebf5c06 100644 --- a/src/service/db/version.rs +++ b/src/service/db/version.rs @@ -13,7 +13,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -use crate::{common::infra::config, service::db}; +use crate::service::db; pub async fn get() -> Result { let ret = db::get("/meta/kv/version").await?;