diff options
author | Vincent Ambo <mail@tazj.in> | 2022-02-19T15·23+0300 |
---|---|---|
committer | tazjin <tazjin@tvl.su> | 2022-02-20T15·46+0000 |
commit | 2cff0712b3a3c08ef8190b1b139c78913a1f892f (patch) | |
tree | 993079d75722225499e2d7185ac250e26d7d9ecc /ops/journaldriver/src/main.rs | |
parent | f817ad86f24ce4387878cd10c4746599cb9167c9 (diff) |
refactor(journaldriver): Replace ureq with crimp r/3862
crimp is in TVL (//net/crimp), and it has fewer dependencies than ureq (including - finally - no more old time or chrono). Change-Id: I354f8f78b34a85abe3af671ffeffbe6a7fded5ee Reviewed-on: https://cl.tvl.fyi/c/depot/+/5318 Tested-by: BuildkiteCI Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'ops/journaldriver/src/main.rs')
-rw-r--r-- | ops/journaldriver/src/main.rs | 63 |
1 files changed, 29 insertions, 34 deletions
diff --git a/ops/journaldriver/src/main.rs b/ops/journaldriver/src/main.rs index 104df84b6e64..4c404e607e5b 100644 --- a/ops/journaldriver/src/main.rs +++ b/ops/journaldriver/src/main.rs @@ -112,28 +112,21 @@ lazy_static! { /// Convenience helper for retrieving values from the metadata server. fn get_metadata(url: &str) -> Result<String> { - let response = ureq::get(url) - .set("Metadata-Flavor", "Google") - .timeout_connect(5000) - .timeout_read(5000) - .call(); - - if response.ok() { - // Whitespace is trimmed to remove newlines from responses. - let body = response - .into_string() - .context("Failed to decode metadata response")? - .trim() - .to_string(); - - Ok(body) - } else { - let status = response.status_line().to_string(); - let body = response - .into_string() - .unwrap_or_else(|e| format!("Metadata body error: {}", e)); - bail!("Metadata failure: {} ({})", body, status) + let response = crimp::Request::get(url) + .header("Metadata-Flavor", "Google")? + .timeout(std::time::Duration::from_secs(5))? + .send()? + .as_string()?; + + if !response.is_success() { + bail!( + "Error response ({}) from metadata server: {}", + response.status, + response.body + ); } + + Ok(response.body.trim().to_owned()) } /// Convenience helper for determining the project ID. @@ -562,26 +555,28 @@ fn prepare_request(entries: &[LogEntry]) -> Value { /// Perform the log entry insertion in Stackdriver Logging. fn write_entries(token: &Token, request: Value) -> Result<()> { - let response = ureq::post(ENTRIES_WRITE_URL) - .set("Authorization", format!("Bearer {}", token.token).as_str()) + let response = crimp::Request::post(ENTRIES_WRITE_URL) + .json(&request)? + .header("Authorization", format!("Bearer {}", token.token).as_str())? // The timeout values are set relatively high, not because of // an expectation of Stackdriver being slow but just to - // eventually hit an error case in case of network troubles. + // eventually force an error in case of network troubles. // Presumably no request in a functioning environment will // ever hit these limits. - .timeout_connect(2000) - .timeout_read(5000) - .send_json(request); + .timeout(std::time::Duration::from_secs(5))? + .send()?; - if response.ok() { - Ok(()) - } else { - let status = response.status_line().to_string(); + if !response.is_success() { + let status = response.status; let body = response - .into_string() - .unwrap_or_else(|_| "no response body".into()); - bail!("Write failure: {} ({})", body, status) + .as_string() + .map(|r| r.body) + .unwrap_or_else(|_| "no valid response body".to_owned()); + + bail!("Writing to Stackdriver failed({}): {}", status, body); } + + Ok(()) } /// Attempt to read the initial cursor position from the configured |