From 252443a50dc48939eb08b3bcd67273bb71bbe349 Mon Sep 17 00:00:00 2001 From: Sanchith Hegde <22217505+SanchithHegde@users.noreply.github.com> Date: Fri, 5 Jan 2024 17:32:36 +0530 Subject: [PATCH] feat: include version number in response headers and on application startup (#3045) --- crates/drainer/src/main.rs | 3 +++ crates/router/Cargo.toml | 2 +- crates/router/src/bin/router.rs | 3 +++ crates/router/src/bin/scheduler.rs | 8 ++++++-- crates/router/src/middleware.rs | 8 +++++++- crates/router_env/src/env.rs | 10 ++++++++++ 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/crates/drainer/src/main.rs b/crates/drainer/src/main.rs index 2b4142abc0..9e8b8e275c 100644 --- a/crates/drainer/src/main.rs +++ b/crates/drainer/src/main.rs @@ -20,6 +20,9 @@ async fn main() -> DrainerResult<()> { let shutdown_intervals = conf.drainer.shutdown_interval; let loop_interval = conf.drainer.loop_interval; + #[cfg(feature = "vergen")] + println!("Starting drainer (Version: {})", router_env::git_tag!()); + let _guard = router_env::setup( &conf.log, router_env::service_name!(), diff --git a/crates/router/Cargo.toml b/crates/router/Cargo.toml index a1ddc37bbf..eb7fbc7ddb 100644 --- a/crates/router/Cargo.toml +++ b/crates/router/Cargo.toml @@ -16,7 +16,7 @@ email = ["external_services/email", "dep:aws-config", "olap"] frm = [] basilisk = ["kms"] stripe = ["dep:serde_qs"] -release = ["kms", "stripe", "basilisk", "s3", "email", "backwards_compatibility", "business_profile_routing", "accounts_cache", "kv_store", "connector_choice_mca_id", "profile_specific_fallback_routing"] +release = ["kms", "stripe", "basilisk", "s3", "email", "backwards_compatibility", "business_profile_routing", "accounts_cache", "kv_store", "connector_choice_mca_id", "profile_specific_fallback_routing", "vergen"] olap = ["data_models/olap", "storage_impl/olap", "scheduler/olap", "dep:analytics"] oltp = ["storage_impl/oltp"] kv_store = ["scheduler/kv_store"] diff --git a/crates/router/src/bin/router.rs b/crates/router/src/bin/router.rs index beb2869f99..a02758d8ed 100644 --- a/crates/router/src/bin/router.rs +++ b/crates/router/src/bin/router.rs @@ -34,6 +34,9 @@ async fn main() -> ApplicationResult<()> { conf.validate() .expect("Failed to validate router configuration"); + #[cfg(feature = "vergen")] + println!("Starting router (Version: {})", router_env::git_tag!()); + let _guard = router_env::setup( &conf.log, router_env::service_name!(), diff --git a/crates/router/src/bin/scheduler.rs b/crates/router/src/bin/scheduler.rs index 32e9cfc6ca..b800ecb897 100644 --- a/crates/router/src/bin/scheduler.rs +++ b/crates/router/src/bin/scheduler.rs @@ -22,8 +22,6 @@ use tokio::sync::{mpsc, oneshot}; const SCHEDULER_FLOW: &str = "SCHEDULER_FLOW"; #[tokio::main] async fn main() -> CustomResult<(), ProcessTrackerError> { - // console_subscriber::init(); - let cmd_line = ::parse(); #[allow(clippy::expect_used)] @@ -58,6 +56,12 @@ async fn main() -> CustomResult<(), ProcessTrackerError> { let scheduler_flow = scheduler::SchedulerFlow::from_str(&scheduler_flow_str) .expect("Unable to parse SchedulerFlow from environment variable"); + #[cfg(feature = "vergen")] + println!( + "Starting {scheduler_flow} (Version: {})", + router_env::git_tag!() + ); + let _guard = router_env::setup( &state.conf.log, &scheduler_flow_str, diff --git a/crates/router/src/middleware.rs b/crates/router/src/middleware.rs index 0f2c5bd2cb..c6c94d3a78 100644 --- a/crates/router/src/middleware.rs +++ b/crates/router/src/middleware.rs @@ -70,7 +70,13 @@ where pub fn default_response_headers() -> actix_web::middleware::DefaultHeaders { use actix_web::http::header; - actix_web::middleware::DefaultHeaders::new() + let default_headers_middleware = actix_web::middleware::DefaultHeaders::new(); + + #[cfg(feature = "vergen")] + let default_headers_middleware = + default_headers_middleware.add(("x-hyperswitch-version", router_env::git_tag!())); + + default_headers_middleware // Max age of 1 year in seconds, equal to `60 * 60 * 24 * 365` seconds. .add((header::STRICT_TRANSPORT_SECURITY, "max-age=31536000")) .add((header::VIA, "HyperSwitch")) diff --git a/crates/router_env/src/env.rs b/crates/router_env/src/env.rs index e57c38e7e4..644f9b50a1 100644 --- a/crates/router_env/src/env.rs +++ b/crates/router_env/src/env.rs @@ -167,3 +167,13 @@ macro_rules! profile { env!("CARGO_PROFILE") }; } + +/// The latest git tag. If tags are not present in the repository, the short commit hash is used +/// instead. Refer to the [`git describe`](https://git-scm.com/docs/git-describe) documentation for +/// more details. +#[macro_export] +macro_rules! git_tag { + () => { + env!("VERGEN_GIT_DESCRIBE") + }; +}