From a8ef16efef75ae9daa17819c832cad0680c33c0f Mon Sep 17 00:00:00 2001 From: Nikhil Sinha Date: Sun, 10 Nov 2024 12:18:45 +0530 Subject: [PATCH 1/2] fix: multiple fixes around analytics 1. fix for multiple calls happening to analytics server global variable to hold latest version sets when server starts get from global variable in about api call 2. send `X-P-Stream` header in POST call to analytics server resolves the issue of hardcoded stream in analytics server code --- server/src/about.rs | 27 ++++++++++++++++++++------- server/src/analytics.rs | 8 +++++++- server/src/handlers/http/about.rs | 10 ++++------ server/src/utils/update.rs | 5 +---- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/server/src/about.rs b/server/src/about.rs index 81b612129..5a1d20e03 100644 --- a/server/src/about.rs +++ b/server/src/about.rs @@ -17,19 +17,20 @@ * */ +use crate::analytics; +use crate::option::Config; +use crate::storage::StorageMetadata; +use crate::utils::update::{self, LatestRelease}; use chrono::Duration; use chrono_humanize::{Accuracy, Tense}; use crossterm::style::Stylize; +use once_cell::sync::OnceCell; use std::env; use std::path::Path; use sysinfo::System; use ulid::Ulid; - -use crate::analytics; -use crate::option::Config; -use crate::storage::StorageMetadata; -use crate::utils::update; - +// Expose some static variables for internal usage +pub static LATEST_RELEASE: OnceCell> = OnceCell::new(); static K8S_ENV_TO_CHECK: &str = "KUBERNETES_SERVICE_HOST"; fn is_docker() -> bool { @@ -50,6 +51,18 @@ pub fn platform() -> &'static str { } } +pub fn set_latest_release(latest_release: Option) { + LATEST_RELEASE + .set(latest_release.clone()) + .expect("only set once") +} + +pub fn get_latest_release() -> &'static Option { + LATEST_RELEASE + .get() + .expect("latest release is fetched from global state") +} + // User Agent for Download API call // Format: Parseable/// (; ) pub fn user_agent(uid: &Ulid) -> String { @@ -122,7 +135,7 @@ pub async fn print(config: &Config, meta: &StorageMetadata) { } else { None }; - + set_latest_release(latest_release.clone()); print_about( current.released_version, latest_release, diff --git a/server/src/analytics.rs b/server/src/analytics.rs index c011b5961..92715d4ee 100644 --- a/server/src/analytics.rs +++ b/server/src/analytics.rs @@ -131,7 +131,13 @@ impl Report { pub async fn send(&self) { let client = reqwest::Client::new(); - let _ = client.post(ANALYTICS_SERVER_URL).json(&self).send().await; + + let _ = client + .post(ANALYTICS_SERVER_URL) + .header("X-P-Stream", "serverusageevent") + .json(&self) + .send() + .await; } } diff --git a/server/src/handlers/http/about.rs b/server/src/handlers/http/about.rs index 1b455d919..11b08e390 100644 --- a/server/src/handlers/http/about.rs +++ b/server/src/handlers/http/about.rs @@ -20,10 +20,9 @@ use actix_web::web::Json; use serde_json::json; use crate::{ - about, + about::{self, get_latest_release}, option::{Mode, CONFIG}, storage::StorageMetadata, - utils::update, }; use std::path::PathBuf; @@ -51,14 +50,13 @@ pub async fn about() -> Json { let meta = StorageMetadata::global(); let current_release = about::current(); - let latest_release = update::get_latest(&meta.deployment_id).await; - + let latest_release = get_latest_release(); let (update_available, latest_release) = match latest_release { - Ok(latest_release) => ( + Some(latest_release) => ( latest_release.version > current_release.released_version, Some(format!("v{}", latest_release.version)), ), - Err(_) => (false, None), + None => (false, None), }; let current_version = format!("v{}", current_release.released_version); diff --git a/server/src/utils/update.rs b/server/src/utils/update.rs index e063a1b82..f6fe8680f 100644 --- a/server/src/utils/update.rs +++ b/server/src/utils/update.rs @@ -25,7 +25,7 @@ use crate::about; use super::uid; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct LatestRelease { pub version: semver::Version, pub date: DateTime, @@ -37,20 +37,17 @@ pub async fn get_latest(deployment_id: &uid::Uid) -> Result Date: Sun, 10 Nov 2024 17:48:49 +0530 Subject: [PATCH 2/2] Stream name from constant --- server/src/analytics.rs | 3 ++- server/src/handlers.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/server/src/analytics.rs b/server/src/analytics.rs index 92715d4ee..9e6fca098 100644 --- a/server/src/analytics.rs +++ b/server/src/analytics.rs @@ -20,6 +20,7 @@ use crate::about::{current, platform}; use crate::handlers::http::cluster::utils::check_liveness; use crate::handlers::http::{base_path_without_preceding_slash, cluster}; +use crate::handlers::STREAM_NAME_HEADER_KEY; use crate::option::{Mode, CONFIG}; use crate::storage; use crate::{metadata, stats}; @@ -134,7 +135,7 @@ impl Report { let _ = client .post(ANALYTICS_SERVER_URL) - .header("X-P-Stream", "serverusageevent") + .header(STREAM_NAME_HEADER_KEY, "serverusageevent") .json(&self) .send() .await; diff --git a/server/src/handlers.rs b/server/src/handlers.rs index a69847239..2232ce53a 100644 --- a/server/src/handlers.rs +++ b/server/src/handlers.rs @@ -22,7 +22,7 @@ pub mod livetail; const PREFIX_TAGS: &str = "x-p-tag-"; const PREFIX_META: &str = "x-p-meta-"; -const STREAM_NAME_HEADER_KEY: &str = "x-p-stream"; +pub const STREAM_NAME_HEADER_KEY: &str = "x-p-stream"; const CACHE_RESULTS_HEADER_KEY: &str = "x-p-cache-results"; const CACHE_VIEW_HEADER_KEY: &str = "x-p-show-cached"; const USER_ID_HEADER_KEY: &str = "x-p-user-id";