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

fix: Don't panic if telemetry client cannot be initialized. #1897

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion crates/sputnik/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub trait Report {
}

/// returns the Client to use when sending telemetry data
fn client(&self) -> Client;
fn client(&self) -> Result<Client, SputnikError>;
}

fn get_or_write_machine_id(path: &Utf8PathBuf) -> Result<Uuid, SputnikError> {
Expand Down
2 changes: 1 addition & 1 deletion crates/sputnik/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Session {
pub fn new<T: Report>(app: &T) -> Result<Session, SputnikError> {
let machine_id = app.machine_id()?;
let command = app.serialize_command()?;
let client = app.client();
let client = app.client()?;
let reporting_info = ReportingInfo {
is_telemetry_enabled: app.is_telemetry_enabled()?,
endpoint: app.endpoint()?,
Expand Down
14 changes: 7 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl Rover {
override_endpoint,
config,
is_sudo,
self.get_reqwest_client_builder()?,
self.get_reqwest_client_builder(),
))
}

Expand All @@ -261,21 +261,21 @@ impl Rover {
Ok(git_context)
}

pub(crate) fn get_reqwest_client(&self) -> RoverResult<Client> {
pub(crate) fn get_reqwest_client(&self) -> reqwest::Result<Client> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we continue to use RoverResult here? I'm wondering about the change in contract and whether it will mean the same thing. I was looking at SputnikError, and it has a derived From<reqwest::Error> for SputnikError::HttpError with the description "HttpError occurs when an error occurs while reporting anonymous usage data.".
With that, I don't know if we want to assign problems with constructing the reqwest client to a reqwest::Error or assign it to an ad hoc error (via anyhow).

Seems like we also might want to consider swapping out lazy-cell in the future, as it hasn't been updated in a while and we may able to use tools in std for this (like OnceLock).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main thing was trying to pass some error back to Sputnik from Rover, which can't be a RoverError because Sputnik can't depend on Rover.

As far as I know, though, Sputnik is only used by Rover, so that whole trait boundary thing could be gotten rid of if we make this a telemetry module within Rover instead of a separate crate. I think that should be its own PR, though.

In general, I do avoid type-erasing errors with something like anyhow unless I need to, since it makes it harder to find out what's going on.

if let Some(client) = self.client.borrow() {
Ok(client.clone())
} else {
self.client
.fill(self.get_reqwest_client_builder()?.build()?)
.expect("Could not overwrite existing request client");
.fill(self.get_reqwest_client_builder().build()?)
.ok();
self.get_reqwest_client()
}
}

pub(crate) fn get_reqwest_client_builder(&self) -> RoverResult<ClientBuilder> {
pub(crate) fn get_reqwest_client_builder(&self) -> ClientBuilder {
// return a copy of the underlying client builder if it's already been populated
if let Some(client_builder) = self.client_builder.borrow() {
Ok(*client_builder)
*client_builder
} else {
// if a request hasn't been made yet, this cell won't be populated yet
self.client_builder
Expand All @@ -285,7 +285,7 @@ impl Rover {
.accept_invalid_hostnames(self.accept_invalid_hostnames)
.with_timeout(self.client_timeout.get_duration()),
)
.expect("Could not overwrite existing request client builder");
.ok();
self.get_reqwest_client_builder()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl ClientBuilder {
}
}

pub(crate) fn build(self) -> Result<Client> {
pub(crate) fn build(self) -> reqwest::Result<Client> {
let client = Client::builder()
.gzip(true)
.brotli(true)
Expand Down Expand Up @@ -141,7 +141,7 @@ impl StudioClientConfig {
}
}

pub(crate) fn get_reqwest_client(&self) -> Result<Client> {
pub(crate) fn get_reqwest_client(&self) -> reqwest::Result<Client> {
if let Some(client) = &self.client {
Ok(client.clone())
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/utils/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ impl Report for Rover {
Ok(config.home.join("machine.txt"))
}

fn client(&self) -> Client {
self.get_reqwest_client()
.expect("could not get request client")
fn client(&self) -> Result<Client, SputnikError> {
self.get_reqwest_client().map_err(SputnikError::from)
}
}

Expand Down