Skip to content

Commit

Permalink
chore: add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Oct 18, 2023
1 parent 2e6a9fe commit 371de47
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 54 deletions.
114 changes: 61 additions & 53 deletions src/handlers/push_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use {
},
serde::{Deserialize, Serialize},
std::sync::Arc,
tracing::instrument,
};

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -66,6 +67,8 @@ pub async fn handler(
let inner_packed = match res {
Ok((res, analytics_options_inner)) => (res.status().as_u16(), res, analytics_options_inner),
Err((error, analytics_option_inner)) => {
warn!("error handling push message: {error:?}");

#[cfg(feature = "analytics")]
let error_str = format!("{:?}", &error);
let res = error.into_response();
Expand Down Expand Up @@ -129,6 +132,7 @@ pub async fn handler(
Ok(response)
}

#[instrument(skip_all, fields(tenant_id, client_id = id, id = body.id))]
pub async fn handler_internal(
Path((tenant_id, id)): Path<(String, String)>,
StateExtractor(state): StateExtractor<Arc<AppState>>,
Expand Down Expand Up @@ -340,6 +344,7 @@ pub async fn handler_internal(
);

if tenant.suspended {
warn!("tenant suspended");
return Err((Error::TenantSuspended, analytics.clone()));
}

Expand All @@ -357,60 +362,63 @@ pub async fn handler_internal(

match provider.send_notification(client.token, body.payload).await {
Ok(()) => Ok(()),
Err(error) => match error {
Error::BadDeviceToken => {
state
.client_store
.delete_client(&tenant_id, &id)
.await
.map_err(|e| (Error::Store(e), analytics.clone()))?;
increment_counter!(state.metrics, client_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"client has been deleted due to a bad device token"
);
Err(Error::ClientDeleted)
}
Error::BadApnsCredentials => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid APNS Credentials")
.await
.map_err(|e| (e, analytics.clone()))?;
increment_counter!(state.metrics, tenant_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"tenant has been suspended due to invalid provider credentials"
);
Err(Error::TenantSuspended)
}
Error::BadFcmApiKey => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid FCM Credentials")
.await
.map_err(|e| (e, analytics.clone()))?;
increment_counter!(state.metrics, tenant_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"tenant has been suspended due to invalid provider credentials"
);
Err(Error::TenantSuspended)
Err(error) => {
warn!("error sending notification: {error:?}");
match error {
Error::BadDeviceToken => {
state
.client_store
.delete_client(&tenant_id, &id)
.await
.map_err(|e| (Error::Store(e), analytics.clone()))?;
increment_counter!(state.metrics, client_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"client has been deleted due to a bad device token"
);
Err(Error::ClientDeleted)
}
Error::BadApnsCredentials => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid APNS Credentials")
.await
.map_err(|e| (e, analytics.clone()))?;
increment_counter!(state.metrics, tenant_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"tenant has been suspended due to invalid provider credentials"
);
Err(Error::TenantSuspended)
}
Error::BadFcmApiKey => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid FCM Credentials")
.await
.map_err(|e| (e, analytics.clone()))?;
increment_counter!(state.metrics, tenant_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"tenant has been suspended due to invalid provider credentials"
);
Err(Error::TenantSuspended)
}
e => Err(e),
}
e => Err(e),
},
}
}
.map_err(|e| (e, analytics.clone()))?;

Expand Down
2 changes: 1 addition & 1 deletion src/providers/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ impl PushProvider for NoopProvider {
impl NoopProvider {
/// Insert empty notifications for a new token
fn bootstrap(&mut self, token: String) {
self.notifications.entry(token).or_insert_with(Vec::new);
self.notifications.entry(token).or_default();
}
}

0 comments on commit 371de47

Please sign in to comment.