From b7a6df54d5b2c8904a6bd0b4282ace423394217b Mon Sep 17 00:00:00 2001 From: Adam Mizerski Date: Tue, 19 Jan 2021 14:39:48 +0100 Subject: [PATCH] report yagna version as metrics closes #838 --- Cargo.lock | 39 +++++++++++++++++++++++++++-- Cargo.toml | 3 ++- core/serv/src/main.rs | 25 +++++++++++++++--- golem_cli/src/main.rs | 4 +-- utils/compile-time-utils/Cargo.toml | 1 + utils/compile-time-utils/src/lib.rs | 30 ++++++++++++++++++---- 6 files changed, 88 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f82ef3e06..9a56780022 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4164,6 +4164,15 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +[[package]] +name = "pest" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53" +dependencies = [ + "ucd-trie", +] + [[package]] name = "petgraph" version = "0.4.13" @@ -5271,7 +5280,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" dependencies = [ - "semver-parser", + "semver-parser 0.7.0", ] [[package]] @@ -5280,16 +5289,34 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "394cec28fa623e00903caf7ba4fa6fb9a0e260280bb8cdbbba029611108a0190" dependencies = [ - "semver-parser", + "semver-parser 0.7.0", "serde", ] +[[package]] +name = "semver" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" +dependencies = [ + "semver-parser 0.10.2", +] + [[package]] name = "semver-parser" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +[[package]] +name = "semver-parser" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" +dependencies = [ + "pest", +] + [[package]] name = "serde" version = "1.0.120" @@ -6302,6 +6329,12 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" +[[package]] +name = "ucd-trie" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" + [[package]] name = "uint" version = "0.7.1" @@ -6973,6 +7006,7 @@ name = "ya-compile-time-utils" version = "0.1.0" dependencies = [ "git-version", + "semver 0.11.0", "vergen", ] @@ -7760,6 +7794,7 @@ dependencies = [ "gftp", "lazy_static", "log", + "metrics", "openssl", "structopt", "tokio", diff --git a/Cargo.toml b/Cargo.toml index fa25684c5e..e700a44395 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,10 +57,11 @@ dotenv = "0.15.0" futures = "0.3" lazy_static = "1.4" log = "0.4" +metrics = "0.12" openssl = "0.10" structopt = "0.3" -url = "2.1.1" tokio = {version = "0.2.22", features=["uds"]} +url = "2.1.1" [package.metadata.deb] name="golem-requestor" diff --git a/core/serv/src/main.rs b/core/serv/src/main.rs index a282c09e44..652da5e85e 100644 --- a/core/serv/src/main.rs +++ b/core/serv/src/main.rs @@ -1,6 +1,7 @@ use actix_web::{middleware, web, App, HttpServer, Responder}; use anyhow::{Context, Result}; use futures::prelude::*; +use metrics::value; use std::{ any::TypeId, collections::HashMap, @@ -11,15 +12,14 @@ use std::{ }; use structopt::{clap, StructOpt}; use url::Url; + use ya_activity::service::Activity as ActivityService; +use ya_file_logging::start_logger; use ya_identity::service::Identity as IdentityService; use ya_market::MarketService; use ya_metrics::{MetricsPusherOpts, MetricsService}; use ya_net::Net as NetService; use ya_payment::{accounts as payment_accounts, PaymentService}; -use ya_sgx::SgxService; - -use ya_file_logging::start_logger; use ya_persistence::executor::DbExecutor; use ya_sb_proto::{DEFAULT_GSB_URL, GSB_URL_ENV_VAR}; use ya_service_api::{CliCtx, CommandOutput}; @@ -28,6 +28,7 @@ use ya_service_api_web::{ middleware::{auth, Identity}, rest_api_host_port, DEFAULT_YAGNA_API_URL, YAGNA_API_URL_ENV_VAR, }; +use ya_sgx::SgxService; use ya_utils_path::data_dir::DataDir; use ya_utils_process::lock::ProcLock; @@ -315,6 +316,20 @@ async fn sd_notify(_unset_environment: bool, _state: &str) -> std::io::Result<() Ok(()) } +fn report_version_to_metrics() { + let version = ya_compile_time_utils::semver(); + value!("yagna.version.major", version.major); + value!("yagna.version.minor", version.minor); + value!("yagna.version.patch", version.patch); + value!( + "yagna.version.is_prerelease", + (!version.pre.is_empty()) as u64 + ); + if let Some(build_number) = ya_compile_time_utils::build_number() { + value!("yagna.version.build_number", build_number); + } +} + impl ServiceCommand { async fn run_command(&self, ctx: &CliCtx) -> Result { if !ctx.accept_terms { @@ -371,8 +386,10 @@ impl ServiceCommand { let mut context: ServiceContext = ctx.clone().try_into()?; context.set_metrics_ctx(metrics_opts); Services::gsb(&context).await?; - let drivers = start_payment_drivers(&ctx.data_dir).await?; + report_version_to_metrics(); + + let drivers = start_payment_drivers(&ctx.data_dir).await?; payment_accounts::save_default_account(&ctx.data_dir, drivers) .await .unwrap_or_else(|e| { diff --git a/golem_cli/src/main.rs b/golem_cli/src/main.rs index a4c24b9130..5306051ea1 100644 --- a/golem_cli/src/main.rs +++ b/golem_cli/src/main.rs @@ -84,9 +84,9 @@ async fn my_main() -> Result { pub fn banner() { terminal::fade_in(&format!( include_str!("banner.txt"), - version = ya_compile_time_utils::semver(), + version = ya_compile_time_utils::semver_str(), git_commit = ya_compile_time_utils::git_rev(), - build = ya_compile_time_utils::build_number().unwrap_or("-"), + build = ya_compile_time_utils::build_number_str().unwrap_or("-"), date = ya_compile_time_utils::build_date() )) .unwrap(); diff --git a/utils/compile-time-utils/Cargo.toml b/utils/compile-time-utils/Cargo.toml index f9b51018ca..4960c90cce 100644 --- a/utils/compile-time-utils/Cargo.toml +++ b/utils/compile-time-utils/Cargo.toml @@ -7,6 +7,7 @@ description = "Compile-time utilities" [dependencies] git-version = "0.3.4" +semver = "0.11.0" [build-dependencies] vergen = "3.1.0" diff --git a/utils/compile-time-utils/src/lib.rs b/utils/compile-time-utils/src/lib.rs index 0729ea9740..cec63955c4 100644 --- a/utils/compile-time-utils/src/lib.rs +++ b/utils/compile-time-utils/src/lib.rs @@ -1,4 +1,5 @@ use git_version::git_version; +use semver::Version; /// Returns latest tag or version from Cargo.toml`. pub fn git_tag() -> &'static str { @@ -19,12 +20,19 @@ pub fn build_date() -> &'static str { } /// Returns Github Actions build number if available or None. -pub fn build_number() -> Option<&'static str> { +pub fn build_number_str() -> Option<&'static str> { option_env!("GITHUB_RUN_NUMBER") } +pub fn build_number() -> Option { + build_number_str().map(|i| { + // should not panic + i.parse().unwrap() + }) +} + /// convert tag to a semantic version -pub fn semver() -> &'static str { +pub fn semver_str() -> &'static str { let mut version = git_tag(); for prefix in ["pre-rel-", "v"].iter() { if version.starts_with(prefix) { @@ -34,12 +42,17 @@ pub fn semver() -> &'static str { version } +pub fn semver() -> Version { + // It must parse correctly and if it passes test it won't change later. + Version::parse(semver_str()).unwrap() +} + #[macro_export] macro_rules! version_describe { () => { Box::leak( [ - $crate::semver(), + $crate::semver_str(), " (", $crate::git_rev(), " ", @@ -60,7 +73,14 @@ mod test { use super::*; #[test] - fn test() { - println!("{}", semver()) + fn test_semver() { + // should not panic + semver(); + } + + #[test] + fn test_build_number() { + // should not panic + build_number(); } }