Skip to content

Commit

Permalink
fix: capture sentry events for the unidentified state
Browse files Browse the repository at this point in the history
  • Loading branch information
pjenvey committed Oct 26, 2023
1 parent cd597d3 commit 9657d2c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ impl WebPushClient {
}

/// Add User information and tags for this Client to a Sentry Event
pub fn add_sentry_info(&self, event: &mut sentry::protocol::Event) {
pub fn add_sentry_info(self, event: &mut sentry::protocol::Event) {
event.user = Some(sentry::User {
id: Some(self.uaid.as_simple().to_string()),
..Default::default()
});
let ua_info = self.ua_info.clone();
let ua_info = self.ua_info;
event
.tags
.insert("ua_name".to_owned(), ua_info.browser_name);
Expand Down
16 changes: 14 additions & 2 deletions autoconnect/autoconnect-ws/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::fmt;
use actix_ws::CloseCode;
use backtrace::Backtrace;

use autoconnect_ws_sm::SMError;
use autopush_common::errors::ReportableError;
use autoconnect_ws_sm::{SMError, WebPushClient};
use autopush_common::{errors::ReportableError, sentry::event_from_error};

/// WebPush WebSocket Handler Errors
#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -51,6 +51,18 @@ impl WSError {
pub fn close_description(&self) -> &str {
self.kind.as_ref()
}

/// Emit an event for this Error to Sentry if set to
pub fn capture_sentry_event(&self, client: Option<WebPushClient>) {
if !self.is_sentry_event() {
return;
}
let mut event = event_from_error(self);
if let Some(client) = client {
client.add_sentry_info(&mut event);
}
sentry::capture_event(event);
}
}

impl ReportableError for WSError {
Expand Down
15 changes: 8 additions & 7 deletions autoconnect/autoconnect-ws/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use tokio::{select, time::timeout};
use autoconnect_common::protocol::{ServerMessage, ServerNotification};
use autoconnect_settings::AppState;
use autoconnect_ws_sm::{UnidentifiedClient, WebPushClient};
use autopush_common::{errors::ReportableError, sentry::event_from_error};

use crate::{
error::{WSError, WSErrorKind},
Expand Down Expand Up @@ -61,7 +60,13 @@ pub(crate) async fn webpush_ws(
// NOTE: UnidentifiedClient doesn't require shutdown/cleanup, so its
// Error's propagated. We don't propagate Errors afterwards to handle
// shutdown/cleanup of WebPushClient
let (mut client, smsgs) = unidentified_ws(client, &mut msg_stream).await?;
let (mut client, smsgs) = match unidentified_ws(client, &mut msg_stream).await {
Ok(t) => t,
Err(e) => {
e.capture_sentry_event(None);
return Err(e);
}
};

// Client now identified: add them to the registry to recieve ServerNotifications
let mut snotif_stream = client.registry_connect().await;
Expand All @@ -75,11 +80,7 @@ pub(crate) async fn webpush_ws(
client.shutdown(result.as_ref().err().map(|e| e.to_string()));

if let Err(ref e) = result {
if e.is_sentry_event() {
let mut event = event_from_error(e);
client.add_sentry_info(&mut event);
sentry::capture_event(event);
}
e.capture_sentry_event(Some(client));
}
result
}
Expand Down

0 comments on commit 9657d2c

Please sign in to comment.