Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Preserve original error message in warn! logs #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 60 additions & 31 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,64 @@ where
}
}

/// Poll for an update one time.
async fn poll_for_update(&self, endpoint: &str, metrics_endpoint: &str) {
debug!("poll: retrieving features");

let response = match self.http.get(&endpoint).recv_string().await {
Ok(res) => res,
Err(e) => {
warn!("poll: failed to get features - {:?}", e);
return;
}
};

let features: Features = match serde_json::from_str(&response) {
Ok(res) => res,
Err(e) => {
warn!(
"poll: failed to parse features - {:?}, original json: {:?}",
e, response
);
return;
}
};

let metrics = match self.memoize(features.features) {
Ok(None) => {
return;
}
Ok(Some(metrics)) => metrics,
Err(e) => {
warn!("poll: failed to memoize features - {:?}", e);
return;
}
};

if !self.disable_metric_submission {
let req = self.http.post(&metrics_endpoint);
let body = match http_types::Body::from_json(&metrics) {
Err(e) => {
warn!("poll: error serializing metrics for upload - {:?}", e);
return;
}
Ok(body) => body,
};

let res = req.body(body).await;
match res {
Ok(res) => {
if res.status().is_success() {
debug!("poll: uploaded feature metrics")
}
}
Err(e) => {
warn!("poll: error uploading feature metrics - {:?}", e);
}
}
}
}

/// Query the API endpoint for features and push metrics
///
/// Immediately and then every self.interval milliseconds the API server is
Expand All @@ -693,37 +751,8 @@ where
let metrics_endpoint = Metrics::endpoint(&self.api_url);
self.polling.store(true, Ordering::Relaxed);
loop {
debug!("poll: retrieving features");
let res = self.http.get(&endpoint).recv_json().await;
if let Ok(res) = res {
let features: Features = res;
match self.memoize(features.features) {
Ok(None) => {}
Ok(Some(metrics)) => {
if !self.disable_metric_submission {
let mut metrics_uploaded = false;
let req = self.http.post(&metrics_endpoint);
if let Ok(body) = http_types::Body::from_json(&metrics) {
let res = req.body(body).await;
if let Ok(res) = res {
if res.status().is_success() {
metrics_uploaded = true;
debug!("poll: uploaded feature metrics")
}
}
}
if !metrics_uploaded {
warn!("poll: error uploading feature metrics");
}
}
}
Err(_) => {
warn!("poll: failed to memoize features");
}
}
} else {
warn!("poll: failed to retrieve features");
}
self.poll_for_update(&endpoint, &metrics_endpoint).await;

let duration = Duration::from_millis(self.interval);
debug!("poll: waiting {:?}", duration);
Delay::new(duration).await;
Expand Down