Skip to content

Commit 615cfbc

Browse files
fix: multiple issues around analytics (#985)
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
1 parent 2be9716 commit 615cfbc

File tree

5 files changed

+34
-19
lines changed

5 files changed

+34
-19
lines changed

server/src/about.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
*
1818
*/
1919

20+
use crate::analytics;
21+
use crate::option::Config;
22+
use crate::storage::StorageMetadata;
23+
use crate::utils::update::{self, LatestRelease};
2024
use chrono::Duration;
2125
use chrono_humanize::{Accuracy, Tense};
2226
use crossterm::style::Stylize;
27+
use once_cell::sync::OnceCell;
2328
use std::env;
2429
use std::path::Path;
2530
use sysinfo::System;
2631
use ulid::Ulid;
27-
28-
use crate::analytics;
29-
use crate::option::Config;
30-
use crate::storage::StorageMetadata;
31-
use crate::utils::update;
32-
32+
// Expose some static variables for internal usage
33+
pub static LATEST_RELEASE: OnceCell<Option<LatestRelease>> = OnceCell::new();
3334
static K8S_ENV_TO_CHECK: &str = "KUBERNETES_SERVICE_HOST";
3435

3536
fn is_docker() -> bool {
@@ -50,6 +51,18 @@ pub fn platform() -> &'static str {
5051
}
5152
}
5253

54+
pub fn set_latest_release(latest_release: Option<LatestRelease>) {
55+
LATEST_RELEASE
56+
.set(latest_release.clone())
57+
.expect("only set once")
58+
}
59+
60+
pub fn get_latest_release() -> &'static Option<LatestRelease> {
61+
LATEST_RELEASE
62+
.get()
63+
.expect("latest release is fetched from global state")
64+
}
65+
5366
// User Agent for Download API call
5467
// Format: Parseable/<UID>/<version>/<commit_hash> (<OS>; <Platform>)
5568
pub fn user_agent(uid: &Ulid) -> String {
@@ -122,7 +135,7 @@ pub async fn print(config: &Config, meta: &StorageMetadata) {
122135
} else {
123136
None
124137
};
125-
138+
set_latest_release(latest_release.clone());
126139
print_about(
127140
current.released_version,
128141
latest_release,

server/src/analytics.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use crate::about::{current, platform};
2121
use crate::handlers::http::cluster::utils::check_liveness;
2222
use crate::handlers::http::{base_path_without_preceding_slash, cluster};
23+
use crate::handlers::STREAM_NAME_HEADER_KEY;
2324
use crate::option::{Mode, CONFIG};
2425
use crate::storage;
2526
use crate::{metadata, stats};
@@ -131,7 +132,13 @@ impl Report {
131132

132133
pub async fn send(&self) {
133134
let client = reqwest::Client::new();
134-
let _ = client.post(ANALYTICS_SERVER_URL).json(&self).send().await;
135+
136+
let _ = client
137+
.post(ANALYTICS_SERVER_URL)
138+
.header(STREAM_NAME_HEADER_KEY, "serverusageevent")
139+
.json(&self)
140+
.send()
141+
.await;
135142
}
136143
}
137144

server/src/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub mod livetail;
2222

2323
const PREFIX_TAGS: &str = "x-p-tag-";
2424
const PREFIX_META: &str = "x-p-meta-";
25-
const STREAM_NAME_HEADER_KEY: &str = "x-p-stream";
25+
pub const STREAM_NAME_HEADER_KEY: &str = "x-p-stream";
2626
const CACHE_RESULTS_HEADER_KEY: &str = "x-p-cache-results";
2727
const CACHE_VIEW_HEADER_KEY: &str = "x-p-show-cached";
2828
const USER_ID_HEADER_KEY: &str = "x-p-user-id";

server/src/handlers/http/about.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ use actix_web::web::Json;
2020
use serde_json::json;
2121

2222
use crate::{
23-
about,
23+
about::{self, get_latest_release},
2424
option::{Mode, CONFIG},
2525
storage::StorageMetadata,
26-
utils::update,
2726
};
2827
use std::path::PathBuf;
2928

@@ -51,14 +50,13 @@ pub async fn about() -> Json<serde_json::Value> {
5150
let meta = StorageMetadata::global();
5251

5352
let current_release = about::current();
54-
let latest_release = update::get_latest(&meta.deployment_id).await;
55-
53+
let latest_release = get_latest_release();
5654
let (update_available, latest_release) = match latest_release {
57-
Ok(latest_release) => (
55+
Some(latest_release) => (
5856
latest_release.version > current_release.released_version,
5957
Some(format!("v{}", latest_release.version)),
6058
),
61-
Err(_) => (false, None),
59+
None => (false, None),
6260
};
6361

6462
let current_version = format!("v{}", current_release.released_version);

server/src/utils/update.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::about;
2525

2626
use super::uid;
2727

28-
#[derive(Debug)]
28+
#[derive(Debug, Clone)]
2929
pub struct LatestRelease {
3030
pub version: semver::Version,
3131
pub date: DateTime<Utc>,
@@ -37,20 +37,17 @@ pub async fn get_latest(deployment_id: &uid::Uid) -> Result<LatestRelease, anyho
3737
.timeout(Duration::from_secs(8))
3838
.build()
3939
.expect("client can be built on this system");
40-
4140
let json: serde_json::Value = agent
4241
.get("https://download.parseable.io/latest-version")
4342
.send()
4443
.await?
4544
.json()
4645
.await?;
47-
4846
let version = json["tag_name"]
4947
.as_str()
5048
.and_then(|ver| ver.strip_prefix('v'))
5149
.and_then(|ver| semver::Version::parse(ver).ok())
5250
.ok_or_else(|| anyhow!("Failed parsing version"))?;
53-
5451
let date = json["published_at"]
5552
.as_str()
5653
.ok_or_else(|| anyhow!("Failed parsing published date"))?;

0 commit comments

Comments
 (0)